Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 95 ยท Working with Files

Finding Files: find, locate and which

Master the Linux find command: search by name, type, size, time and permissions, run commands with -exec, use -print0 with xargs, prune directories, and know when to use locate.

A Linux system has hundreds of thousands of files. Sooner or later you need to answer questions such as "where is that config file?", "which logs are bigger than 100 MB?", "what changed in the last hour?" or "delete every .tmp file below this directory". The tool for all of this is find. It walks a directory tree, tests every entry against the conditions you give it, and then does something with the entries that pass: print them, delete them or run a command on them.

find is more of a tiny programming language than a normal command, and its syntax looks odd at first: there are no -i-style flags for most things, but expressions made of tests and actions. Once you understand the model (walk, test, act) it becomes one of the most useful tools you own. This lesson also covers locate (fast, index-based search) and which/type (finding commands). It follows Wildcards and Globbing, whose patterns you will reuse, and leads into links.

The anatomy of find

TEXT
find  [where to look]  [tests and actions...]
       ^ one or more     ^ evaluated left to right for every file found
         directories

find visits every file and directory below each starting point, recursively. For each entry it evaluates the expression from left to right. The tests (-name, -type, ...) are true or false; the default action when you give none is -print. Tests are joined by an implicit AND.

Bash find basics: name, type, depth

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.

Four things to notice:

  1. find does not guarantee any order. The directory order depends on the filesystem, so we pipe to sort here. In real life, do not rely on it.
  2. Quote the pattern. -name '*.c' must reach find unexpanded: find does its own matching. If you write -name *.c and the current directory contains a .c file, the shell expands the star first and find receives garbage.
  3. -name matches only the last component (the base name), never the directory part. Use -path 'src/*/core.c' to match the whole path.
  4. -maxdepth and -mindepth limit how deep to go. GNU find wants them before other tests (it warns otherwise).

Tests you will use every day

Test Meaning
-name PATTERN, -iname Base name matches the glob (-iname ignores case)
-type f / d / l Regular file, directory, symbolic link
-size +1M, -size -10k, -size 0 Size greater than, less than, exactly. Units: c bytes, k, M, G
-mtime -1, +7 Modified less than 1 day ago, more than 7 days ago
-mmin -30 Modified less than 30 minutes ago
-newer FILE Modified more recently than FILE
-perm 644, -perm -u+x Exact mode, or has at least these bits
-user NAME, -group NAME Owner / group
-empty Empty file or empty directory
-not, !, -o, ( ) NOT, OR and grouping

Sizes round up to whole units, a classic surprise. In the example, tiny.txt (4 bytes) counts as 1 KiB, so -size -2k ("less than 2 units") lists it together with run.sh (18 bytes) and the three zero-byte files, and medium.bin (3,000 bytes) rounds up to 3 KiB and matches -size 3k. -size 1M matches files from 1 byte up to 1 MiB. Times work in full 24-hour periods: -mtime +7 means "modified 8 or more days ago". For exact comparisons use -newermt, which takes a date, or -newer with a reference file.

Bash find with size, time, permissions and logic

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.

Parentheses must be escaped (\( ... \)) or quoted so the shell does not treat them as syntax. Without grouping, -o (OR) has lower precedence than AND, which produces results you did not mean.

Doing something: -print, -exec and -delete

A test only decides which entries; an action says what to do with them. The default is -print. The important ones:

  • -print0: print the path ended by a NUL byte instead of a newline, for safe piping into xargs -0.
  • -exec COMMAND {} ;: run the command once per file; {} stands for the path.
  • -exec COMMAND {} +: run the command with as many paths as fit on one command line (like xargs).
  • -delete: remove matches. Implies -depth.
  • -printf FORMAT (GNU only): print custom fields such as %p path, %s size, %f base name.
Bash -exec with ; and with +

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.

The difference matters for speed: \; starts one process per file (10,000 files, 10,000 processes), + starts a handful. Use + whenever the command accepts many arguments, as wc, rm, chmod, grep and mv -t do. Use \; when the command can only handle one file, or when the order of execution matters.

The braces and semicolon need quoting from the shell too: {} is usually safe, but ; must be \; or ';'.

-delete, and how to use it safely

find ... -delete is the right way to remove many files matching a rule. Two rules keep it safe:

  1. Run the same command without -delete first and look at the list.
  2. Put -delete last. The expression is evaluated left to right, so find . -delete -name '*.tmp' deletes everything (the -name test comes too late).
Bash -delete: preview first, and the order mistake

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.

Note that we did not combine -prune with -delete here: -delete switches on -depth (children before parents), and then -prune has no effect. GNU find says so (The -delete action automatically turns on -depth, but -prune does nothing when -depth is in effect.) and, in our test, deleted nothing. Use -not -path with -delete.

The last command prints everything even though -name '*.nothing' matches nothing: -print came first and ran for every entry. Replace -print with -delete and the whole tree is gone. This is why you always preview.

Pruning: not descending into a directory

Some directories you never want to search: .git, node_modules, caches. -prune stops find from entering a directory it has just matched. The standard idiom is:

TEXT
find . -path ./node_modules -prune -o  -name '*.js' -print
        ^ if this matches, prune and be true      ^ otherwise test and print

The -o is essential: for the pruned directory, -prune is true so the right-hand side is skipped; for everything else -path is false and the right-hand side runs. And note that -print must be explicit here, because without it GNU find would also print the pruned directory itself.

Bash Pruning a directory with -prune

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.

The second form (-not -path) is easier to read but still walks the pruned tree and discards the entries afterwards, which is far slower for a directory with 100,000 files. Prefer -prune when speed matters.

Filenames with spaces: -print0 and xargs -0

Never pipe plain find output into a loop or xargs when names may contain spaces or newlines. The newline-separated list is ambiguous. The NUL byte, the only character that cannot appear in a path, solves it.

Bash Safe pipelines with -print0 and xargs -0

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.

In the broken version the apostrophe in it's.txt makes xargs stop with an unmatched-quote error (GNU xargs treats quotes and backslashes specially). Even without the apostrophe, my file.txt would have been split into the two items ./my and file.txt. The -print0 | xargs -0 pair is the habit of every careful shell user; see xargs for more, and the while read -d '' loop in Loops.

locate: fast search from an index

find reads the actual filesystem, which is exact but slow on a large disk. locate instead searches a prebuilt database of all file names (updated daily by updatedb, usually via cron or a systemd timer), so it answers in milliseconds:

CONSOLE
$ locate nginx.conf
/etc/nginx/nginx.conf
/usr/share/doc/nginx/examples/nginx.conf
$ locate -i '*.pdf' | head -3
$ sudo updatedb          # refresh the index now

Read-only example: locate and its mlocate/plocate packages are not installed in our sandbox, and a database of a throw-away temporary directory would be useless anyway. Its limits are worth knowing: results can be stale (a file created a minute ago is invisible until the next updatedb), it can only show files you are allowed to see, it matches by name only, and it cannot filter by size or time. Use locate for "where is this file on my machine?" and find for everything precise.

Finding commands: which, type and command -v

For executables the question is different: "which program runs when I type python?". The shell searches the directories listed in $PATH in order. Three tools tell you the answer:

Bash which, type and command -v

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.

command -v NAME is the portable and script-friendly way: it prints the path (or alias, or builtin name) and exits non-zero if the name is unknown. type is Bash's richer version and tells you the kind of command (builtin, file, alias, function; type -a shows all matches). The external which program is a Debian-style shell script or binary and, on some systems, only prints files from PATH without knowing about aliases and builtins, so avoid it in scripts. More about command types in The Shell: Commands, Arguments and Options.

Performance and recipes

  • Start from the narrowest directory you can: find ~/projects beats find /.
  • Put cheap tests (-name, -type) first; -exec last. find evaluates left to right and stops at the first false test.
  • -prune skips whole subtrees; -maxdepth limits recursion; -xdev keeps find on one filesystem (it will not wander into /proc or network mounts).
  • Prefer -exec ... + or -print0 | xargs -0 over \; for many files.
  • GNU find accepts a missing start path (find -name x means .); BSD/macOS find requires one. Always write ..
TEXT
find . -type f -name '*.log' -size +100M          big logs
find . -type f -mtime +30 -name '*.tmp' -delete   old temp files
find . -type f -exec grep -l 'TODO' {} +          files mentioning TODO
find . -type f -perm /o+w                         world-writable files
find . -xtype l                                   broken symlinks (GNU)
find . -type d -empty -delete                     remove empty directories

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why should you write find . -name '*.c' with quotes?

    An unquoted *.c may be expanded by the shell to the .c files of the current directory, so find would get a list of names instead of a pattern. Quoting passes the star through.
  2. 2. What is the difference between -exec cmd {} \; and -exec cmd {} +?

    The semicolon form starts one process for every match. The plus form appends many paths to a single command line, like xargs, which is much faster.
  3. 3. What does find . -delete -name '*.tmp' do?

    find evaluates the expression from left to right, and -delete runs on every entry before -name is tested. Always put -delete last and preview first.
  4. 4. Which pipeline is safe for file names containing spaces and newlines?

    NUL is the only byte that cannot be part of a path, so -print0 with xargs -0 never mis-splits names. The other pipelines split on whitespace or newlines.
  5. 5. How does -prune help in find . -path ./node_modules -prune -o -name '*.js' -print?

    When -path matches, -prune is true and find does not enter that directory; the -o makes the other branch run for all other entries.

Summary

  • find DIR tests actions walks a tree recursively; tests are ANDed left to right, and -print is the default action.
  • Quote patterns (-name '*.c'), escape parentheses, and put -maxdepth early.
  • Common tests: -name, -type, -size, -mtime/-newermt, -perm, -empty; combine with -not, -o and \( \).
  • -exec cmd {} + batches; \; runs per file; -delete goes last and only after a preview.
  • -prune skips directories; -print0 | xargs -0 is the safe way to pipe file names.
  • locate searches a prebuilt index (fast but possibly stale); command -v, type and which find commands in PATH.

Next: Hard Links, Symbolic Links and Inodes