Linux find command with example
The find command is used for locating files and directories based on various criteria such as name, type, size, modification time, permissions, and more.
Below is an example of how to use the find command:
for example, you want to find all the text files in your home directory and its subdirectories.
find /home/yourusername -type f -name "*.txt"
Explanation:
find
is the command itself./home/yourusername
is the directory from where the search will begin.-type f
specifies that only regular files should be searched, not directories or other special files.-name "*.txt"
specifies that only files ending with.txt
should be returned.
Another example could be finding all files in the current directory modified in the last 24 hours:
find . -type f -mtime -1
Explanation:
.
is the starting directory, which in this case is the current directory.-type f
specifies that only regular files should be searched, not directories or other special files.-mtime -1
specifies that only files modified within the last 24 hours should be returned. The-1
argument means less than one day old.
Here are some commonly used options for the find
command with examples:
-name
: Search for files with a specific name or pattern.
find /home -name myfile.txt
-type
: Search for files of a specific type.
find /home -type f -name "*.txt" # search for text files
find /home -type d -name "docs" # search for directories named "docs"
-mtime
: Search for files modified within a specific time frame.
bashCopy codefind /home -type f -mtime -7 # search for files modified within the last 7 days
find /home -type f -mtime +7 # search for files modified more than 7 days ago
-size
: Search for files of a specific size.
find /home -type f -size +10M # search for files larger than 10 MB
find /home -type f -size -1G # search for files smaller than 1 GB
-user
: Search for files owned by a specific user.
find /home -type f -user john # search for files owned by user "john"
-group
: Search for files owned by a specific group.
find /home -type f -group users # search for files owned by the group "users"
-perm
: Search for files with specific permissions.
find /home -type f -perm 644 # search for files with permissions set to 644
-exec
: Execute a command on the search results.
find /home -type f -name "*.txt" -exec rm {} \; # delete all text files
-print
: Print the search results.
find /home -type f -name "*.txt" -print # list all text files
-inum
: Search for files with inode number 'N'.
find /home -type f -inum 45578825 #search file using inode number
-links
: Search for files with 'N' links.
find /home -type f -links 2 #search for file with 2 links
-empty
: Search for empty files and directories.
find /home -empty #search for empty files and directories
-mindepth
: Themindepth
option in thefind
command specifies the minimum depth at which to start the search. By default,find
starts the search at the current directory, but withmindepth
, you can specify a higher level directory to start the search. Here's an example of how to use themindepth
option:Suppose you want to search for all text files in the
/home
directory and its subdirectories, but you want to start the search from a higher level directory, such as the root directory. The command would be:
find / -mindepth 2 -type f -name "*.txt"
maxdepth
: Themaxdepth
option in thefind
command specifies the maximum depth at which to search for files. By default,find
searches for files in all subdirectories, but withmaxdepth
, you can limit the depth of the search to a certain level.Here's an example of how to use the
maxdepth
option:Suppose you want to search for all text files in the
/home
directory and its immediate subdirectories only. The command would be:find /home -maxdepth 2 -type f -name "*.txt"
We can refer to the man
page for find
for a complete list of options and their descriptions.
These are just a few examples of how to use the find
command in Linux. If you have further questons then you can reach out to us on whatsapp numbers: 7838238895/8909068089/8920228066