find Command Guide for Searching the Filesystem
This guide helps you understand and use the find command, which searches for files and directories that match criteria such as name, type, size, or modification time.
What is find, and Why Use It?
The find command walks through a directory tree and prints the paths of everything that matches the tests you give it. It is the most powerful search tool on any Unix-like system because you can combine name patterns with type, size, date, permission, and action filters.
Why Learn find?
- Locate files by rule, not name alone: “every
.conffile modified in the last 7 days” is impossible withls. - Safe deletion:
findpaired with-deleteor-exec rmremoves only what matches, which is far safer than guessing. - Scripting backbone: Almost every shell script that cleans up, backs up, or audits a system uses
find. - No database required: Unlike
locate,findreads the filesystem directly, so results are always current.
findis part of the base system on Linux and macOS. There is nothing to install.
1. Find Files by Name
find . -name "notes.txt"
-namematches the file name exactly; it is case-sensitive.
find . -iname "notes.txt"
-inameis the case-insensitive variant.
find /etc -name "*.conf"
- The pattern must be quoted, or the shell expands the
*beforefindever sees it.
Example:
find . -name "*.md"
./introduction.md
./SUMMARY.md
./cli-navigation/ls.md
2. Find Files and Directories by Type
find . -type f -name "*.sh"
-type frestricts results to regular files.
find . -type d
-type dlists directories only.
find /var/log -type l
-type lfinds symbolic links.
3. Limit How Deep find Looks
find . -maxdepth 2 -type f
-maxdepth 2stops descending after two levels — the single most useful control for large trees.
find . -mindepth 1 -maxdepth 1
-mindepth 1skips the starting directory itself, giving you a clean “list this folder” result.
4. Find by Size
find . -size +100M
+100Mmeans larger than 100 megabytes.
find . -size -1k -type f
-1kmeans smaller than 1 kilobyte.
find /var/log -size +10M -exec ls -lh {} +
- The suffixes
c,k,M, andGselect bytes, kilobytes, megabytes, and gigabytes.
5. Find by Modification Time
find . -mtime -7
-mtime -7matches files modified less than 7 days ago.
find . -mtime +30
+30matches files modified more than 30 days ago.
find . -newer reference.txt
-newercompares against another file’s timestamp instead of a fixed number of days.
6. Act on the Results
find . -name "*.tmp" -delete
-deleteremoves every match. There is no confirmation — test with a plainfindfirst.
find . -name "*.log" -exec gzip {} +
-exec ... {} +runs a command on the matches.{}is replaced by each file path.
find . -name "*.bak" -exec rm -i {} \;
-exec ... \;runs the command once per file;-iprompts before each deletion.
Always run the search without
-deleteor-execfirst, read the list, and only then add the action.
7. Combine Conditions
find . -type f -name "*.sh" -o -name "*.bash"
-omeans OR: the command prints files matching either pattern.
find . -type f -size +1M ! -name "*.zip"
!negates a test: files larger than 1 MB that are not zip archives.
find . \( -name "*.tmp" -o -name "*.bak" \) -delete
- Parentheses must be escaped so the shell passes them to
find; they group the OR conditions.
Common Pitfalls
- Forgetting to quote patterns.
find . -name *.mdlets the shell expand*.mdfirst andfinderrors or misbehaves. Always write-name "*.md". - Using
-deletewithout testing. Run the search alone first;-deletecannot be undone. findcan be slow on huge trees. Add-maxdepthor search a narrower path when scanning/or/home.-namevs-path.-namematches only the final component; use-pathwhen the directory part matters, e.g.-path "*/src/*".- Permissions errors. On system directories, prefix with
sudoor accept that some subdirectories are skipped.
Related Commands
| Command | Description |
|---|---|
locate | Fast filename search via a prebuilt index |
ls | List directory contents |
grep | Search file contents, not filenames |
tree | Draw a directory tree |
du | Report disk usage, useful with -size results |
find Cheatsheet
| Command | Description |
|---|---|
find . -name "*.txt" | Find files by name pattern |
find . -iname "*.txt" | Case-insensitive name match |
find . -type f | Regular files only |
find . -type d | Directories only |
find . -type l | Symbolic links only |
find . -maxdepth 2 | Limit recursion depth |
find . -size +100M | Files larger than 100 MB |
find . -mtime -7 | Modified in the last 7 days |
find . -mtime +30 | Modified more than 30 days ago |
find . -newer file | Modified after file |
find . -name "*.tmp" -delete | Delete matches (test first!) |
find . -exec CMD {} + | Run CMD on all matches |
find . -exec CMD {} \; | Run CMD once per match |
find . -name a -o -name b | OR two conditions |
find . ! -name "*.zip" | Negate a condition |