Finding the ‘find’ command
The common problem users or admins run into when first dealing with a Linux machine is how to find the files they are looking for.
We discuss here the GNU/Linux find command which is one of the most important and much-used commands in Linux systems. find is used to search and locate a list of files and directories based on conditions you specify for files that match the arguments. Find can be used in a variety of conditions like you can find files by permissions, users, groups, file type, date, size and other possible criteria.
1. Find files under the current directory or in a specific directory:
ajoy@testserver:~$ find . -name file2.txt
./file2.txt
ajoy@testserver:~$ find /home/ajoy/ -name file5.txt
/home/ajoy/file5.txt
ajoy@testserver:~$
2. Find only files or only directories
ajoy@testserver:~$ find /home/ajoy -type f -name file1.txt
/home/ajoy/file1.txt
ajoy@testserver:~$ find /home/ajoy -type f -name “*.txt”
/home/ajoy/File1.txt
/home/ajoy/File5.txt
/home/ajoy/file3.txt
/home/ajoy/File2.txt
==result truncated=====
/home/ajoy/file9.txt
/home/ajoy/File9.txt
/home/ajoy/File7.txt
ajoy@testserver:~$
In the above example if we replace -type f to -type d find will look for only directories.
ajoy@testserver:~$ find . -type d -name “test”
./test
ajoy@testserver:~$
3. Find files ignoring the case
ajoy@testserver:~$ find . -iname file2.txt
./file2.txt
ajoy@testserver:/tmp$ find /home/ajoy/ -iname file5.txt
/home/ajoy/File5.txt
/home/ajoy/file5.txt
ajoy@testserver:~$
4. Find files limitting the directory traversal
ajoy@testserver:~$ find test/ -maxdepth 3 -name “*.py”
test/subdir1/subdir2/github_summary.py
test/subdir1/github_summary.py
test/github_summary.py
ajoy@testserver:~$ find test/ -maxdepth 2 -name “*.py”
test/subdir1/github_summary.py
test/github_summary.py
ajoy@testserver:~$
5. Find file inverting the match
ajoy@testserver:~$ find test/ -not -name “*.py”
test/
test/File1.txt
test/File5.txt
test/File2.txt
test/subdir1
test/subdir1/subdir2
test/File3.txt
test/File6.txt
test/File4.txt
test/File8.txt
test/File9.txt
test/File7.txt
ajoy@testserver:~$
6. Find with multiple search criterias
ajoy@testserver:~$ find test/ -name “*.txt” -o -name “*.py”
test/File1.txt
test/File5.txt
test/File2.txt
test/subdir1/subdir2/github_summary.py
test/subdir1/github_summary.py
test/File3.txt
test/File6.txt
test/File4.txt
test/File8.txt
test/github_summary.py
test/File9.txt
test/File7.txt
ajoy@testserver:~$
7. Find files with certain permissions
ajoy@testserver:~$ find . -type f -perm 0664
./file3.txt
./file6.txt
./file1.txt
./.gitconfig
./file5.txt
./file4.txt
./file2.txt
./file8.txt
./file7.txt
ajoy@testserver:~$
ajoy@testserver:~$ sudo find / -maxdepth 2 -perm /u=s 2>/dev/null
/bin/ping6
/bin/ping
/bin/fusermount
/bin/mount
/bin/umount
/bin/su
ajoy@testserver:~$
The above example shows the files with suid permissions set. The /dev/null bit bucket is used to remove errors related to permission while find traverses through directories
ajoy@testserver:~$ sudo find / -maxdepth 2 -type d -perm /o=t 2>/dev/null
/tmp
/var/tmp
/var/crash
/run/shm
/run/lock
ajoy@testserver:~$
The above example shows the directories with sticky bit
8. Find files based on users and groups
ajoy@testserver:~$ sudo find /var -user www-data
/var/cache/apache2/mod_cache_disk
ajoy@testserver:~$
ajoy@testserver:~$ sudo find /var -group crontab
/var/spool/cron/crontabs
ajoy@testserver:~$
9. Find files as per access time and modified time and changed time
ajoy@testserver:~$ find / -maxdepth 2 -mtime 50 ==> last modified 50 days back
ajoy@testserver:~$ find / -maxdepth 2 -atime 50 ==> last accessed 50 days back
ajoy@testserver:~$ find / -mtime +50 -mtime -100 ==> modified between 50 to 100 days ago
ajoy@testserver:~$ find . -cmin -60 ==> changed in last 60 minutes (1 hour)
ajoy@testserver:~$ find / -mmin -60 ==> modified in last 60 minutes
ajoy@testserver:~$ find / -amin -60 ==> accessed in last 60 minutes
10. Find files based on size
ajoy@testserver:~$ find . -size 50 ==> all files of 50 MB
ajoy@testserver:~$ find / -size +50 -size -100 ==> all files greater than 50 MB & less than 100 MB
ajoy@testserver:~$ find /var -type f -empty ==> empty file
ajoy@testserver:~$ find /var -type d -empty ==> empty directory
11. Listing out files found with find
ajoy@testserver:~$ find . -maxdepth 1 -name “*.txt” -exec ls -l {} \;
-rw-rw-r– 1 ajoy ajoy 0 Jan 31 17:40 ./file3.txt
-rw-rw-r– 1 ajoy ajoy 0 Jan 31 17:40 ./file6.txt
-rw-rw-r– 1 ajoy ajoy 0 Jan 31 17:40 ./file1.txt
-rw-rw-r– 1 ajoy ajoy 0 Jan 31 17:40 ./file5.txt
-rw-rw-r– 1 ajoy ajoy 0 Jan 31 17:40 ./file4.txt
-rw-rw-r– 1 ajoy ajoy 0 Jan 31 17:40 ./file2.txt
-rw-rw-r– 1 ajoy ajoy 0 Jan 31 17:40 ./file8.txt
-rw-rw-r– 1 ajoy ajoy 0 Jan 31 17:40 ./file7.txt
-rw-rw-r– 1 ajoy ajoy 0 Jan 31 17:40 ./file9.txt
ajoy@testserver:~$
The above command will long list the files matching the find criteria and the one given below will remove the files matching the given criteria
ajoy@testserver:~$ find . -maxdepth 1 -name “*.txt” -exec rm -f {} \;
ajoy@testserver:~$ ls
script.sh test
ajoy@testserver:~$
12. Finding Smallest and Largest file
4 largest files in the current directory and sub dorectories
ajoy@testserver:~$ sudo find /var -type f -exec ls -l {} \; |sort -n -r |head -4
-rwxr-xr-x 1 root root 998 Dec 5 2012 /var/lib/dpkg/info/sgml-base.preinst
-rwxr-xr-x 1 root root 991 Mar 25 2013 /var/lib/dpkg/info/ureadahead.postinst
-rwxr-xr-x 1 root root 980 Sep 23 2014 /var/lib/dpkg/info/man-db.postrm
-rwxr-xr-x 1 root root 972 May 15 2014 /var/lib/dpkg/info/locales.preinst
2 smallest files in the current directory and sub dorectories
ajoy@testserver:~$ sudo find /var -type f -exec ls -l {} \; |sort -n |head -2
-rw——- 1 daemon daemon 2 Jan 31 16:36 /var/spool/cron/atjobs/.SEQ
-rw——- 1 root ajoy 40 Jan 31 20:16 /var/lib/sudo/ajoy/0
ajoy@testserver:~$