Table of Contents
In this article, I will take you through 40 best examples of find command in Linux. Find is a powerful tool that allow us to quickly and easily scan through our Filesystems to find files and directories based on certain criteria. We can even perform some action on the results we get back.
Sometimes you might end up in writing long code using any of the programming languages to find files and directories based on some criteria which you can easily do through a simple find command in Linux. So it is very important to fully understand this command in Linux.
Find command in Linux:-
Also Read: How to Update or Upgrade CentOS 7.X to 7.7 with Easy Steps
1. Check Find command version
To check version of find command, use find -version
command.
root@localhost:~# find -version
find (GNU findutils) 4.5.11
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
2. Find all the files Only created in last 40 mins
You can run below find command in Linux to search all the files created in last 40 mins under /home/example.
root@localhost:~# find /home/example -type f -cmin -40
-cmin n: File’s status was last changed n minutes ago.
-type f: regular file
3. Find all the files Only modified in last 40 mins
You can use below command to find only the files modified in last 40 mins.
root@localhost:~# find /home/example -type f -mmin -40
-mmin n: File’s data was last modified n minutes ago.
Also Read: Linux Basics Command Cheat Sheet
4. Find all the files Only accessed in last 40 mins
You can use below command to find only the files accessed in last 40 mins.
root@localhost:~# find /home/example -type f -amin -40
-amin n: File was last accessed n minutes ago.
5. Find all the files and directories created in last 40 mins
You can use below find command in Linux to search all the files and directories created in last 40 mins.
root@localhost:~# find /home/example -cmin -40
6. Find all the files and directories modified in last 40 mins
You can use below find command in Linux to search all the files and directories modified in last 40 mins.
root@localhost:~# find /home/example -mmin -40
7. Find all the files and directories accessed in last 40 mins
You can use below command to find all the files and directories accessed in last 40 mins.
root@localhost:~# find /home/example -amin -40
8. Find all the files only created in last 40 mins
You can use below command to find only the files created in last 40 mins under /home/example.
root@localhost:~# find /home/example -type f -cmin -40
9. Find all the files only modified in last 40 mins
You can run below command to find only the files modified in last 40 mins under /home/example.
root@localhost:~# find /home/example -type f -mmin -40
10. Find all the files only accessed in last 40 mins
You can use below command to find only the files accessed in last 40 mins.
root@localhost:~# find /home/example -type f -amin -40
11. Find all the created files and directories older than 40 mins
You can use below command to check all the created files and directories older than 40 mins.
root@localhost:~# find /home/example -cmin +40
12. Find all the modified files and directories older than 40 mins
You can use below command to check all the modified files and directories older than 40 mins.
root@localhost:~# find /home/example -mmin +40
13. Find all the accessed files and directories older than 40 mins
You can use below command to search all the accessed files and directories older than 40 mins.
root@localhost:~# find /home/example -amin +40
14. Find all the created files only in last 5 days
You can use below command to find only files created in last 5 days.
root@localhost:~# find /home/example -type f -mtime -5
-mtime n: File’s data was last modified n*24 hours ago.
15. Find all the modified files only older than 5 days
You can use below command to find only modified files older than 5 days.
root@localhost:~# find /home/example -type f -mtime +5
16. Find all the files and directories modified in last 5 days
You can use below command to find all the files and directories modified in last 5 days.
root@localhost:~# find /home/example -mtime -5
17. Find all the modified files and directories older than 5 days
You can use below command to find all the modified files and directories older than 5 days.
root@localhost:~# find /home/example -mtime +5
18. Find a file “test.txt” at the maxdepth of level 2
You can use below find command in linux to search a file “test.txt” at the maxdepth of Level 2.
root@localhost:~# find /home/example/ -maxdepth 2 -name test.txt
-maxdepth levels: Descend at most levels (a non-negative integer) levels of directories below the starting-points.
19. Find a file “test.txt” at the mindepth of level 2
You can use below find command in linux to find a file “test.txt” at the mindepth of Level 2.
root@localhost:~# find /home/example/ -mindepth 2 -name test.txt
-mindepth levels: Do not apply any tests or actions at levels less than levels(a non-negative integer).
20. Find a file test.txt between mindepth of 2 and maxdepth of 4
You can run below command to find a file test.txt between mindepth of 2 and maxdepth of 4.
root@localhost:~# find /home/example -mindepth 2 -maxdepth 4 -name test.txt
21. Find all the files created by User “example”
You can use below command to find all the files created by User example.
root@localhost:~# find /home -user example
22. Find all Empty Files
You can use below command to find all empty files under /home/example path.
root@localhost:~# find /home/example -empty
-empty: File is empty and is either a regular file or a directory.
23. Find all Executable Files and Directories
You can use below command to find all executable files and directories under /home/example.
root@localhost:~# find /home/example -executable
-executable: Matches files which are executable and directories which are searchable (in a file name resolution sense) by the current user.
24. Find all Executable Files Only
You can use below command to find only executable files under /home/example.
root@localhost:~# find /home/example -type f -executable
25. Find all Empty Files Only
If you want to find all empty files only, you need to use below command.
root@localhost:~# find /home/examples -type f -empty
26. Find all the Case Insensitive match pattern
You can use below command to find the match pattern which does not need to be case sensitive under /home/example.
root@localhost:~# find /home/example -iname File1
-iname pattern: Like -name, but the match is case insensitive.
27. Find all the files on ext4 filesystem
You can use below command to find all the files on ext4 filesystem under current path.
root@localhost:~# find . -fstype ext4
-fstype type: File is on a filesystem of type ext4.
28. Find all the files with group name test
You can use below command to find all the files with its group name test
under /home/example
path.
root@localhost:~# find /home/example -group test
-group gname: File belongs to group gname (numeric group ID allowed).
29. Find all the files which does not belong to any known group
You can use below command to find all the files under current directory which does not belong to any known group.
root@localhost:~# find . -nogroup
-nogroup: No group corresponds to file’s numeric group ID.
30. Find all the files which does not belong to any known User
You can run below command to find all the files under current directory which does not belong to any known user.
root@localhost:~# find . -nouser
-nouser: No user corresponds to file’s numeric user ID.
31. Find all the files by their Group ID
You can use below command to find all the files with the file group ID 500.
root@localhost:~# find /home/example -group 500
32. Find all the files by their User ID
You can run below command to find all the files with its file User ID 500.
root@localhost:~# find /home/example -user 500
-user uname: File is owned by user uname (numeric user ID allowed).
33. Find all the files which is either executable by User or by Group
You can use below command to find all the files which is either executable by user or by group.
root@localhost:~# find /home/example -perm /u=x,g=x
-perm /mode: Any of the permission bits mode are set for the file. Symbolic modes are accepted in this form. You must specify `u’, `g’ or `o’ if you use a symbolic mode.
34. Find all the files which is executable by both User and Group
You can use below command to find all the files which is executable by both user as well as group.
root@localhost:~# find /home/example -perm -g+w,u+w
-perm -mode: All of the permission bits mode are set for the file.Symbolic modes are accepted in this form, and this is usually the way in which you would want to use them. You must specify `u’, `g’ or `o’ if you use a symbolic mode.
35. Find all files and directories which is executable by somebody
You can execute below command to find all files and directories which is executable by somebody.
root@localhost:~# find /home/example -perm /111
36. Find all the files created between last 5-10 days
You can run below command to find all the files created between last 5 to 10 days.
root@localhost:~# find . -ctime -10 -ctime +5
37. Find all the files accessed between last 5-10 days
You can use below command to find all the files accessed between last 5-10 days.
root@localhost:~# find . -atime -10 -atime +5
38. Find all the files modified between last 5-10 days
You can use below find command in linux to search all the files modified between last 5 to 10 days.
root@localhost:~# find . -mtime -10 -mtime +5
39. Find all the files which is either readable by User or by Group
You can use below command to find all the files which is either readable by user or by user and does not necessarily need to be readable by both.
root@localhost:~# find /home/example -perm /u=r,g=r
40. Find all the files which is readable by both User and Group
To find all the files which is readable by both User and Group, you need to run below command.
root@localhost:~#find /home/example -perm -g+r,u+r
Also Read: How to check Port Connectivity in Linux
Reference: Find Man Page