The find Commands You Will Use Most
find /var/www -name "config.php"
find . -iname "*.jpg"
find /var/log -type f -mtime +30
sudo find / -xdev -type f -size +500M
The pattern is always the same: where to start, then the tests. find searches the start folder and every folder below it and prints the path of each match. Put patterns with * in quotes, so the shell does not expand them before find sees them.
Search by Name
-name: matches the file name exactly, case-sensitive.-name "*.log"finds every name that ends in .log.-iname: the same, ignoring case, so"*.jpg"also findsPHOTO.JPG.-path: matches the whole path, for example-path "*/cache/*".-not: reverses a test:-not -name "*.gz".
To match one of several names, group them: find . \( -name "*.log" -o -name "*.txt" \).
Search by Type
| Test | Matches |
|---|---|
-type f | Regular files |
-type d | Directories |
-type l | Symbolic links |
-empty | Empty files and folders |
Search by Age and Size
find /var/backups -type f -mtime +30
find /var/www -type f -mmin -60
find /home -type f -size +100M
-mtime +30 means modified more than 30 days ago, and -mtime -7 less than 7 days ago. find counts whole 24-hour periods and drops the remainder, so +30 in practice means 31 days or more. -mmin works the same way in minutes. For size, +100M means larger than 100 MiB; the units are k, M, and G. Sizes are rounded up to the unit, so -size -1M matches only empty files; write -size -1024k instead.
Limit the Depth
find /var/www -maxdepth 1 -type d
-maxdepth 1 looks only at the start folder and the entries directly inside it, not deeper; with -type d, the start folder itself is listed too. Put it right after the path, before the other tests; GNU find prints a warning otherwise. -xdev keeps find on one filesystem, which matters when you search from /.
Hide Permission Errors
find / -name "nginx.conf" 2>/dev/null
As a regular user, find prints a "Permission denied" line for every folder it cannot read. 2>/dev/null discards those error lines and keeps the results. To search those folders as well, run the command with sudo.
Run a Command on Each Result
find /var/www -type f -name "*.php" -exec ls -l {} \;
find /var/www -type f -name "*.php" -exec grep -l "eval(" {} +
{} stands for the path. Ending with \; runs the command once per file; ending with + passes many paths to one command, which is much faster. -ok instead of -exec asks before each run. To pass results to other programs, see xargs.
Delete Files Safely
Deleting with find cannot be undone, and a server has no trash folder. Always run the same command with -print first and read the list:
find /var/log/app -type f -name "*.log" -mtime +30 -print
find /var/log/app -type f -name "*.log" -mtime +30 -delete
Only when the list is right, replace -print with -delete. Keep -delete at the very end: find reads the tests from left to right, so -delete placed before -name removes everything under the path. The same applies to -exec rm {} +. Do not use find to delete files in /var/lib or database folders, and have a backup or snapshot before any large cleanup, as explained in snapshot vs backup. For removing whole folders, see how to remove a directory in Linux.