Home·Terminal commands

find

Files and directories

Usage

find [path] -name '[pattern]' -type f

Walks a directory tree testing every entry; quote the pattern as -name '*.log' or the shell expands it before find ever sees it.

Common flags

FlagMeaning
-nameMatch the file name, case sensitive; -iname ignores case
-typeRestrict to f (file), d (directory) or l (symlink)
-sizeFilter by size, as in -size +100M
-mtimeFilter by age in days, as in -mtime +7
-maxdepthLimit how deep to walk; GNU wants it before the tests
-deleteDelete every match, no confirmation
-execRun a command on each hit, ending with \; or +
-print0Separate results with NUL, to pair with xargs -0

Examples

find . -name '*.log' -mtime +7 -delete

Deletes log files older than a week.

find / -type f -size +1G 2>/dev/null

Finds files over a gigabyte, hiding permission errors.

find . -name '*.jpg' -exec mv {} photos/ +

Moves every JPEG it finds.

Moving, deleting and finding. Some of these do not ask twice, which is why -i becomes a habit.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does find do?

Walks a directory tree testing every entry; quote the pattern as -name '*.log' or the shell expands it before find ever sees it.

Q. How do I type it?

find [path] -name '[pattern]' -type f — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

8 are listed here; the full set is in man find. This command sits under Files and directories.

Related commands

man page: man find