The Linux find command is one of the most powerful and essential tools in a developer's or system administrator's toolkit. It's much more than a simple file search; it's a program that lets you locate files and directories based on a vast array of conditions and then perform actions on them.
While a basic search is useful, find's real power lies in its ability to chain criteria and execute commands. Let's break it down from the fundamentals to advanced, practical examples.
Basic Syntax of find
The basic structure of the command is:
find [path...] [expression][path...]: The directory (or directories) where you want to start the search. Use . for the current directory or / for the entire filesystem.
[expression]: This is the heart of the command. It defines what you're looking for (tests like -name, -type) and what to do with the results (actions like -print, -exec).
1. Finding Files by Name
This is the most common use case. The -name test is case-sensitive, while -iname is case-insensitive.
Find a file named config.php in the current directory and its subdirectories:
find . -name "config.php"Find all .log files starting with system (using wildcards):
find /var/log -name "system*.log"Find files named README regardless of case (e.g., readme, ReadMe):
find /home/projects -iname "readme"2. Finding Files by Type
Use the -type test to filter by file type. This is incredibly useful for separating files from directories.
f : regular file
d : directory
l : symbolic link
Find all directories named node_modules (handy for cleaning up disk space):
find . -type d -name "node_modules"List all symbolic links in the /usr/bin directory:
find /usr/bin -type l3. Finding Files by Size
Quickly locate files that are consuming disk space. The -size test uses suffixes:
c : bytes
k : Kilobytes
M : Megabytes
G : Gigabytes
+ : greater than
- : less than
Find files larger than 100 Megabytes in the /home directory:
find /home -size +100MFind files smaller than 1 Kilobyte (e.g., empty log files or placeholders):
find /tmp -size -1k4. Finding Files by Modification/Access Time
This is crucial for debugging and log rotation. Time is measured in days.
-mtime n : File's data was last modified *n* days ago.
-atime n : File was last accessed *n* days ago.
Find files modified within the last 7 days (useful for recent changes):
find /var/www/html -mtime -7Find log files not accessed in the last 30 days (archiving candidates):
find /var/log -name "*.log" -atime +30Level Up: Combining Conditions and Taking Action
The real magic happens when you combine tests and use the -exec action to do something with the results.
Combining Tests (AND/OR)
AND (implied): Just place conditions next to each other.
Find PDF files modified in the last 10 days:
find . -name "*.pdf" -mtime -10OR (-o): Use the -o flag.
Find files that are either .js or .jsx extensions:
find src -name "*.js" -o -name "*.jsx"The Powerful -exec Flag
This allows you to run any command on each file found. The magic string {} is a placeholder for the found file, and the command must be terminated by \;.
Find all .tmp files and delete them (always test with -ls first!):
# First, see what you'll delete
find . -name "*.tmp" -ls
# Then, execute the delete
find . -name "*.tmp" -exec rm {} \;Find all .conf files and search for the string "localhost" inside them:
find /etc -name "*.conf" -exec grep -l "localhost" {} \;A faster alternative to -exec is +, which passes multiple files to the command at once, which is more efficient for commands like chmod or chown:
# Change permissions of all .sh files in one go
find . -name "*.sh" -exec chmod 755 {} +Pro-Tip: Use -delete for Simpler Deletion
Instead of -exec rm {} \;, you can use the built-in -delete action. Important: Use it with caution and place it at the end of your expression.
find . -name "*.tmp" -deleteSummary: Your find Command Cheat Sheet
Task Command
Find file by name find /path -name "filename"
Find file (case-insensitive) find /path -iname "filename"
Find directories find /path -type d -name "dirname"
Find files larger than 50MB find /path -size +50M
Find files modified last 24h find /path -mtime -1
Find & delete files find /path -name "*.log" -delete
Find & list details find /path -name "*.conf" -ls
Find & change permissions find /path -name "*.sh" -exec chmod 755 {} +The find command is a perfect example of a tool that seems simple but offers incredible depth. Mastering it will save you hours of manual searching and file management. What's your favorite find command trick? Share it in the comments below