Mastering the Find Command in Linux :
The find
command in Linux is an incredibly powerful tool for searching files and directories based on a variety of criteria. Whether you're looking for a specific file, searching by file type, or even executing commands on found files, find
can do it all. In this guide, we'll break down the essential uses of the find
command and explain each flag in simple terms.
Basic Syntax :
The basic syntax of the find
command is:
find [path] [expression]
[path]: The directory where the search begins.
[expression]: Criteria to match files and directories.
Commonly Used Flags and Options :
Searching by Name :
-name [filename]
: Search for files by their name.find /path/to/search -name "filename" # Search file by the name
-iname [filename]
: Case-insensitive search for files by their name.find /path/to/search -iname "filename" # Search file by Case-insensitive name
Searching by Type :
-type [type]
: Search for files of a specific type.f
for regular filesd
for directoriesl
for symbolic linksb
for block devices
for socket filesfind /path/to/search -type f #To find regular files find /path/to/search -type d #To find directories find /path/to/search -type l #To find symbolic links find /path/to/search -type b #To find block device find /path/to/search -type s #To find socket files
Searching by Size :
-size [size]
: Search for files by their size.Use
c
for bytes,k
for kilobytes,M
for megabytes, andG
for gigabytes.We can also use
-size [size] -size [size]
to check files between the given range .find /path/to/search -size +100M # Files larger than 100MB find /path/to/search -size -50k # Files smaller than 50KB find /path/to/search -size +1M -size -100M # Files Between 1 - 100 MB
Searching by Time :
-mtime [days]
: Search for files modified a certain number of days ago.+n
for more thann
days ago-n
for less thann
days agofind /path/to/search -mtime +10 # Modified more than 10 days ago find /path/to/search -mtime -5 # Modified within the last 5 days
-ctime [days]
: Search for files changed a certain number of days ago.
find /path/to/search -ctime +10 # Files modified in last 10 days
Searching by Permissions :
-perm [mode]
: Search for files with specific permissions.find /path/to/search -perm 644 # 644 - owner can read/write, group/others can read only. find /path/to/search -perm /u=r # Only files that have user read Permission
Searching by Owner and Group :
-user [username]
: Search for files owned by a specific user.find /path/to/search -user username # Files that owned by specific user
-group [groupname]
: Search for files belonging to a specific group.find /path/to/search -group groupname # Files that owned by specific Group
Executing Commands on Found Files :
-exec [command] {} \;
: Execute a command on each found file.find /path/to/search -name "*.log" -exec rm {} \; # Delete all .log files
Combining Multiple Criteria :
Combine multiple criteria using logical operators.
-and
or-a
: Logical AND (default)-or
or-o
: Logical OR!
: Logical NOTfind /path/to/search -type f -name "*.txt" -size +1M # Find text files larger than 1MB find /path/to/search -type f \( -name "*.jpg" -o -name "*.png" \) # Find JPG or PNG files
Conclusion :
The
find
command is a versatile and powerful tool for locating files and directories based on a wide range of criteria. By mastering the various flags and options, you can efficiently search your Linux filesystem and perform operations on the files you find. Experiment with these commands and customize them to suit your specific needs.If you have any questions or need further explanations, feel free to leave a comment below. Happy searching!
Thank You ๐โค๏ธ๐.
Subscribe to my newsletter
Read articles from Raj Kumar Behera directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Raj Kumar Behera
Raj Kumar Behera
A ๐ Passionate Linux and Cloud Computing Student . ๐ Enthusiast in DevOps and System Administration ๐งโ๐ป.