Introduction
​In Linux when you are working with shell, Find Command in Linux is one of the most useful one where one can check all files/folders on you server. It helps to search for a specific text string in a file or files. The command sends the specified lines to the standard output device.
Let us see different find commands with variety of filters and parameters.
Find Commands in linux with Examples​
​1. This command checks all files in /home directory with the name fastwebhost.txt
find /home -name fastwebhost.txt
2. This find command in linux checks all files in current working directory with the name fastwebhost.txt
find . -name ​fastwebhost.txt
3. This command will search all files in home directory irrespective to case sensitive.
find /home -iname FastWebhost.txt​
4. This command checks all directories with a particular name like "usr" in root folder
find / -type d -name usr​
5. This find command in linux checks all tmp files with file name tmp.php
find / -type f -name tmp.php​
Check files/folders as per their permissions
6. The below command can check all "test.php" files with 777 permissions
​find / -perm 777 -name test.php
​7. The below find command in linux can check files with SUID bit set with permissions 755
"SUID" is defined as giving temporary permissions to a user to run a program/file with the permissions of the file owner rather that the user who runs it.
find / -perm 4755​
8. How to search SGID bit set files with 644 permissions
"SGID" is nothing but assigning temporary permissions to a user to run a program/file with the permissions of the file group permissions to become member of that group to execute the file. In simple words users will get file Group's permissions when executing a Folder/file/program/command. SGID is similar to SUID.
find / -perm 2644​
9. The below Command checks sticky bit files with permissions 755
A Sticky bit is a permission bit that is set on a file or a directory that lets only the owner of the file/directory or the root user to delete or rename the file. No other user is given privileges to delete the file which was created by any other user.
​find / -perm 1755
Using Find command based on users and groups
10. This command checks all the files with test.txt which is owned by a particular user.
find / -user fastwebhost -name test.txt
11. This find command in linux checks all the files with test.txt which is owned by a particular group.
find / -group fastweb​host -name test.txt
Find command to search the modified date & time of a particular file( In Days)
12. ​This command search for the files which were modified more than 90 days back
find / -ctime +90 test.txt​
13. This command searches all files which were modified exactly 90 days back
find / -mtime ​90
14. This command searches all files which were access less than 90 days
find / -atime -90​
15. This command will search all files which were modified in between two time ranges, like more than 90 days and less than 180 days.
find / -mtime +90 -mtime -180​
Find command to search when the files was modified (Short period, like in minutes/hours)
16. This command fiinds all files which were modified less than 45 minutes.
​find / -cmin -45
17. The below command to check all files with last change log if it is exatly below 45 minutes.
find ​/ -mmin 45
18. This command helps you check all files which have been changed more than 45 minutes back.
find / -amin +45​
19. It finds all files modified after 6 minutes and below 30 minutes.
find / -mmin +6 -mmin -30​
20. Lets see how to check the file which were created after creating test.txt file
​find / -newer test.txt
Find Command to search files/folders based on size
​21. It searches for the files/folders which are more than 10 bytes in size.
find / -size +10c​
22. It finds the file/folders which were more than 20 kb(kilo bytes) in any of the folder
find /tmp -size 20k​
23. The following command searches the file/folder with less than 10MB in a folder
find /tmp -size -10M​
24. The below command will search all files/folders which are more than 1GB
find /​ -size +1G
25. To search all empty files in server
find / -size 0c
26. Find all the files which are with more than size 50MB and less than 500GB and the owner of the file is test and the file name is fastwebhost.txt in /fast folder.
find /fast -size +50M -size -500M -user test -iname fastwebhost.txt
Executing commands on the files/folder found with "find" command
​Caution: When using -exec option which you are going to learn below, you should be more cautious. If you don't use wisely this can remove/change anything.
There are some instances when you want to execute commands on the found files with find command. -exec is the option used in find command to execute shell commands directly on found files/folders. Let’s discuss this with a simple example.
27. ​The below command with -exec option will search /tmp folder for *pass.* file and list the output on your screen.
find /tmp -iname *pass.* -exec ls -ld {} ;
28. The below command finds the file test.txt with the fast1 as owner in /home folder, than it will change the ownership to fast2 by executing the chown command using -exec
find /home -user fast1 -name test.txt -exec chown fast2.fast2 {} ;​
29. The below command will find the file "pass.txt" under /tmp folder and try to grep the word "Name" using executive command.
find /tmp -iname pass.txt -exec grep "Name" {} ;​
30. This command will search for test.txt.db file under the user fast and change the file permission to 755 using -exec option.
find /var/named -user fast -name test.txt.db -exec chmod 755 {} ;​
Conclusion
​Hope this articles helps you get familiar with Find Command in Linux. I will keep adding more commands to this article soon.