Skip to content
# Files, text & logs

Finding files: find and locate

Search the filesystem by name, size, age or permissions.

find: search by name

find walks a directory tree and matches files against criteria. Search by name (case-insensitive with -iname) starting from a given path.

bash
find /var/www -iname '*.log'

find: search by size, age or type

Combine flags to narrow results: -size for size, -mtime for modification age in days, -type f/d for files vs directories.

bash
find / -type f -size +100M
find /var/log -type f -mtime +30
find /tmp -type d -empty

Act on results with -exec or xargs

-exec runs a command per match; {} is replaced by the path and \; ends the command. For many results, piping to xargs is faster since it batches arguments into fewer invocations.

bash
find /var/log -name '*.log' -mtime +30 -exec rm {} \;
find /var/log -name '*.log' -mtime +30 -print0 | xargs -0 rm

locate: instant name lookups from an index

locate searches a prebuilt database instead of walking the disk, so it's near-instant but can be stale until the index refreshes. Run updatedb manually to force a rebuild.

bash
sudo apt install mlocate   # or dnf/zypper/pacman
sudo updatedb
locate nginx.conf