- 25+ most used find commands in Linux [Cheat Sheet]
- Syntax to use find command
- Different examples to use find command
- 1. Find all files and directories in the current directory
- 2. Find files in the specific directory
- 3. Find only directories with find command
- 4. Find only files with find command
- 5. Find symbolic link files only
- 6. Find files by name
- 7. Find directories by names
- 8. Find files by case insensitive name
- 9. Find specific files by extension
- 10. Find files having specific patterns
- 11. Find empty files and directories
- 12. Find files with executable permission
- 13. Find files based on size
- 14. Find files by size range
- 15. Find files by permission
- 16. Find files by modification time
- 17. Find files by last accessed time
- 18. Find files by changed time
- 19. Find all files owned by specific user
- 20. Find all files owned by specific group name
- 21. Find and delete files
- 22. Find text in files
- 23. Find text files and view their content
- 24. Find the bash script file and execute it
- 25. Find files by limiting the directory depth to search
- 26. Find and print files that do not match the pattern
- Conclusion
- What’s Next
- Further Reading
- How to list empty files? (Bash)
- 3 Answers 3
25+ most used find commands in Linux [Cheat Sheet]
find command is one of the popular and commonly used tools in Unix-based operating systems. As its name suggests, it finds the files and directories in a directory hierarchy. You can pass different parameters and search files by their name, extension, type, size, permissions, modification time, owner, groups, and more. It performs a recursive search on the specified directory which means it searches in all sub-directories too.
Syntax to use find command
The syntax to use the find command is:
$ find [options] [path] [expression]
- options : It includes the options that must appear before the first path name: -H , -L , -P , -D , and -O .
- path : It indicates the directory path where you want to search the files.
- expression : It defines search options, patterns to match, and actions to perform on the files.
The default path is the current working directory. When the find command is used without any parameters, it lists all files and folders in the current directory and its sub-directories.
Different examples to use find command
1. Find all files and directories in the current directory
Use find command followed by . symbol to search for all files and directories in the current working directory.
Sample Output:
2. Find files in the specific directory
To find files in the specific directory, use the following syntax.
The following example searches for all files and directories present in the /etc/apt directory.
Sample Output:
3. Find only directories with find command
The -type option followed by d searches for directories only. The following example finds all directories in the home/golinux/computing directory.
$ find /home/golinux/computing -type d
Sample Output:
4. Find only files with find command
The -type f is used for regular files. It lists all files present in the specified directory.
$ find /home/golinux/computing -type f
Sample Output:
5. Find symbolic link files only
The -type l will search for symbolic links only.
Sample Output:
6. Find files by name
You can find files by name using the -name option. It is case-sensitive. It is useful for checking whether the file is present in the specified directory hierarchy.
Run this command to locate new.txt files in the current working directory.
Sample Output:
7. Find directories by names
Similarly, you can search for directories by name with the following command. It finds all directories having a name spark in the current working directory.
Sample Output:
8. Find files by case insensitive name
You can use -iname option to search for files by name where the match is case insensitive.
Sample Output:
9. Find specific files by extension
With this method, you can find all files in the directory which has the same extension. For instance, to locate all .cpio files, use the command below.
Sample Output:
10. Find files having specific patterns
You can use find command to search files by specific patterns, like files whose name begins with prep . The following command locates files whose name starts with prep in the /tmp directory.
$ find /home/golinux/record -name "tes*"
Sample Output:
# find /tmp/ -type f -name "prep*" /tmp/prepTspInstall18457.log /tmp/prepTspInstall2897.log /tmp/prepTspInstall11221.log /tmp/prepTspInstall15505.log /tmp/prepTspInstall13806.log
11. Find empty files and directories
The -empty option searches for the empty files and directories only.
$ find /home/golinux/test -empty
Sample Output:
12. Find files with executable permission
The -executable flag matches files that are executable and directories that are searchable. To search executable files only in the current directory, you can run the following command.
Sample Output:
13. Find files based on size
You can find files by size using the -size option. The following suffixes are used with -size .
- b : 512 byte blocks (default, if no suffix is used)
- c : bytes
- w : two-byte words
- k : Kilobytes (1024 bytes)
- M : Megabytes
- G : Gigabytes
The following command finds all files that are exactly 960 bytes in the specified directory.
$ find /home/golinux/record -type f -size 960c
Sample Output:
To find files that are greater or smaller than the specified size, use the + or — operator respectively.
This command finds files that are greater than 20MB in the Downloads directory.
$ find /home/golinux/Downloads -type f -size +20MB
The following command finds files that are smaller than 20MB in the Downloads directory.
$ find /home/golinux/Downloads -type f -size -20MB
14. Find files by size range
You can also search files in a specific size range. For example, this command searches for files having sizes between 1 and 20 MB.
$ find /home/golinux/Downloads -type f -size +1M -size -20M
Sample Output:
15. Find files by permission
You can use -perm option to search for files on the basis of permissions. The following command finds all files which have read and write permission for their owner and group but are only readable by other users.
Sample Output:
Similarly, to search for files that have full access (read/write/execute) to anyone, you can use:
16. Find files by modification time
The -mtime option finds files that were last modified n*24 hours ago. The following command will search for all files that are modified 15 days ago.
$ find /home/golinux/test -mtime 15
Sample Output:
You can also use + or — operator for specifying the greater or smaller than n value. For example, this command will search files that are modified less than 15 days ago.
$ find /home/golinux/test -mtime -15
Sample Output:
17. Find files by last accessed time
The -amin option finds files that were last accessed n minutes ago. You can use the following command to search for files that were last accessed 5 minutes ago.
$ find /home/golinux/test -amin 5
Sample Output:
The -atime option finds files that were last accessed n*24 hours ago. This command will search files that were last accessed 5 days ago.
$ find /home/golinux/test -atime 5
18. Find files by changed time
The -cmin option allows you to search files that are changed n minutes ago. You can run the following command to find all files that are changed 5 minutes ago.
Sample Output:
To find files that are changed n days ago, you have to use the -ctime option. The following command searches for all files that are changed 5 days ago.
19. Find all files owned by specific user
You can use -user option to find files that are owned by the specified user. For example, this command will search all files that are owned by the user deepak .
Sample Output:
20. Find all files owned by specific group name
The -group option allows you to search files based on the group name. The following command finds all files in the current directory that belong to the group student .
Sample Output:
21. Find and delete files
You can use -delete flag at the end of a command to delete files if found in the specified location. For example, the following command will remove all .txt files from the directory /home/golinux/test .
$ find /home/golinux/test -name "*.txt" -delete
Sample Output:
It is always the best choice to check files before deleting them.
22. Find text in files
The -exec flag runs the specified command on the files. The following command finds the text book in all .txt files in the current working directory.
$ find . -name "*.txt" -exec grep 'book' <> \;
Here, the -exec executes the grep command with a pattern book to match. The brackets <> are replaced by the filename found by the find command. The semicolon ; denotes the end of a command argument.
Sample Output:
23. Find text files and view their content
Similarly, you can run the cat command with the -exec option to view the content of all text files.
$ find /home/golinux/test -name "*.txt" cat <> \;
Sample Output:
24. Find the bash script file and execute it
The following command finds a bash script file script.sh in the current working directory and runs the script.
$ find . -name "script.sh" -exec bash <> \;
Sample Output:
25. Find files by limiting the directory depth to search
You can limit the recursion of the depth using the -maxdepth or -mindepth flag.
The following command searches for all .txt files in the directories that are not more than 3 levels deeper than the current working directory.
$ find . -maxdepth 3 -name "*.txt"
Sample Output:
The following example includes only the directories that are at least 3 levels deeper than the current working directory.
$ find . -maxdepth 3 -name "*.txt"
Sample Output:
To search for all .txt files in the directories that are at least 2 levels and not more than 4 levels deeper than the current working directory, you can use this command.
$ find . -mindepth -2 -maxdepth 4 -name "*.txt"
26. Find and print files that do not match the pattern
You can use the -not or ! option to search files that do not match the search pattern. For example, to find all files that are not owned by user deepak in the directory /home/golinux/test , you can use the command below.
$ find /home/golinux/test -type f -not -user deepak
$ find /home/golinux/test -type f ! -user deepak
Sample Output:
Conclusion
You have learned various examples of the find command to search files and directories based on different conditions. We have also shown you how to find files and perform operations on them using the find command. find is one of the most powerful commands and very useful for searching files and directories. Now you should know how to use find command in Linux. If you have any questions, please let us know in the comment section.
What’s Next
Further Reading
Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud
If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.
For any other feedbacks or questions you can either use the comments section or contact me form.
Thank You for your support!!
How to list empty files? (Bash)
It works but it is a bit slow as wc -l counts the number of newline characters in each file while this is not necessary. A process that excludes files as soon as they find a single newline character would be much faster. How can one list empty files in a performant way?
3 Answers 3
Print files which match *.txt and are of size zero.
-size 0c might be more efficient, for some vanishingly small value of «more». ( find doesn’t need to first divide the file size by 512 and round the result up.)
To find all empty files in the current directory:
find . -maxdepth 1 -type f -name '*.txt' -empty
- find . This starts find looking for files in the current directory.
- -maxdepth 1 By default, find searches recursively through subdirectories. This tells it not to. If you do want a recursive search, just remove this option.
- -type f This limits the search to regular files.
- -name ‘*.txt’ This limits the search to .txt files.
- -empty This limits the search to empty files.
Why the -maxdepth stuff? The question doesn’t mention limiting it to the current directory, does it? The filtering stuff doesn’t eliminate files from sub-directories either, AFAICS.
@JonathanLeffler One of the examples in the question was wc -l *.txt which limits the search to the current directory. Removing maxdepth provides, of course, the recursive solution.
OK, fair enough — the primary example didn’t use that limit. It was a minor quibble. Using -empty instead of -size 0 is a (GNU find ) extension compared with POSIX find , though Mac OS X (BSD) find also supports it. I think they’re separate implementations — the Mac OS X version does not respond to find —version but the GNU version does.