- 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 Find Which Shell You Are Using on Linux
- Find out which shell you are using in Linux
- Method 1
- Method 2
- Method 3
- Method 4
- Bonus Tip: Know the version of the shell
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 Find Which Shell You Are Using on Linux
Here are four simple commands to find out which shell are you using in Linux.
Which Shell am I using in Linux? Is that even a question? Of course, it is. There are several shell available for Linux systems. Some of the most popular ones are:
You may wonder this in a few situations.
For example, if you log into a Linux system not known to you, you may wonder which shell is being used by default. If you frequently change shell in Linux, you might wonder which shell you are using at the moment.
Let me show you various Linux commands to find out which shell you are using.
Find out which shell you are using in Linux
Now there is no command that will give you this output with 100% accuracy for all the shells. But these commands should be accurate for most of the shells.
Method 1
You can use the special shell parameter $$. “$$” indicates the process id of the current instance of the shell you are running. This is a read-only parameter and cannot be modified.
If you try to see the process information, it should show you the process name i.e. the shell here.
The output should be like this:
PID TTY TIME CMD 15012 pts/0 00:00:00 zsh
Method 2
You can also use $0. $0 can be the name of the shell or the name of shell script. When it is used inside a shell script, it denotes the name of the script.
But if you use it in a shell without filename, it will show the name of the shell.
The output will simply have the name of the shell:
Method 3
You can also use the pstree command. pstree means process tree and it shows all the running processes as a tree.
If you provide it with no argument, it will show all the processes from init or systemd.
However, if you give it a process id, it will show all that processes as the root of the tree. In other words, it will show all the processes initiated by that process.
You can use the same $$ bash parameter we saw in method 1.
Method 4
The last method is using the files in proc directory. If you read the article about checking CPU info in Linux, you probably already know that this directory contains the runtime system information about your Linux system.
You can use the following command to get the shell you are using:
The output should give you the name of the shell.
Bonus Tip: Know the version of the shell
So you learned how to know which shell you are using. What about the version of the shell you are using? You can get the version of the shell simply by adding –version after the name of your running shell.
For example, if you are running zsh, you can use this:
The output will give you the version information:
zsh 5.4.2 (x86_64-ubuntu-linux-gnu)
I hope this quick tutorial helped you in finding out which shell you are running. Don’t forget to check out another simple tip on changing shell in Linux.
By the way, do you use some other way to check which shell you are using? Why not share it with us in the comment section?