Find command is one of the most useful & important command used in Linux. Its available & installed by default on almost all the versions of Linux. Everything on Linux is in the form of files, & we should be able to locate a file when required.
Here you can find few examples of find command.
1) Find a file with name in a directory
To look for a file by its name in a particular directory, command is
[root@centos ~]# find /root -name "lampblogs.txt"
This will look for lampblogs.txt file in /root folder. We can also look for all the files with .txt extension like below
[root@centos ~]# find /root -name "*.txt"
2) Find a file in multiple directories
To find a file by its name in multiple directories, we can use
[root@centos ~]# find /root /etc -name "lampblogs.txt"
With this command, we can look for lampblogs.txt file in /root & /etc directories
3) Find a file with name ignoring case
file with its name irrespective of the case i.e. whether its upper case or lower case, we can use ‘-iname‘ option
[root@centos ~]# find /root -iname "Lampblogs.txt"
This command will provide all the files that are named lampblogs.txt, whether its in lower case or upper case or in mixed cases.
4) Find all file types other than the mentioned type
[root@centos ~]# find /root -not -name "*.txt"
5) Invert match
[root@centos ~]# find ./test -not -name "*.php"
This is helpful when we know which files to exclude from the search.in the above example we found all files that do not have the extension of php, either non-php files.
6) This command checks all directories with a particular name like "usr" in root folder.
[root@centos ~]# find / -type d -name usr
7) This find command in linux checks all tmp files with file name tmp.php
[root@centos ~]# find / -type f -name tmp.php
8) Find files with multiple conditions
We can also combine more than one condition to search the files , Let’s suppose we want to search files of ‘.txt’ and ‘.html’ extensions
[root@centos ~]# find . -regex ".*\.\(txt\|html\)$"
9) Find files with using OR condition
We can also combine multiple search criteria & then look for the files based on the fulfillment of any of the one condition using OR operator.
[root@centos ~]# find -name "*.txt" -o -name "lampblogs*"
10) This command search for the files which were modified more than 60 days back
[root@centos ~]# find / -ctime +60 test.txt
11) This command searches all files which were modified exactly 60 days back
[root@centos ~]# find / -mtime 60
12) This command searches all files which were access less than 90 days
[root@centos ~]# find / -atime -90
13) This will search all files which were modified in between two time ranges, like more than 30 days and less than 60 days.
[root@centos ~]# find / -mtime +30 -mtime -60
14) This command fiinds all files which were modified less than 45 minutes.
[root@centos ~]# find / -cmin -45
15) It finds all files modified after 5 minutes and below 30 minutes.
[root@centos ~]# find / -mmin +5 -mmin -30
16) This command find files of particular size
If we want want to search a file for which we know the exact size, then we can use ‘-size‘ option with find command to locate the file.
[root@centos ~]# find / -size -5M
17) find command to locate all the files whose size is greater than 500 MB
[root@centos ~]# find / -size +500M
18) find command searches the file/folder with less than 100MB in a folder
[root@centos ~]# find /home -size -100M
19) find command to search only files or only directories
Sometimes we want to find only files or only directories with a given name. Find can do this easily as well.
[root@centos ~]# find ./test -name abc*
[root@centos ~]# find ./test -type f -name "abc*"
[root@centos ~]# find ./test -type d -name "abc*"
20) find all the files matching a criteria & delete them
[root@centos ~]# find / -type f -name 'lampblogs.*' -exec rm -f {} \;
Lets take another example where we want to delete files larger than 100MB
[root@centos ~]# find /var/log -type f -name '*.log' -size +100M -exec rm -f {} \;