# find — the complete field guide
`find` walks a directory tree and tests every entry against your filters, then
acts on the matches. macOS ships **BSD find** (notes below where it differs from
GNU/Linux). Call this card with **`cheat find`** or **`findcheat`**.
> Anatomy: `find [WHERE] [WHAT to match] [WHAT to do]`
> Example: `find ~/Downloads -iname '*toybox*.zip' -print`
> └ path ┘ └ test ┘ └ action ┘
>
> **Rule 1 — path comes first.** `find toybox` fails; `find . -name toybox` works.
> **Rule 2 — quote wildcards.** `'*toybox*'` — unquoted, the shell eats `*` first.
---
## 1. WHERE — paths to search
```sh
find . # current dir, recursive (the default action prints all)
find ~/Downloads # one specific tree
find ~ /tmp /opt # several trees at once
find / # the WHOLE system (add 2>/dev/null, see §13)
find -L ~/Links # -L = follow symlinks while walking
```
## 2. BY NAME
```sh
find . -name 'toybox*' # case-SENSITIVE glob on the file name
find . -iname '*toybox*' # -i = case-INSENSITIVE (Toybox, TOYBOX, …)
find . -name '*.zip' # by extension
find . -not -name '*.log' # everything EXCEPT .log
find . -path '*/cache/*' # match against the whole path, not just the name
find . -ipath '*/Cache/*' # case-insensitive path match
find . -regex '.*/toy.*\.zip' # regex over the FULL path (BSD: add -E for ERE)
```
Globs in `-name`: `*` any run, `?` one char, `[abc]` a set. They are matched by
find, not the shell — that's why you quote them.
## 3. BY TYPE
```sh
find . -type f # regular files
find . -type d # directories
find . -type l # symlinks
find . -type f -name '*.sh' # combine: shell scripts only
```
Other types: `b` block dev · `c` char dev · `p` pipe · `s` socket.
## 4. BY SIZE
```sh
find . -size +100M # bigger than 100 MB
find . -size -10k # smaller than 10 KB
find . -size +1G -type f # files over 1 GB
find . -empty # zero-byte files AND empty dirs
```
Units: `c`=bytes `k`=KB `M`=MB `G`=GB. `+` = greater than, `-` = less than, none = exact.
## 5. BY TIME
```sh
find . -mtime -7 # modified in the last 7 DAYS (- = "less than ago")
find . -mtime +30 # modified MORE than 30 days ago
find . -mmin -60 # modified in the last 60 MINUTES
find . -atime -1 # accessed in the last day
find . -newer ref.txt # changed more recently than ref.txt
find . -newermt '2026-01-01' # modified after a date (BSD + GNU)
find . -newermt '2 hours ago' # relative (GNU; BSD wants a real date)
```
`m`=modified · `a`=accessed · `c`=metadata changed. Day tests count 24h blocks.
## 6. BY OWNER & PERMISSIONS
```sh
find . -user daemon1 # owned by a user
find . -group staff # owned by a group
find . -perm -u+x # executable by owner (-perm - = "at least these bits")
find / -perm -4000 -type f 2>/dev/null # SUID binaries (privesc hunting)
find / -perm -2000 -type f 2>/dev/null # SGID binaries
find . -perm 644 # EXACTLY rw-r--r--
find . -writable # GNU only (BSD: use -perm tests)
```
## 7. DEPTH — how deep to dig
```sh
find . -maxdepth 1 -type f # only THIS dir, no recursion
find . -maxdepth 2 # this dir + one level down
find . -mindepth 1 # exclude the starting dir itself
```
Put `-maxdepth` first — it's a global option.
## 8. LOGIC — combine filters
```sh
find . -name '*.jpg' -o -name '*.png' # -o = OR
find . -type f -name '*.tmp' # adjacent tests = AND (implicit)
find . -not -name '*.md' # NOT (or use ! -name)
find . \( -name '*.c' -o -name '*.h' \) -type f # group with \( \) — quote them
find . -name node_modules -prune -o -type f -print # -prune = don't descend into a dir
```
`-prune` is how you SKIP a folder (faster, less noise). Read it as: "if node_modules,
stop; otherwise, if a file, print."
## 9. ACTIONS — do something with matches
```sh
find . -name '*.log' -print # print path (the default)
find . -name '*.log' -print0 # NUL-separated — safe for weird names
find . -name '*.log' -ls # ls -l style detail
find . -name '*.tmp' -delete # DELETE matches (see warning §14)
find . -name '*.txt' -exec wc -l {} \; # run cmd per file; {} = the file
find . -name '*.txt' -exec wc -l {} + # batch into ONE cmd (faster)
find . -name '*.bak' -ok rm {} \; # -ok = -exec but asks y/n each time
```
`\;` runs the command once per file. `+` packs many files into one invocation.
## 10. PIPE INTO OTHER TOOLS
```sh
find . -type f -print0 | xargs -0 grep -l 'TODO' # files CONTAINING text (safe)
find . -type f | wc -l # count files
find . -name '*.jpg' -print0 | xargs -0 du -ch | tail -1 # total size of all jpgs
```
Always pair `-print0` with `xargs -0` — survives spaces/newlines in names.
---
## 11. REAL-WORLD RECIPES — "find anything"
```sh
# the zip you're hunting
find ~/Downloads -iname '*toybox*' -type f
# 20 biggest files under home
find ~ -type f -print0 2>/dev/null | xargs -0 du -h | sort -rh | head -20
# things downloaded today
find ~/Downloads -type f -mtime -1
# empty files and empty dirs (clutter)
find . -empty
# broken symlinks
find . -type l ! -exec test -e {} \; -print
# every PDF, then open the first
find ~ -iname '*.pdf' 2>/dev/null | head
# files containing a string (name unknown)
find . -type f -print0 | xargs -0 grep -rln 'API_KEY'
# delete .DS_Store everywhere (dry-run first: swap -delete for -print)
find . -name .DS_Store -print
find . -name .DS_Store -delete
# old junk: logs not touched in 90+ days
find ~/Library/Logs -type f -mtime +90
```
## 12. BY "FILE TYPE" (content, not extension)
`find` matches NAMES, not real content. For true type detection pipe to `file`:
```sh
find . -type f -exec file {} + # show detected type of every file
find . -type f -exec file --mime-type {} + | grep 'image/' # real images
```
## 13. PERMISSION NOISE & whole-system scans
Searching `/` or `~/Library` spews `Permission denied`. Silence it:
```sh
find / -iname '*.kdbx' 2>/dev/null # 2>/dev/null drops stderr
sudo find / -iname '*.kdbx' # or run with sudo to actually read them
```
## 14. ⚠ SAFETY
- `-delete` and `-exec rm` are **irreversible**. **Always run the query with
`-print` first**, eyeball the list, *then* swap in `-delete`.
- `-delete` implies depth-first; it won't remove non-empty dirs unless they end
up empty. Test on a copy when unsure.
---
## 15. macOS (BSD) vs GNU/Linux gotchas
- **No `-printf`** on BSD find. Use `-print`, `-ls`, or `-exec`.
- `-regex` is **basic** regex on BSD; add **`-E`** *before* the path for extended:
`find -E . -regex '.*\.(jpg|png)'`.
- `-newermt 'relative'` (e.g. "2 hours ago") is GNU-friendly; BSD prefers a real
timestamp like `'2026-06-01 12:00:00'`.
- Want GNU find on mac? `brew install findutils` → it's **`gfind`** (and `gxargs`).
## 16. The modern shortcut — `fd`
Same jobs, friendlier defaults (smart-case, skips .git, no quoting, colored):
```sh
fd toybox ~/Downloads # ≈ find ~/Downloads -iname '*toybox*'
fd -e zip # ≈ find . -name '*.zip'
fd -t d cache # ≈ find . -type d -name '*cache*'
fd -H -I secret # include Hidden + Ignored (≈ no skip rules)
fd -tf -x du -h {} # -x = -exec ; -X = -exec +
fd --changed-within 1d # ≈ find . -mtime -1
```
`findhelp` = quick card · `cheat find` / `findcheat` = this full guide · `ff` = fzf picker.