The find command in Unix is the tool you reach for when you need to locate files and directories by name, size, age, permissions, owner, or almost any other property — anywhere in the directory tree. Day to day, unlike graphical search boxes, find doesn't just return paths; it can filter with surgical precision and act on the results in the same breath, deleting, archiving, compressing, or executing commands. Once it clicks, it permanently changes how you administer a Unix or Linux system.
What the Find Command Actually Is
find is a recursive directory walker. Point it at a starting location, give it a set of tests, and it descends through every subdirectory, evaluating each file against your conditions. Everything you see printed on screen is a match, not a mere listing.
What makes it durable — it has existed since the earliest Unix releases and appears on macOS, Linux, BSD, and even most embedded systems — is that it operates on the filesystem metadata itself. It doesn't need an index, a daemon running in the background, or a package installed. If the filesystem is mounted, find can search it And that's really what it comes down to. Surprisingly effective..
Basic Syntax
The general form follows a simple grammar:
find [starting-path] [expression]
The expression is a chain of tests, actions, and operators. If you provide no action at all, find silently assumes -print.
find . -name "*.log"
That single line searches the current directory and everything beneath it for files ending in .log.
Searching by Name and Pattern
Name matching is where most people begin.
# Case-sensitive exact filename
find /etc -name "hosts"
# Glob pattern — quotes protect the asterisk from the shell
find /home -name "*.txt"
# Case-insensitive matching
find /var/log -iname "*.LOG"
# Match either of two patterns
find . \( -name "*.jpg" -o -name "*.png" \)
Notice the escaped parentheses. They group conditions so the -o (logical OR) applies only to the name tests rather than the whole expression — a detail that trips up a great many beginners But it adds up..
Filtering by File Type
The -type test narrows results to a specific kind of filesystem object:
| Flag | Object type |
|---|---|
f |
regular file |
d |
directory |
l |
symbolic link |
b |
block device |
c |
character device |
p |
named pipe |
s |
socket |
# All directories named "backup" at any depth
find /var -type d -name "backup"
# Broken symlinks in the current project
find . -type l ! -exec test -e {} \; -print
Searching by Size
Size tests accept c (bytes), k (kilobytes), M (megabytes), and G (gigabytes). A leading + means greater than, a leading - means less than.
# Files larger than 100 MB
find /home -type f -size +100M
# Files between 10 and 50 MB
find /var -type f -size +10M -size -50M
# Empty files
find . -type f -empty
# Your ten largest files, sorted
find / -type f -size +500M -exec ls -lh {} \; 2>/dev/null
This is the fastest way to answer the age-old question: what is eating my disk?
Searching by Time
Unix tracks three timestamps per file, and find exposes all of them:
-atime— last time the file was read-mtime— last time its contents changed-ctime— last time its metadata (permissions, ownership) changed
# Modified in the last 7 days
find /var/log -type f -mtime -7
# Not modified in over 90 days
find /home/user/docs -type f -mtime +90
# Accessed more than 30 days ago
find . -type f -atime +30
For finer control, -mmin, -amin, and -cmin measure in minutes Still holds up..
# Anything changed in the last 15 minutes
find /etc -type f -mmin -15
Searching by Permissions, Owner, and Depth
# World-writable files — a genuine security concern
find / -type f -perm -0002 2>/dev/null
# Files owned by a specific user
find /home -user alice -type f
# Files with no owner (often left behind after a deleted UID)
find / -nouser -o -nogroup
# Limit recursion to two levels
find . -maxdepth 2 -type d
-maxdepth and -mindepth must appear before other tests to behave predictably, because find evaluates arguments left to right.
Acting on Results
The real power emerges with action operators.
# -exec runs a command per match; {} is substituted, \; ends the command
find . -name "*.tmp" -exec rm -f {} \;
# -execdir runs from the matched file's own directory (safer, faster)
find . -type f -name "*.bak" -execdir gzip {} \;
# -ok asks for confirmation before each action
find /home -type f -name "*.old" -ok rm {} \;
# -delete removes matches directly — no subprocess at all
find . -type d -name "__pycache__" -delete
Prefer + over \; when you can. It batches filenames into fewer invocations, which matters enormously on large trees:
# One chmod call with many arguments
find . -type f -perm /111 -exec chmod a-x {} +
# One grep call
find /var/log -name "*.log" -exec grep -i "error" {} +
Handling Filenames Safely
Filenames containing spaces or newlines can shatter poorly written scripts. Combine -print0 with xargs -0:
find /music -type f -name "*.mp3" -print0 | xargs -0 -I{} mv {} /archive/
Pruning Directories
Sometimes you want to search everywhere except certain paths. -prune skips a subtree entirely:
find . -type d \( -name node_modules -o -name .git \) -prune -o -type f -print
Practical Recipes Worth Memorizing
- Clean up old logs:
find /var/log -name "*.gz" -mtime +30 -delete - Fix permissions on a web root:
find /var/www -type d -exec chmod 755 {} +thenfind /var/www -type f -exec chmod 644 {} + - Find recently changed config:
find /etc -type f -mtime -1 - Count files in a directory: `find . -type f | wc
-l
- Locate large files:
find /home -type f -size +100M -exec ls -lh {} + | sort -k5 -hr - Remove empty directories:
find . -type d -empty -delete - Snapshot file metadata for auditing:
find /etc -type f -printf "%m %u %g %p\n" > /root/etc-perms-$(date +%F).txt
Most guides skip this. Don't.
Combining Tests with Boolean Logic
find evaluates expressions left-to-right using implicit AND (-a). Explicit operators let you build precise filters:
# (name matches A OR B) AND size > 10M
find . -type f \( -name "*.iso" -o -name "*.img" \) -size +10M
# Modified recently AND NOT owned by root
find /var/www -type f -mtime -2 ! -user root
# Complex: world-writable regular files NOT in /proc or /sys
find / -type f -perm -0002 ! -path "/proc/*" ! -path "/sys/*" -ls
Parentheses \( \) must be escaped so the shell passes them to find intact. The ! operator negates the following test; -o is logical OR.
Debugging and Performance
When a command behaves unexpectedly, -D flags expose the internal decision tree:
# Show which tests pass/fail for each file
find . -name "*.log" -D tree,stat,opt
For massive filesystems, a few habits keep runtimes reasonable:
- Narrow the starting point.
find / -name foois rarely justified;find /opt/app -name foois polite. - Order tests cheap-to-expensive. Name checks (
-name) are metadata-only; content checks (-exec grep) force disk reads. Put-namefirst. - Avoid
-execin tight loops. Piping toxargs -Pparallelizes work:
find /data -type f -name "*.json" -print0 | xargs -0 -P4 -n100 jq . > /dev/null
- Consider
locate/mlocate/plocatefor name-only searches on static data; they query a prebuilt database in milliseconds.
A Note on Standards
POSIX specifies a baseline find, but GNU find (Linux), BSD find (macOS, FreeBSD), and Solaris find diverge on extensions:
| Feature | GNU | BSD/macOS |
|---|---|---|
-maxdepth |
✅ | ✅ (since ~2004) |
-printf |
✅ | ❌ (use -exec stat) |
-perm /mode (any bit) |
✅ | ❌ (use -perm +mode — deprecated in GNU) |
-delete |
✅ | ✅ |
-exec … + |
✅ | ✅ |
Scripts targeting portability should stick to POSIX primitives (-name, -type, -mtime, -exec … \;), or detect the implementation at runtime But it adds up..
Conclusion
find is the Swiss Army knife of the Unix filesystem: expressive enough to encode complex policy, efficient enough to run on petabyte-scale trees, and ubiquitous enough to appear on every POSIX system you will ever touch. So naturally, mastering its evaluation model—tests, actions, and operators as a single left-to-right expression—turns fragile one-liners into reliable automation. The next time you reach for a GUI search tool or a scripting language’s os.walk, pause and ask whether find can do the job with less code, fewer dependencies, and predictable exit codes. Usually, it can.