Mastering the Linux find Command: A Beginner's Guide
find
The general structure of the find
command is:
find [path] [options] [expression]
path
: The directory where the search starts (defaults to current directory .
if omitted).options
: Modify search behavior (e.g., depth, follow symlinks).expression
: Defines what to search for (name, size, type, etc.).The most common use of find
is locating files by their name.
example.txt
in the current directory:find . -name "example.txt"
-name
is case-sensitive. Use -iname
for case-insensitive search..jpg
files in /home/user
:find /home/user -name "*.jpg"
You can filter results by file type using -type
:
f
→ Regular filed
→ Directoryl
→ Symbolic link/var
:find /var -type d
/usr/bin
:find /usr/bin -type l
Search for files based on their size with -size
:
+100M
→ Larger than 100MB-10k
→ Smaller than 10KB1024c
→ Exactly 1024 bytes/home
:find /home -size +50M
find . -type f -size 0
You can locate files modified within a certain timeframe:
-mtime n
→ Modified exactly n
days ago-mtime +n
→ Modified more than n
days ago-mtime -n
→ Modified less than n
days agofind /var/log -mtime -7
find /backups -mtime +30
You can combine multiple conditions using:
-and
(default, can be omitted)-or
-not
.conf
files modified in the last 3 days:find /etc -name "*.conf" -mtime -3
.txt
files:find . -not -name "*.txt"
You can perform actions on found files using -exec
:
.tmp
files older than 30 days:find /tmp -name "*.tmp" -mtime +30 -exec rm {} \;
{}
is a placeholder for the found file.\;
terminates the command..sh
files to executable:find ~/scripts -name "*.sh" -exec chmod +x {} \;
find
with xargs
For better performance with large searches, pipe results to xargs
:
.log
files:find /var/log -name "*.log" | xargs gzip
The find
command is an essential tool for Linux users, offering powerful file-searching capabilities. By mastering its options, you can quickly locate and manage files efficiently.
| Command | Description |
|---------|-------------|
| find . -name "file"
| Find by name |
| find /dir -type d
| Find directories |
| find ~ -size +100M
| Find large files |
| find /etc -mtime -7
| Find recent files |
| find . -exec rm {} \;
| Delete found files |
Now that you understand the basics, experiment with find
to streamline your Linux workflow!
What’s your favorite find
trick? Let us know in the comments!