Search & Find
Getting Started
Three different jobs, three tool families: find walks the filesystem live and can filter on any attribute, locate answers instantly from a prebuilt index, and grep searches inside files. fd, plocate, and rg are their modern, faster counterparts.Find Files by Name
find needs a starting directory; -iname matches case-insensitively. fd searches the current directory by default, ignores hidden and git-ignored files, and uses regex patterns.$ find . -iname "*report*"
$ find / -name "[fileName]" 2>/dev/null
$ fd [query]
$ fd -e pdf [query]
Searching from / prints permission errors for directories you cannot read; 2>/dev/null hides them.Find by Type, Size, and Age
Tests combine freely: a number like -7 means "less than", +7 means "more than", a bare 7 means "exactly".$ find . -type f -size +10M -size -100M
$ find . -type f -empty
$ find . -type d -empty
$ find . -mmin -60
$ find . -mtime -7
$ find . -mtime +30
| Test | Description |
|---|---|
| -type f / -type d | Files / directories |
| -size +10M | Larger than 10 MB |
| -mmin -60 | Content modified less than 60 minutes ago |
| -mtime -7 | Content modified within the last 7 days |
| -cmin / -ctime | Status (permissions, owner) changed |
| -atime | Last accessed |
| -empty | Empty file or directory |
| -user name | Owned by a user |
Act on What You Find
-exec runs a command on every match; {} is replaced by the file name. Ending with + passes many files per invocation instead of one at a time. -delete removes matches directly.$ find . -name "*.tmp" -delete
Test destructive commands first: replace -delete or -exec rm with -print, check the list, then run it for real.Indexed Search
locate finds path names instantly from an index instead of scanning the disk. The index is updated periodically by updatedb, so very new files may be missing. plocate is the faster modern implementation.$ locate [query]
$ plocate [query]
$ sudo updatedb
Search File Contents
grep -r searches directories recursively; -n shows line numbers, -i ignores case, -l lists only file names. rg (ripgrep) is recursive by default, skips git-ignored files, and is much faster on big trees.$ grep "[query]" [file]
$ grep -rni "[query]" [path]
$ grep -rl "[query]" [path]
$ rg "[query]"
$ rg -t py "[query]"
$ ag "[query]"
Search the output of any command by piping it through grep.Fuzzy Finding
fzf filters any list interactively as you type. On its own it fuzzy-finds files; combined with other commands it becomes a universal picker.$ fzf
fzf's shell integration binds Ctrl+R (history), Ctrl+T (files), and Alt+C (cd into directory).