The find command helps you to search for a specific string of characters using Linux command-line. It is a practical tool which is recommended for Prod Environment. You can use it to search a specific location, add options to control the behavior of the search, and use additional commands to tell it how to manage the data it finds. In this tutorial, you will learn top 20 Linux find command practical Examples.
Basic find Command Syntax
Find command basic syntax as follows:
find <location> <options> <file or directory>
- Options – This attribute is not a mandatory element of the find command.
- Location – Instructs find where to start looking for a string.
- File or Directory Name – Represents the string of text (or other data) you are searching for.
Top 20 find Command Practical Usage
We can use find command in various scenarios. Let’s look on these.
1. Lists all the files in current directory and its sub-directories
$ find
2. Find all the files or directories of current working directory
$ find . -type f
Only directories can be listed in your present working directory with the following command.
$ find . -type d
3. Find a file with name in a particular directory
A file can be looked by its name in a particular directory with the following command.
$ find /home/ravi -name "thecodecloud.txt"
find command will look for “thecodecloud.txt” file in /home/ravi directory. You can also look for all the files with .txt extension,
$ find /home/ravi -name "*.txt"
Here, we used /home/ravi directory but you can use any directory name in present in your machine.
You can search a particular directory on file in multiple directories with the following command.
$ find /root /etc /var -name "TheCodeCloud.txt"
4. Find a file with name ignoring case
You can find a file with its name irrespective of the case i.e. whether its lower case or upper case . Only you have to use ‘-iname‘ option with the find command
$ find /home/ravi -iname "Thecodecloud.txt"
The result of the command will provide all the files that are named Thecodecloud.txt, whether its in lower case or upper case.
5. Find all file types except Mentioned type
Sometime we need to find all the files except a mentioned type. You can use -not option with find command to search those files.
$ find /home/ravi -not -name "*.txt"
In above example, command will find all files except text files.
6. Find the hidden files/directories
You can search for the hidden files in a directory with the following command,
$ find ~ -type f -name ".*"
Hidden directories can also be searched using below command.
$ find ~ -type d -name ".*"
7. Find all the empty files or directories
You can search empty files and directories in your machine with the following command,
$ find / -empty -type f $ find / -empty -type d
8. Find files with multiple conditions
You can also combine more than one condition to search files. Sometimes we need to search files of multiple extensions such as ‘.txt’ and ‘.html’. Use the following command to search these files,
$ find / -regex ".*\.\(txt\|html\)$"
9. Find all the files with SGID, SUID and executable files
You can locate all the files with SGID bits with the following command,
$ find / -perm /g=s
You can locate all the files with SUID bits with the following command,
$ find / -perm /u=s
You can locate all the files that are executables with the following command,
$ find / -perm /a=x
10. Find files with OR condition
You can also combine multiple search criteria & then locate the files based on the satisfy of any of the given one condition using OR operator,
$ find / -name "*.txt" -o -name "Thecodecloud*"
11. Find all the read-only files
You can also look for only read-only files using find command,
$ find / -perm /u=r
12. Find all the files owned by a user
The files that are owned by a particular user can be located with the following command,
$ find . -type f -user ravi
Here, we are looking for files which are owned by ravi user.
13. Find all the files owned by a group
You can locate all the files that are owned by a particular group with the below given command,
$ find . -type f -group sysgrp
Here, we are looking for files which are owned by sysgrp group.
14. Find files of particular size
If you want want to search a file or directory of exact particular size, then we can use ‘-size‘ option with find command to search for the file,
$ find / -type f -size 10M $ find / -type d -size 10M
Here, command will search for exact 10M size files/directories.
If you want want to search a file or directory of less than the exact particular size.
$ find / -type f -size -10M $ find / -type d -size -10M
Here, command will search for less than 10M size files/directories.
If you want want to search a file or directory of greater than the exact particular size.
$ find / -size +10M
Here, command will search for greater than 10M size files/directories.
If you are looking for a file for which we don’t know the actual size but know a range of size or just want to locate all the files within a size range, then you can also locate the file using that criteria.
$ find / -size +2M -size -5M
Here, you will get files/directories between 2M and 5M size range.
15. Find N days ago, modified files/directories
For instance, you want to locate all the files that have been modified 10 days ago. You can do it using ‘-mtime‘ option with the find command,
$ find / -mtime 10
Here, we found 10 days modified files/directories but you can use this as per your requirement.
16. Find N days ago, accessed files/directories
For instance, you want to locate all the files that have been accessed 10 days ago. You can do it using ‘-atime‘ option with the find command,
$ find / -atime 10
Here, we found 10 days accessed files/directories but you can use this as per your requirement.
17. Find smallest and largest files/directories
You can locate largest or smallest file with find command additionally ‘sort‘ command will be used. if you further want to list top three largest files, you need to combine ‘head‘ command.
To locate top three files in the current directory, command is
$ find . -type f -exec ls -s {} \; | sort -n -r | head -3
You can similarly find the smallest files in your machine,
$ find / -type f -exec ls -s {} \; | sort -n | head -3
You can get smallest and largest directories as well using ‘-type d’ with find command.
$ find . -type d -exec ls -s {} \; | sort -n -r | head -5
Here, you will get top 5 consuming directories in your current directory.
18. Find all the files/directories matching a criteria & delete them
Sometimes, you required to locate & delete files matching a criteria. To do this with the following find command,
$ find / -type f -name 'thecodecloud.*' -exec rm -f {} \;
Here, you will get deleted “thecodecloud” file from your entire filesystem.
Similarly, directories can be located and deleted just like files.
$ find / -type d -name 'thecodecloud.*' -exec rm -f {} \;
These were some simple examples demonstrating the functionality of find command & it can be used to perform tedious, repetitive search/locate task more easy.
19. Find all the files/directories with specific permissions & change permissions
You can look for files based on their permissions. Using -perm option with find command
$ find / -type f -perm 0777
Directories can also be searched by permission with find command.
$ find / -type d -perm 0777
With find command, you can also achieve some advanced functionalities. For instance, you can list all the files that have permission 544 and then change those permissions to 777. To achieve this execute following command,
$ find / -type f -perm 644 -print -exec chmod 777 {} \;
Similarly, you can locate and change the permission of a particular directory in your root file system.
$ find / -type d -perm 775 -print -exec chmod 777 {} \;
Additionally, you can use other options with above commands and get desired results.
20. Find all the files/directories & copy them into other directory
With find command, you can also achieve some advanced functionalities. For instance, you can list all the files that have and then copy those files to other directory. To achieve this execute following command,
$ find / -name "*.txt" -exec cp -a { } /NewDir \;
Here, we are locating and copying text files from root filesystem into NewDir.
Conclusion
Hence, in this guide you learnt top 20 Linux Find Command Practical Examples. For more tutorials, please stay tuned.
Read Also : How to Generate a SELF-SIGNED SSL Certificate with Openssl in Linux
2 thoughts on “Top 20 Linux Find Command Practical Examples”