Master the “find” command in Linux: A practical cheatsheet
The find
command is one of the most powerful tools in a developer’s or sysadmin’s arsenal, but many of us barely scratch the surface of what it can do. It’s not just for searching; it’s for analysis and automation.
This guide is a comprehensive, visual cheatsheet to help you master it, broken down into three parts: finding files, searching by metadata, and taking action on your results.
Part 1: Finding Files by Name & Type
Let’s start with the basics. Here are the most common ways to find files and directories by their name or extension.
Copy-paste version:
Find files by name (case-sensitive):
find /path/to/search -type f -name "myfile.txt"
Find files by name (case-insensitive):
find /path/to/search -type f -iname "myfile.txt"
Find all directories named ‘logs’:
find / -type d -name "logs"
Find files by extension (e.g., all .conf files):
find /etc -type f -name "*.conf"
Part 2: Finding by Time & Size
Now for a more advanced use case: searching by metadata. This is incredibly useful for finding old logs, large downloads, or just analyzing disk space.
Copy-paste version:
Find files modified in the last 24 hours:
find /var/log -type f -mtime -1
Find files modified MORE than 7 days ago:
find /home/user/Downloads -type f -mtime +7
Find files larger than 100MB:
find / -type f -size +100M
Find empty files or directories:
find /path/to/search -empty
Part 3: Taking Action (-exec
, -delete
)
This is where the find
command becomes a superpower. So far, we’ve only searched for files. Now, let’s learn how to perform actions on them.
Copy-paste version:
Execute a command on each found file (e.g., change permissions):
find /path/to/app -type f -name "*.sh" -exec chmod +x {} ;
Periodically delete old backup files (older than 3 days):
WARNING: The -delete
action is final. Use with caution!
find /var/backups -name "*.zip" -type f -mtime +3 -delete
Find all cache files and ask for confirmation before deleting (y/n):
find / -type f -name "*.cache" -ok rm {} ;
Downloadable version:
Source & downloads:
You can find the original PNG version of this cheatsheet in the original post: