Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Viewing and Searching Files

Linux find command

Reference for the Linux find command: search directory trees by name, type, size, time and permissions, combine tests, and run actions safely with -exec.

find walks one or more directory trees and tests every entry against an expression you write. Entries that satisfy the expression are acted on: by default they are printed, but you can delete them, run a command on them or format the output yourself. Unlike locate, find reads the real filesystem, so its answers are always current.

The expression is built from tests (name, type, size, age, owner, permissions), operators (and, or, not, parentheses) and actions (-print, -exec, -delete).

Terminal
find [-H|-L|-P] [START...] [EXPRESSION]

Try it

Bash Names, types and sizes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Actions: -exec, -printf, -delete

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
find [START...] [TESTS] [ACTIONS]
Test or action Description
-name PATTERN Base name matches the glob (case-sensitive; -iname ignores case).
-path PATTERN Whole path matches the glob.
-regex RE Whole path matches a regular expression.
-type X f file, d directory, l symlink, p pipe, s socket.
-size N[ckMG] File size; +N larger, -N smaller. Sizes round up to the unit.
-mtime N, -atime N, -ctime N Age in 24-hour units; +N older than, -N newer than.
-mmin N, -newer FILE Minutes; newer than a reference file.
-perm MODE Exact mode, -MODE all bits set, /MODE any bit set.
-user NAME, -group NAME, -empty Owner, group, empty file or directory.
-maxdepth N, -mindepth N Limit how deep to descend.
-prune Do not descend into a matching directory.
-print, -print0, -printf FMT Output, NUL-separated output, or formatted output.
-exec CMD {} \; / -exec CMD {} + Run CMD once per file, or once per batch.
-delete Remove matches.
! TEST, -a, -o, ( ... ) Not, and, or, grouping (quote the parentheses).

Returns: 0 if every start path was processed without error; non-zero otherwise (for example when a directory was unreadable). Use -exec ... {} + or check the command's own status when it matters.

Usage notes

  • Quote patterns. find . -name *.c lets the shell expand *.c first; write '*.c'.
  • Order matters for actions and -prune. -delete placed before the tests deletes everything. Put -delete last, and try with -print first.
  • Implicit -print. If you give no action, the whole expression is wrapped with -print. With an explicit action such as -prune -o ... -print, remember to print.
  • Time arithmetic is in whole days. -mtime +1 means older than 2 days, because fractions are discarded. Use -mmin for finer control.
  • Spaces in names: -print0 | xargs -0 or -exec ... {} + are safe; plain xargs is not.
  • -exec ... + versus ;: + is much faster (fewer processes). Use ; when the command takes only one file at a time.
  • Symlinks: by default (-P) find does not follow them; -L does, which may loop on cyclic links.
  • Permission denied messages go to standard error; add 2>/dev/null to silence them.
  • Performance: find touches the disk; put the cheap tests (-name, -type) before costly ones and use -maxdepth when you can.