Strange problem with find command on ubuntu - linux

I used the 'find' command to find a file and encountered a strange issue:
the file exists, but 'find' can't find it
I found two .sock in /run with 'sudo find /run -name docker.sock'
$sudo find /run -name docker.sock
/run/march/docker.sock
/run/docker.sock
I got nothing when run 'sudo find /var -name docker.sock' and 'sudo find /var/run -name docker.sock'
$sudo find /var -name docker.sock
$sudo find /var/run -name docker.sock
$
but in fact there are two .sock in /var/run/, any comments?
$ls -al /var/run/docker.sock
srwxrwxrwx+ 1 root docker 0 Oct 18 20:45 /var/run/docker.sock
$ls -al /var/run/march/docker.sock/
total 0
drwxr-xr-x 2 root root 40 Oct 31 20:35 .
drwxr-xr-x 5 root root 100 Oct 31 20:35 ..
$ls -al /var/run/march/
total 0
drwxr-xr-x 5 root root 100 Oct 31 20:35 .
drwxr-xr-x 34 root root 1120 Oct 31 23:45 ..
drwxr-xr-x 2 root root 40 Oct 31 20:35 docker
drwxr-xr-x 2 root root 40 Oct 31 20:35 docker.pid
drwxr-xr-x 2 root root 40 Oct 31 20:35 docker.sock
$
$
BTW it's on Ubuntu 20.04.2 LTS
Thanks in advance

As /var/run is a symbolic link to /run, you have to tell find to follow links :
sudo find -L /var/run -name docker.sock

Related

Set the permissions of all files copied in a folder the same

I would like to create a folder (in Linux) that can be used as cloud-like storage location, where all files copied there automatically will have g+rw permissions (without the need of chmod'ing), such that they are readable and writable by people beloning to that specific group.
You can use the command setfacl, e.g.:
setfacl -d -m g::rwx test/
It sets the rwx permissions to every new file in test/ folder.
$ touch test/test
$ ls -la test/
total 48
drwxr-xr-x 2 manu manu 4096 Jan 28 08:39 .
drwxrwxrwt 20 root root 40960 Jan 28 08:39 ..
-rw-r--r-- 1 manu manu 0 Jan 28 08:39 test
$ setfacl -d -m g::rwx test/
$ ls -la test/
total 48
drwxr-xr-x+ 2 manu manu 4096 Jan 28 08:39 .
drwxrwxrwt 20 root root 40960 Jan 28 08:39 ..
-rw-r--r-- 1 manu manu 0 Jan 28 08:39 test
$ touch test/test2
$ ls -la test/
total 48
drwxr-xr-x+ 2 manu manu 4096 Jan 28 08:40 .
drwxrwxrwt 20 root root 40960 Jan 28 08:39 ..
-rw-r--r-- 1 manu manu 0 Jan 28 08:39 test
-rw-rw-r-- 1 manu manu 0 Jan 28 08:40 test2

How to export directory created inside the Docker image to the host machine?

The program I'm running inside the Docker image, first creates a directory and writes some file into the directory.
To transfer the directory onto the host machine, I've mounted a datadir/ and then moved the directory created inside the image into the mounted directory, e.g.:
mkdir datadir
DATADIR=datadir/
docker run -i \
-v $(pwd)/$DATADIR:/$DATADIR/ ubuntu \
bash -c "mkdir /x1 && echo 'abc' > x1/test.txt && mv x1 $DATADIR"
But when I tried to access datadir/x1, it has root as the owner and it comes with read-only permissions:
$ mv datadir/x1/ .
mv: cannot move 'datadir/x1/' to './x1': Permission denied
$ ls -lah datadir/x1/
total 12K
drwxr-xr-x 2 root root 4.0K Jun 28 16:38 .
drwxrwxr-x 3 alvas alvas 4.0K Jun 28 16:38 ..
-rw-r--r-- 1 root root 4 Jun 28 16:38 test.txt
Is mounting the additional volume and copying the created directory inside the image the right approach to move files between the Docker image and the host machine? If not, what's the "canonical" way to perform the same operation?
About the directory permissions, what should be the correct way to assign the host machine permission to any files inside the mounted volume?
I've tried to chmod -R 777 inside the Docker image but I don't think that's the safe approach, i.e.:
$ docker run -i -v $(pwd)/$DATADIR:/$DATADIR/ -i ubuntu bash -c "mkdir /x1 && echo 'abc' > x1/test.txt && mv x1 $DATADIR && chmod -R 777 $DATADIR"
$ mv datadir/x1/ .
$ ls -lah x1
total 12K
drwxrwxrwx 2 root root 4.0K Jun 28 16:47 .
drwxrwxr-x 12 alvas alvas 4.0K Jun 28 16:47 ..
-rwxrwxrwx 1 root root 4 Jun 28 16:47 test.txt
To avoid permission issues use docker cp
For example:
# This is the directory you want to save the outputs
mkdir datadir
# We create a directory and file inside it, inside the Docker image.
# And we are naming the Docker image "thisinstance"
docker run -i --name thisinstance ubuntu \
bash -c "mkdir /x1 && echo 'abc' > x1/test.txt"
# Copies the new directory inside the Docker image to the host.
docker cp thisinstance:/x1 datadir/
# Destroy the temporary container
docker rm thisinstance
# Check the ownership of the directory and file
ls -lah datadir/x1/
[out]:
drwxr-xr-x 3 alvas 679754705 102B Jun 29 10:36 ./
drwxr-xr-x 3 alvas 679754705 102B Jun 29 10:36 ../
-rw-r--r-- 1 alvas 679754705 4B Jun 29 10:36 test.t

chmod doesn't work in mounted partition

I have this file in my mounted partition
/path/to/hardDiskDrive/$ ls -l
-rw------- 1 arash arash 92827804 Jun 15 17:35 qt-creator-opensource-linux-x86_64-4.0.2.run
and then try to chmod it but nothing happens even with sudo
/path/to/hardDiskDrive/$ chmod +x qt-creator-opensource-linux-x86_64-4.0.2.run
/path/to/hardDiskDrive/$ ls -l
-rw------- 1 arash arash 92827804 Jun 15 17:35 qt-creator-opensource-linux-x86_64-4.0.2.run
but when i copy it to my Linux home directory everything works fine ..
What is the resne of this ?!!
~/Desktop $ ls -l
-rw------- 1 arash arash 92827804 Jun 15 17:35 qt-creator-opensource-linux-x86_64-4.0.2.run
~/Desktop $ chmod +x qt-creator-opensource-linux-x86_64-4.0.2.run
~/Desktop $ ls -l
-rwx--x--x 1 arash arash 92827804 Jun 15 17:35 qt-creator-opensource-linux-x86
What is the reason of this?!! and How can i run this file from original place in Hard Drive?
thanks

Script to remove all directories older than x days but keep certain ones

I'm trying to write a bash script to remove all directories and their files but keep certain ones.
drwxr-xr-x 20 ubuntu admin 4096 Jan 21 17:58 .
drwxr-xr-x 8 ubuntu admin 4096 Nov 21 16:45 ..
drwxr-xr-x 11 ubuntu admin 4096 Jan 9 13:09 1763
drwxr-xr-x 11 ubuntu admin 4096 Jan 16 16:46 1817
drwxr-xr-x 11 ubuntu admin 4096 Jan 16 17:39 1821
drwxr-xr-x 11 ubuntu admin 4096 Jan 19 10:15 1823
drwxr-xr-x 11 ubuntu admin 4096 Jan 19 11:57 1826
drwxr-xr-x 11 ubuntu admin 4096 Jan 19 14:55 1827
drwxr-xr-x 11 ubuntu admin 4096 Jan 19 21:34 1828
drwxr-xr-x 11 ubuntu admin 4096 Jan 20 13:29 1833
drwxr-xr-x 11 ubuntu admin 4096 Jan 20 16:13 1834
drwxr-xr-x 11 ubuntu admin 4096 Jan 21 10:06 1838
drwxr-xr-x 11 ubuntu admin 4096 Jan 21 12:51 1842
drwxr-xr-x 11 ubuntu admin 4096 Jan 21 15:20 1845
drwxr-xr-x 11 ubuntu admin 4096 Jan 22 13:00 1848
drwxr-xr-x 11 ubuntu admin 4096 Nov 24 16:34 217
drwxr-xr-x 11 ubuntu admin 4096 Dec 2 20:44 219
drwxr-xr-x 11 ubuntu admin 4096 Dec 15 16:42 221
drwxr-xr-x 11 ubuntu admin 4096 Dec 16 12:04 225
drwxr-xr-x 2 ubuntu admin 4096 Jan 20 16:10 app-conf
lrwxrwxrwx 1 ubuntu admin 19 Jan 21 17:58 latest -> /opt/qudiniapp/1848
In the example above we'd want to clear out all non sym-linked folders except the app-conf folder.
The plan is to have this triggered by my ansible deployment script before deployment so we can keep our server from filling up with builds.
Provided, all directories, that are to be deleted, consist only of numbers, this would be one way solve this:
cd /tempdir
rm -rf $(find . -type d -name "[0-9]*" | grep -v "$(readlink latest)")
As this is a housekeepingjob, you should create a cronjob, that regularly deletes old directories. The find command would then include checking, for example, if the last modification time is beyond a number of days:
rm -rf $(find . -type d -mtime +20 -name "[0-9]*" | grep -v "$(readlink latest)")
bash script:
#!/bin/bash
find /your/path -type d ! \( -path '*app-conf*' -prune \) -mtime +2 -delete
per man find
-P Never follow symbolic links. This is the default behaviour. When find examines or prints information a file, and the file is a symbolic link, the information used shall be taken from the properties of the symbolic link itself.
-mtime n File's data was last modified n*24 hours ago. See the comments for -atime to understand how rounding affects the interpretation of file modification times.
This is what I use in my Ansible deployments, hope it will be helpful for you as it does almost exactly what you need.
I always remove oldest release on each deployment if there are >= 5 builds in "{{ releases_path }}" directory. "{{ releases_path }}" contains directories which are basically Git commit hashes (long)
- name: Find oldest release to remove
shell: '[[ $(find "{{ releases_path | quote }}" -maxdepth 1 -mindepth 1 -type d | wc -l) -ge 6 ]] && IFS= read -r -d $"\0" line < <(find "{{ releases_path | quote }}" -maxdepth 1 -mindepth 1 -type d -printf "%T# %p\0" 2>/dev/null | sort -z -n); file="${line#* }"; echo "$file";'
args:
executable: /bin/bash
chdir: "{{ releases_path }}"
register: releasetoremove
changed_when: "releasetoremove.stdout != ''"
- debug: var=releasetoremove
- name: Remove oldest release
file: path={{ releasetoremove.stdout }} state=absent
when: releasetoremove|changed
This is what I always have on each server in releases directory (last 5 always kept):
$ ls -lt | cut -c 28-
62 Jan 22 17:42 current -> /srv/releases/2a7b80c82fb1dd658a3356fed7bba9718bc50527
4096 Jan 22 17:41 2a7b80c82fb1dd658a3356fed7bba9718bc50527
4096 Jan 22 15:22 73b1252ab4060833e43849e2e32f57fea6c6cd9b
4096 Jan 22 14:47 9df7f1097909aea69916695194ac41938a0c2e9a
4096 Jan 22 14:16 f6a2862d70f7f26ef75b67168a30fb9ef2202555
4096 Jan 22 13:49 fa89eefc5b2505e153b2e59ed02a23889400c4bf

find command + remove older directories according to directory time stamp

I want to delete directories that older than 180 days
for example directories that older than 180 days:
drwxr-xr-x 2 root root 4096 Oct 1 2009 nis
drwxr-xr-x 3 root root 4096 Nov 4 2012 pkgs
I use this command:
find /var/tmp -depth -mindepth 1 -type d -ctime +180 -exec rm -rf {} \;
After I run the find command , I see that the older directories are still exist
Please advice what wrong with my find command?
[root#vm1 /var/tmp]# ls -ltr
total 20
drwxr-xr-x 2 root root 4096 Oct 1 2009 nis
drwxr-xr-x 3 root root 4096 Nov 4 2012 pkgs
drwxr-x--- 2 root root 4096 Dec 3 08:24 1
drwxr-x--- 2 root root 4096 Dec 3 08:41 2
drwxr-x--- 2 root root 4096 Dec 3 08:41 3
[root#vm1 /var/tmp]# find /var/tmp -depth -mindepth 1 -type d -ctime +180 -exec rm -rf {} \;
[root#vm1 /var/tmp]# ls -ltr
total 20
drwxr-xr-x 2 root root 4096 Oct 1 2009 nis
drwxr-xr-x 3 root root 4096 Nov 4 2012 pkgs
drwxr-x--- 2 root root 4096 Dec 3 08:24 1
drwxr-x--- 2 root root 4096 Dec 3 08:41 2
drwxr-x--- 2 root root 4096 Dec 3 08:41 3
I also try this ( but not remove the old dir ) the -mtime only change the date of the old dir to the current date
find /var/tmp -depth -mindepth 1 -type d -mtime +180 -exec rm -rf {} \;
-t sort by modification time
try
find /var/tmp -depth -mindepth 1 -type d -mtime +180 -exec rm -rf {} \;
Update : delete options depth and mindepth

Resources