December 4, 2026

The Linux find Command: Examples for Names, Sizes, Dates and the Traps in Each

A terminal on a monitor showing a glowing directory tree with one branch highlighted

find walks a directory tree and tests every file it meets. The syntax looks like a pile of flags, but it is a small expression language with its own operator precedence, and most of the surprises come from that. Every command below was run with GNU findutils 4.9.0 on Ubuntu 24.04 against a small test tree, and the output shown is what came back.

Video: Corey Schafer on YouTube

The shape of a find command

Starting point, then expression

The manual page opens with the one sentence that explains the whole program: GNU find searches the directory tree rooted at each given starting-point by evaluating an expression. So a command has two parts. First where to look, then what to test and what to do.

find . -name '*.py'
./src/app.py

The . is the starting point. -name '*.py' is a test. No action is given, so find supplies one: with no expression of its own, the manual says, -print is used.

The test tree

The examples use this layout:

proj/
  src/        app.py  Util.PY  empty.txt
  logs/       big.log (2.5 MB)  small.log (300 bytes)
              old.log (10 days old)  yest.log (36 hours old)
  my docs/    notes one.txt
  node_modules/pkg/index.js
  empty/
Brown wooden drawer

Searching by name and type

Quote the pattern

-name compares a shell pattern against the base of the file name, with the leading directories removed. It is case sensitive. -iname is not:

find . -iname '*.py'
./src/Util.PY
./src/app.py

The quotes matter. The manual warns in so many words to enclose the pattern in quotes to protect it from expansion by the shell. Without them, the shell expands *.py before find ever starts. In a directory that holds a.py, app.py and b.py, this is what happens:

find . -name *.py
find: paths must precede expression: `app.py'
find: possible unquoted pattern after predicate `-name'?

That is the lucky case, because it fails loudly. With exactly one matching file in the current directory the command runs without complaint and searches for that one name only.

Files, directories, depth

-type f matches regular files and -type d directories. -maxdepth limits how far down the walk goes:

find . -maxdepth 1 -type d
.
./empty
./logs
./my docs
./node_modules
./src

The starting point itself is depth 0, which is why . is in the list. Add -mindepth 1 to drop it.

Searching by size

The rounding trap

-size takes a number and a unit: c for bytes, k for kibibytes, M for mebibytes, G for gibibytes. With no unit at all the number means 512-byte blocks. A plus sign means more than, a minus sign less than.

find . -type f -size +1M
./logs/big.log

So far so good. Now the reverse, files under one mebibyte:

find . -type f -size -1M
./logs/old.log
./logs/yest.log
./src/empty.txt

The tree holds eight files under a megabyte. Only three came back, and all three are empty. The 300-byte small.log is missing.

The manual explains it: sizes are counted in the stated units, rounding up. A 300-byte file rounds up to 1 M. One is not less than one, so it fails the test. Only a zero-byte file rounds to 0. The manual states the consequence outright: -size -1M is not equivalent to -size -1048576c.

The fix is to ask in a smaller unit:

find . -type f -size -1024k

That returned all eight.

Weighing scale

Searching by time

Days are counted in whole 24-hour blocks

-mtime n tests when a file’s data was last modified, in units of 24 hours. The same plus and minus convention applies, and the same kind of rounding bites.

find . -type f -mtime +7
./logs/old.log

The ten-day-old file. Now a file modified 36 hours ago, tested three ways:

find . -type f -mtime 1      # ./logs/yest.log
find . -type f -mtime +1     # ./logs/old.log only
find . -type f -mtime -1     # files from the last 24 hours

Thirty-six hours is more than one day by any ordinary reading, yet -mtime +1 skipped it. The manual’s note on the time tests says any fractional part is ignored, so 36 hours counts as 1 day. +1 means strictly more than 1, which in practice means at least two full days.

Minutes, and comparing against another file

When day granularity is too coarse, -mmin does the same job in minutes: -mmin -60 is anything changed in the last hour. -newer file skips arithmetic altogether and matches whatever was modified more recently than a reference file, which is handy with a timestamp file touched at the start of a job.

-empty matches zero-length files and directories with nothing in them:

find . -empty
./empty
./logs/old.log
./logs/yest.log
./src/empty.txt

Combining tests: and, or, not

The implicit and

Tests written side by side are joined with a logical and. The manual: where an operator is missing, -a is assumed. -o is or, ! is not.

find src -type f ! -name '*.py'
src/Util.PY
src/empty.txt

Util.PY is there because -name is case sensitive.

The or that drops results

This is the trap that catches nearly everyone once:

find . -name '*.py' -o -name '*.txt' -print
./src/empty.txt
./my docs/notes one.txt

Two kinds of file were asked for and app.py is missing. The reason is precedence. The man page lists the operators in decreasing order of binding, and and binds tighter than or. The command above therefore reads as: name matches *.py, or (name matches *.txt and print). The Python file satisfies the left side and nothing tells find to print it.

Parentheses, escaped from the shell, fix it:

find . \( -name '*.py' -o -name '*.txt' \) -print
./src/empty.txt
./src/app.py
./my docs/notes one.txt

Skipping a directory with -prune

A filter such as ! -path './node_modules/*' hides the results but still makes find walk every file in the directory. -prune stops the descent. Its definition in the manual is short: if the file is a directory, do not descend into it.

find . -path ./node_modules -prune -o -type f -name '*.js' -print

That printed nothing, where a plain find . -name '*.js' returned ./node_modules/pkg/index.js. Read it with the precedence rule from the last section: if the path is ./node_modules, prune it; otherwise, if it is a regular file ending in .js, print it. The explicit -print is required. Leave it off and the default action applies to the whole expression, which prints the pruned directory’s own name.

Person holding pliers to a plant

Running a command on each result

One at a time, or in batches

-exec has two endings and they behave differently.

find . -name '*.log' -exec echo got {} \;
got ./logs/old.log
got ./logs/big.log
got ./logs/yest.log
got ./logs/small.log

Ending with \; runs the command once per file. Four files, four processes. Ending with + appends the file names and runs the command as few times as possible:

find . -name '*.log' -exec echo got {} +
got ./logs/old.log ./logs/big.log ./logs/yest.log ./logs/small.log

For anything that accepts many file arguments, such as grep, chmod or rm, the + form is the faster one.

Spaces in file names

Piping find into xargs breaks on the first file with a space in its name, because xargs splits on whitespace:

find . -name '*.txt' | xargs ls -l
ls: cannot access './my': No such file or directory
ls: cannot access 'docs/notes': No such file or directory
ls: cannot access 'one.txt': No such file or directory

-print0 separates names with a null byte instead of a newline, and xargs -0 reads them that way:

find . -name '*.txt' -print0 | xargs -0 ls -l

Both files listed correctly. The manual notes this form copes with file names containing newlines or other white space.

-execdir

-execdir works like -exec but runs the command from the directory that holds the matched file. The man page describes it as a much more secure method for invoking commands, because it avoids race conditions while paths are being resolved.

Deleting, and why order matters

Print first

-delete removes what the expression matched. The safe habit is to run the command with -print in its place, read the list, then swap the action:

find logs -name 'old.log' -print
find logs -name 'old.log' -delete

The expression is evaluated left to right

-delete is an action, and it fires at the point where it appears. In a scratch directory t holding a.txt and b.log:

find t -delete -name '*.log'
ls -d t
ls: cannot access 't': No such file or directory

The name test came too late. Everything went, including the directory t itself. The manual carries this exact warning: putting -delete first will make find try to delete everything below the starting points given. It also notes that -delete switches on -depth automatically, so a directory’s contents are processed before the directory.

One more formatting tool is worth knowing before reaching for ls. -printf prints chosen fields, here size in bytes and path, ready for sorting:

find logs -type f -printf '%s\t%p\n' | sort -n
0   logs/yest.log
300 logs/small.log
2500000 logs/big.log

Unlike -print, the manual points out, -printf adds no newline of its own. Forget the \n and the whole listing arrives as one line.

Sources

  • find(1), Linux manual page (GNU findutils), man7.org: https://man7.org/linux/man-pages/man1/find.1.html

719 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *