The command “find” in linux helps to find the files or folders/directories within a filesystem or across the filesystem based on the search text along with size, ownership, last modified date and time of the files. In this following post, you will learn what is find command in linux and it’s 24 types of examples.
The syntax of find command is
find <Path of the directory> <Search parameters>
General usage of find command :
1. To find all the “.txt” files within present directory, use below command.
find . –type f –name “*.txt”
. – To find all the files on the present working directory only.
-type f – To denote the type of files which is required. Here, instead of “f”, can use “d” for directory, “l” for links, “b” for blocks etc.,
-name “.txt” – As the name of the files are not known, “*” followed with an extension “.txt” has been used. Here “name” is case sensitive, for case insensitive, “iname” will be used.
2. To find all the directories matching with text/pattern “.d”, use below command.
find /etc -type d -name "*.d"
/etc – To find the directories within /etc directory.
-type d – To find for only directories/folders.
-name .d – To find the directories which only ends with .d extenstion.
Usage of find commands based on modified time:
3. To find the files that are modified before 5 minutes of time, use below command.
find <directory> –type f –mmin 5
4. To find all the files that are modified before 5 minutes ago in directory, use below command.
find <directory> -type f –mmin +5
5. To find all the files that are modified between last 5 minutes use the below command.
find <directory> -type f –mmin -5
Note : Similar to modified time, there are other two options available which are: ctime(changed time) – used to know the file’s metadata changed time on the files and atime(accessed time) – used to know the last accessed time of the files.
Instead on mtime in the command, you can use ctime and atime.
Usage of find commands based on modified days:
6. To find all the files that are modified 5 days ago, use the below command.
find <directory> -type f –mtime +5
7. To find all the files that are modified between 5 days, use the below command.
find <directory> -mtime -5
8. To find the files that are modified on exact day from present day, use below command.
find <directory> -mtime 3
Usage of find commands based on modified size:
9. To find the file that are more than 1024 bytes, use below command.
find <directory> -type f -size +1024c
10. To find the files that are between 0 to 5 kilobytes, use below command.
find <directory> -type f -size -5k
11. To find the files that are more than 1 megabytes, use below command.
find <directory> -type f -size +1M
12. To find the files that are more than 1 megabytes and less than 5 gigabytes, use below command.
find <directory> -type f -size +1M -size 5G
Usage of find commands based on files/folder permissions:
13. To find the files/folders that are with 644 permissions, use below command.
find <directory> -perm 644
14. To find the files/folders that are based on users, use below command.
find <directory> -user <username>
15. To find the specific files and delete it, use the below command
find <directory> -name `*.temp` -delete
Usage of find commands based on users:
16. To find all the files except one file, use the below command.
find <directory> -type f \(-iname “*.txt” ! –iname “user1.txt”\)
17. To find all the files except one file and delete outputs, use below command.
find <directory> -type f \(-iname “*.txt” ! –iname “user1.txt”\) –delete
18. To find all the directories/folders except 1 directory/folder, use below command.
find <directory> -type d \(-iname “*.d” ! –iname “user1.d” \)
19. To find all the directories/folders except 1 directory/folder and delete, use below command.
find <directory> -type d \(-iname “*.d” ! –iname “user1.d” \) –execdir rm –rfv {} \;
20. To find all files that have the executable permission bit enabled for the user that owns them and save the output in /tmp/foundthem.txt file, use below command.
find <directory> -type f -perm -u=x > /tmp/foundthem.txt
21. To find and remove all files having SETUID permission enabled, use below command.
find <directory> -perm /4000 -exec rm -f {} \;
22. To find all the files that are greater than 1 kilobytes and copy the output files, use below command.
find <directory> -type f -size +1k -exec cp "{}" /opt/ \;
23. To find all the files with particular depth of sub-directories, use below command.
find <directory> -type f –maxdepth 2
Here, the above command will search for the files under 2 sub-directories level.
24. To find all the files within the filesystem and not across the filesystem, use below command.
find <directory> -xdev -type f –name “*.txt”
How do you feel about this post? Drop your comments below..