Tools › Cheat sheets › Linux & Shell › find
🔍

find cheat sheet

find walks a directory tree and tests each file. Syntax: find WHERE TESTS ACTIONS. Put -maxdepth early, and quote patterns so the shell does not expand them.

Click any command to copy it.

By name and type

CommandWhat it does
find . -name "*.log"Files whose name matches (case-sensitive).
find . -iname "*.jpg"Case-insensitive name match.
find . -type dDirectories only.
find . -type fRegular files only.
find . -maxdepth 2 -name '*.conf'Limit how deep it searches.
find / -name "nginx.conf" 2>/dev/nullSearch the whole system and hide permission errors.
find . -emptyEmpty files and directories.

By size, time and permissions

CommandWhat it does
find . -size +100MFiles larger than 100 MB.
find . -size 0Zero-byte files.
find . -mtime -7Modified in the last 7 days.
find . -mtime +30Modified more than 30 days ago.
find . -mmin -60Modified in the last 60 minutes.
find . -newer reference.txtNewer than another file.
find . -perm 777Files with exactly these permissions.
find . -user aliceFiles owned by a user.
find . -xtype lBroken symbolic links.

Combine and exclude

CommandWhat it does
find . \( -name '*.jpg' -o -name '*.png' \)Match either pattern.
find . -not -path '*/node_modules/*' -name '*.js'Exclude a directory.
find . -name '*.bak' -not -newer ref.txtCombine conditions with -not.

Do something with the results

CommandWhat it does
find . -name '*.log' -exec ls -lh {} +Run a command on matches (batched).
find . -name '*.tmp' -deleteDestructive: Delete all matching files. Run without -delete first to preview.
find . -type f -exec chmod 644 {} +Set permissions on every file.
find . -type d -exec chmod 755 {} +Set permissions on every directory.
find . -name '*.txt' -print0 | xargs -0 grep -l 'error'Safely pipe file names with spaces to another command.
find . -type f -exec du -h {} + | sort -rh | head -20The 20 largest files.

Frequently asked questions

Why does find say 'paths must precede expression'?

A shell expanded your wildcard. Quote the pattern: -name '*.txt'.

How do I safely delete files found by find?

Run the same command without -delete first to see the list, then add -delete (put it last).

More Linux & Shell cheat sheets

🐚 Bash🔎 grep✂️ sed🧮 awk📦 tar📝 Vim🪟 tmux🔐 chmod permissionsAll Linux & Shell cheat sheets →
Copied to clipboard