60+ Practical find commands examples with explanation
find command is very much powerful command which can do good work when it’s needed to find files with conditions. Find command is useful when finding files with complex requirement such as size, permissions etc. Suppose we want to find a file which is a regular file and it’s size is more than 1GB and it’s accessed more than 90 days back and it’s owner is none and then delete it. This entire requirement is done with single command without even writing a shell script. Let’s see how we can use find command from basics to advanced in this post.
find command can find files according to
1) File names 2) File types 3) Permissions 4) Owners 5) Modified date and time 6) Size
Advanced finds command usages
1) Mix of all the above things 2) AND operator 3) Search for files and execute commands on them
a) chown, chmod, grep, ls, rm, mv, cp,md5sum 4) Multiple execute commands 5) Search for multiple files a) With different files with same extension b) Same file With different extensions 6) Search in multiple locations a) Exclude one location b) Search in multiple locations 7) OR( –o ) operator
8) ! Negation operator
9) Linux find command with Regular Expressions
10) Linux find commnd practical examples
Basics of file/folders search using find command
find files with name
find path options filename
Example1: find all the files in /home with name test.txt. Here –name is used to specify the filename.
find /home –name test.txt
Example2: find the files whose name is test.txt and in present working directory
find . –name test.txt
find –name test.txt
Example3: find all the files whose name contains both capital letters and small letters in it.
find /home –iname test.txt
-iname option is used to mention ignore the case sensitivity of a file.
Search for files depending on their File types:
Example4: Search for only directories whose name is var in / directory
find / -type d –name var
Example5: Search for an mp3 files whose name is temp.mp3
find / -type f –name temp.mp3
Below are the file types supported by find command, to know more about file types in Linux/Unix please have a look at our other post on File types.
|6||l||Symbolic link file|
Search for files depending on their Permissions
Example6:Search for a file name test.txt and it’s permissions are 775 in a given box
find / -perm 775 –name test.txt
Example7: How about searcing files with SUID bit set and file permissions are 755?
find / -perm 4755
Example8:How can i find SGID bit set files with 644 permissions?
find / -perm 2644
Example9: How can i find Sticky bit set files in my system with permissions 551?
find / -perm 1551
Example10:Search for all the files whose SUID bit is set
find / -perm /u=s
Example11: Search for all the files whose SGID bit is set
find / -perm /g+s
Note: We can use = or + interchangeably to check if a permissions is set or not as shown in above two examples.
Example12: Search for all the files whose StickyBit is set
find / -perm /o=t
Example13: Search for all the files whose owener permissions is read only.
find / -perm /u=r
Example14:Search for all the files which have user, group and others with executable permissions
find / -perm /a=x
To know about more on the permissions you have look at our other posts on chmod command.
Search according to Owners and group owners.
Example15: Search for all the files with name test.txt and the owner of this file is Surendra
find / -user Surendra –name test.txt
Example16: find all the files whos name is test.txt and owned by a group called redcluster
find / -group redcluster –name test.txt
to know more about owners and groups you have to look at our the previous post on chown command.
Search according to Modified date and time.
Below is the matrix which give you brief idea on how to search according to modified date, accessed date etc.
|+90||File status changed more then 90 days back||Modified more than 90 days back||Accessed more than 90 days back|
|90||File status changed exactly 90 days back||Modified exactly 90 days back||Accessed exactly 90 days back|
|-90||File status changed less than 90 days||Modified less than 90 days||Accessed less than 90 days back|
Example17: Search for a file: test.txt whose file status is changed more than 90 days back
find / -ctime +90 –name test.txt
Example18: Search for all the files which are modified exactly 90 days back
find / -mtime 90
Example19: Search for all the files with name test.txt which is accessed less than 90 days
find / -atime -90
Example20: find all the files which are modified more than 90 days back and less than 180 days
find / -mtime +90 –mtime -180
Below is the matrix which gives you brief idea on how to search according to modified time, accessed time in minutes etc.
|+30||File status changed more then 30 mins back||Modified more than 30 mins back||Accessed more than 30 mins back|
|30||File status changed exactly 30 mins back||Modified exactly 30 mins back||Accessed exactly 30 mins back|
|-30||File status changed less than 30 mins||Modified less than 30 mins||Accessed less than 30 mins|
Example21: find all the files changed less than 30mins
find / -cmin -30
Example22: find all the files modified exactly 30 mins back
find / -mmin 30
Example23: find all the files accessed more than 30 mins back
find / -amin +30
Example24: find all the files which are modified more than 5mins back and less than 25mins
find / -mmin +5 –mmin -25
Example25: I have new file called test.txt which is just created, now I want to get all the files which are created later this file creation.
find / -newer test.txt
Search for files/folders depending on the size with –size option
|c for bytes(8 bit’s)||Search for files more than 10c size||Search for files exactly 10b size||Search for files less than 10b size|
|k for kilobytes||Search for files more than 10k size||Search for files exactly 10k size||Search for files less than 10k size|
|M for Megabytes||Search for files more than 10M size||Search for files exactly 10M size||Search for files less than 10M size|
|G for Gigabytes||Search for files more than 10G size||Search for files exactly 10G size||Search for files less than 10G size|
Example26: Search for files whose size is more than 10bytes
find / -size +10c
Example27: Search for files which are exactly 10kb in /opt folder
find /opt –size 10k
Example28: Search for files which are less than 10MB in /var folder
find /var –size -10M
Example29: Search for files which are more than 1GB size in /usr folder
find /usr –size +1G
Example30: find all the empty files in my system
find / -size 0k
In next post on find command we will see more depth in to this find command. Please click here for more advanced Linux Find command usage
Latest posts by Surendra Anne (see all)
- FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018
- wget download a file to a directory in Linux/Unix - June 4, 2018
- GIT: How to compare two GIT branches? - June 3, 2018