Find command is one of the best search tool in Unix/Linux.
There are a lot of options available with find command. This command is very useful in bash scripting also. We discussed one of the useful find command application to remove old emails from server.
You’ll get more details from here >> cronjob to delete old emails from server << Here I’m discussing some common switches of find command with detailed examples.
Like the name “find”, this command is using to search files under a directory hierarchy.
One simple example is shown below;
find / -name linux
Here the second part, that means “/” has an important role in this command syntax. This is the path for searching the file which has the name linux. This command will return all files with name “linux” under the location “/“.
Short-cuts
This will redirect you to the point 🙂
>>> Find files using its name
>>> Path pattern
>>> Find file/files with its permission
>>> User and group
>>> Empty files
>>> Find files with size
>>> Find file with type
>>> Switches related to modification time
>>> Find with inode & links
>>> Delete & exec operations
I, Numeric arguments
This is very helpful and should be useful in some advanced searches.
Please see the samples;
+n >> for greater than n,
-n >> for less than n,
n >> for exactly n.
For example it can use with the switch size. Here is the example:
There are two files in the directory “find”:
[root@vps find]# ll
-rw-r--r-- 1 root root 48894 Mar 21 11:13 crybit
-rw-r--r-- 1 root root 605062 Feb 28 04:09 csf.tgz
Here we go, we can find the file/files larger than 50k using +n option.
[root@vps find]# find . -size +50k
./csf.tgz
That’s it man, go ahead with your own way 😉
II, Switches and usages
1. -name pattern ==> Find the matched name pattern.
1.1 -iname pattern ==> Like -name, but the match is case insensitive.
The above commands will help you to find the file/files if you know its name. I’m explaining this with some different examples.
Examples
# find / -name test123.txt #with exact file name
/home/***/crybit/test123.txt
# find / -iname TeSt123.txt #case insensitive
/home/***/crybit/test123.txt
# find / -iname TeSt***.txt #case insensitive with incomplete file name
/home/***/crybit/test123.txt
# find / -name te**.txt #incomplete file name
/home/***/crybit/test123.txt
2. -path pattern ==> It will list out the exact path if it is exist.
You can find the exact path by using this. If you don’t know the exact path to the file which you’re searching, then move with this intelligent switch.
Examples
# find / -path "/e**wd"
/etc/pam.d/chpasswd
/etc/pam.d/passwd
/etc/cron.daily/passwd
/etc/passwd
/etc/security/opasswd
# find / -path "/us**.conf"
/usr/share/onboard/onboard-defaults.conf
/usr/share/popularity-contest/default.conf
/usr/share/base-files/nsswitch.conf
/usr/share/samba/smb.conf
# find / -path "/us**.sh"
/usr/share/onboard/scripts/changekbd.sh
/usr/share/alsa-base/alsa-info.sh
/usr/share/libreoffice/shell-lib-extensions.sh
/usr/share/debconf/confmodule.sh
3. -perm mode ==> File’s permission bits are exactly mode (octal or symbolic).
Find file/files with its permission. Here is the examples:
Example
Sample files:
# ll
total 8
drwxrwxr-x 2 root root 4096 Sep 5 20:37 ./
drwxr-xr-x 34 root root 4096 Sep 5 19:52 ../
-rwxrwxrwx 1 root eclinux 0 Sep 5 20:37 test1234.txt*
-rwx--x--x 1 root root 0 Sep 5 20:37 test1235.txt*
-rw-rw-r-- 1 root root 0 Sep 5 20:38 test123.txt
Find file/files with permission 644
# find ./ -perm 664
./test123.txt
Here./ is the path for searching(current directory). This will find out the file with permission 664.
You can also use alphabetical options.
3.1 -readable >> Matches files which are readable.
3.2 -writable >> Matches files which are writable.
3.3 -executable >> Matches files which are executable.
Example
# find ./ -executable
./
./test1235.txt
./test1234.txt
This will list all files with executable permission.
4. -gid & -uid
Find file/files with a specific user or group.
4.1 -gid n >> File’s numeric group ID is n.
4.2 -group gname >> File belongs to group gname (numeric group ID allowed).
4.3 uid n >> File’s numeric user ID is n.
4.4 -user name >> File belongs to user name (numeric user ID allowed).
Examples
# ll
total 8
drwxrwxr-x 2 root root 4096 Sep 5 20:37 ./
drwxr-xr-x 34 root root 4096 Sep 5 19:52 ../
-rwxrwxrwx 1 root eclinux 0 Sep 5 20:37 test1234.txt*
-rwx--x--x 1 root root 0 Sep 5 20:37 test1235.txt*
-rw-rw-r-- 1 root root 0 Sep 5 20:38 test123.txt
# find ./ -gid 1003
./test1234.txt
# find ./ -group eclinux
./test1234.txt
Similarly we can use -uid & -user.
5. -empty : this will find all files having empty content.
Example
# find ./ -empty
./test1235.txt
./test1234.txt
./test123.txt
6. -size n[cwbkMG] ==> File uses n units of space. The following suffixes can be used:
This is such a wonderful switch in practice. We already discussed an example above.
'b' for 512-byte blocks (this is the default if no suffix is used) 'c' for bytes 'w' for two-byte words 'k' for Kilobytes (units of 1024 bytes) 'M' for Megabytes (units of 1048576 bytes) 'G' for Gigabytes (units of 1073741824 bytes)
7. -type ==> Specify the file type.
To find different file types. In Linux there are 7 types of files. That are listed below:
b block (buffered) special c character (unbuffered) special d directory p named pipe (FIFO) f regular file l symbolic link s socket D door (Solaris)
Example
# find ./ -type f
./test1235.txt
./test1234.txt
./test123.txt
The file type “f” representing normal files. See the above example.
8. Switches related to modification time
This is an advanced option with find command. It will help you to list file/files with its modification time.
8.1 -amin n >> File was last accessed n minutes ago.
8.2 -atime n >> File was last accessed n*24 hours ago.
8.3 -cmin n >> File's status was last changed n minutes ago.
8.4 -ctime n >> File's status was last changed n*24 hours ago.
8.5 -mmin n >> File's data was last modified n minutes ago.
8.6 -mtime n >> File's data was last modified n*24 hours ago.
Example
# find ./ -mmin +1
./test1235.txt
./test1234.txt
As an example you can refer one of its application level example. In this example we need to remove old emails from the server. Here we can use the “-mtime” switch of find command. Refer this >> Cronjob to delete all emails older than one month <<
9. inode & links
9.1 -inum n >> File has inode number n.
9.2 -samefile name >> File refers to the same inode as name.
9.3 -links n >> File has n links.
Example
ls -i to find out the inode number.
# ls -i test123.txt
1316256 test123.txt
# find ./ -inum 1316256
./test123.txt
# ll
total 8
drwxrwxr-x 2 root root 4096 Sep 5 20:37 ./
drwxr-xr-x 34 root root 4096 Sep 5 19:52 ../
-rwxrwxrwx 1 root eclinux 0 Sep 5 20:37 test1234.txt*
-rwx--x--x 1 root root 0 Sep 5 20:37 test1235.txt*
-rw-rw-r-- 1 root root 0 Sep 5 20:38 test123.txt
# find ./ -links 1
./test1235.txt
./test1234.txt
./test123.txt
All three files having single links.
10. -delete & -exec operations
10.1 -delete : This switch is use to remove a particular that already specified in the find command. Use this switch with extra care.
Example
# find ./ -inum 1316256
./test123.txt
# find ./ -inum 1316256 -delete
# find ./ -inum 1316256
In this case, -delete switch remove the file test123.txt. Similarly we can remove anything that found by find command.
10.2 -exec : This will execute commands on the find syntax.
Example
# ll
total 8
drwxrwxr-x 2 root root 4096 Sep 5 20:37 ./
drwxr-xr-x 34 root root 4096 Sep 5 19:52 ../
-rwxrwxrwx 1 root eclinux 0 Sep 5 20:37 test1234.txt*
-rwx--x--x 1 root root 0 Sep 5 20:37 test1235.txt*
-rw-rw-r-- 1 root root 0 Sep 5 20:38 test123.txt
Run the command to change the permission.
# find ./ -type f -exec chmod 777 {} \;
# ll
total 8
drwxrwxr-x 2 root root 4096 Sep 5 20:37 ./
drwxr-xr-x 34 root root 4096 Sep 5 19:52 ../
-rwxrwxrwx 1 root eclinux 0 Sep 5 20:37 test1234.txt*
-rwxrwxrwx 1 root root 0 Sep 5 20:37 test1235.txt*
-rwxrwxrwx 1 root root 0 Sep 5 20:38 test123.txt*
the chmod command after -exec in find command change the file permission to 777.
# find ./ -type f -exec rm -rf {} \;
This will remove all files in the current working directory.
I believe this article will enrich your knowledge about the usages of find command in UNIX/LINUX.
Thank you!
More:
groupdel, groupmems, groupmod, useradd , usermod , chgrp, chown, ls, head, tail, top, ps, find, crontab
20+ Rsync command’s switches and common usages with examples – Unix/Linux
The “rsync” is a powerful command under the Linux environment.
The rsync stands for Remote Sync. Normally rsync is used to transfer file from one server(source) to another server(destination).
rsync has a lot of switches or option for managing the command much usefully. Here I am explaining the switches of rsync command with examples.
Great list – thanks. Using find forever and I didn’t know about iname and -delete.
Thanks for your feedback 🙂
Don’t forget to quote every argument with stars and other globbing characters.
find / -iname TeSt***.txt
should be written as
find / -iname ‘TeSt***.txt’