find Command on Linux: Search by Name, Size, Age and Run Actions

find command linux - custom-find-featured.png

Graphical search is not an option on most servers. find walks the directory tree you specify and tests each file against rules you define
— name, type, how old it is, how large it is, and more.

Pair find with disk cleanup in our df/du/ncdu guide and archive old logs with
tar once you know what to remove.

What find Actually Does

Starting at a path, find visits entries depth-first (by default), evaluates predicates, and optionally runs actions on matches.

architecturefind-walk.txt
  find /var --> test each entry --> match? --> print or -exec action
find predicates
name, type, time

Find by Filename

Globs must be quoted so the shell does not expand them before find sees them. -iname is case-insensitive.

bashby name
find /var/log -name '*.log'
find /home -iname 'readme*'
find -name
quote globs

Files vs Directories

Use -type f or -type d to avoid surprises. -maxdepth stops find from diving deeper than you intend.

bashtyped search
find /etc -maxdepth 2 -type f -name '*.conf'
type and depth
-type f, -maxdepth

Find by Modification Time

-mtime is in 24-hour days. -mtime -7 means modified within the last week; +30 means older than 30 days.

bashage
find /var/log -type f -mtime +30 -name '*.log'
find /tmp -type f -mtime -1
mtime
recent vs old

Find by Size

Hunt for huge files when a filesystem is full. Combine -size with -type f.

bashlarge files
find / -xdev -type f -size +500M 2>/dev/null | head
size
+100M etc.

Run Commands on Matches (Carefully)

-delete only removes empty directories safely in bulk; for files, prefer explicit -exec rm after a dry run with -print first.

bashsafe pattern
find /var/tmp -type f -name '*.tmp' -mtime +7 -print
# after review:
find /var/tmp -type f -name '*.tmp' -mtime +7 -delete
WarningNever pipe find straight to rm without checking the list on production.
exec and delete
print before rm

Skip Noisy Directories

Prune node_modules, .git, or backup mounts so find finishes in reasonable time.

bashskip .git
find ~/project \( -path '*/.git' -prune \) -o -type f -name '*.py' -print
prune
-path -prune -o

Quick Reference

  • Quote globs: find . -name '*.log'
  • -mtime -7 recent; +30 older than 30 days
  • -print before any -delete or rm

Related tutorials

Diagrams are original illustrations by Gnome IT Solutions. Tutorial text © Gnome IT Solutions.