Skip to content

Menu

find and xargs Cheat Sheet: Tests, -exec, -print0 and Parallel Runs

Checked against find and xargs GNU findutils 4.11.0 manual (Texinfo, 9 July 2026); examples run with macOS BSD find and xargs on 28 November 2026. Each section links to the official docs it was checked against.

This sheet is for anyone who uses find to pick files and then needs to do something with them safely. It was checked against the GNU findutils 4.11.0 manual, read from the Texinfo source in the release tarball on ftp.gnu.org because www.gnu.org was down on 28 November 2026. Examples were run with the BSD find and xargs that ship with macOS; rows that only work in GNU, or that my shell wouldn't let me run, are marked untested.

When to use what

To run a command on what find matched, you have two good options and one bad one. -exec cmd {} + packs many file names into each run of the command and needs nothing else. find ... -print0 | xargs -0 cmd does the same and adds xargs's extras, like -P for parallel runs and -n to limit how many names go into each call. The bad option is plain find | xargs: on my machine a file called with space.log turned into two arguments and ls failed on both halves.

Use -exec cmd {} \; only when the command really takes one file at a time, since it starts a new process per match. -execdir is the safer cousin when the command should run inside the file's own directory.

For time filters, remember that -mtime counts whole 24-hour periods and throws away the fraction, so -mtime +1 means at least two days old. When you have an exact cutoff, -newer reffile is easier to reason about. The glob pattern tester helps when a -name pattern isn't matching what you expect, and our bash scripting safely post covers quoting, which is where most find scripts break. If you only need to count lines in some pasted text, the line counter does the job of wc -l without a terminal.

GNU find rounds each file's size up to the unit before comparing, so -size -1k or -size -1M matches only empty files. macOS find disagreed in my test and matched a 500-byte file with -size -1k; counting in bytes gives the same result on both.

find . -name '*.log'

Match file names by shell pattern; quote it so the shell doesn't expand it first

find . -iname 'readme*'

Same as -name but case-insensitive

From the docs, not run by us
find src -type f -size -1024c

Regular files under 1 KiB, counted in bytes (c) so rounding can't change the answer

find . -size +100M

Files bigger than 100 MiB (k, M, G are powers of 1024)

From the docs, not run by us
find . -maxdepth 1 -empty -type f

Empty regular files in the current directory only

find . -mindepth 2 -name '*.md'

Skip the top level and match only deeper files

From the docs, not run by us

-mtime n counts 24-hour periods and ignores the fraction. In -newerXY, X is the file's timestamp and Y the reference's: a access, B birth, c status change, m modification, and t (Y only) for a date string.

find logs -type f -mtime +30

Files last modified more than 30 full days ago

find logs -type f -newer ref

Files modified more recently than the file ref

find logs -type f -newermt '2026-06-01'

Files modified after a date: -newermt is -newerXY with the file's m time against a literal time

find . -mmin -15

Files modified in the last 15 minutes

From the docs, not run by us
find lg -newermt '2026-06-01' ! -newermt '2026-07-01'

Files modified during June 2026, by pairing a test with its negation

find ~ -daystart -type f -mtime 1

GNU: files modified yesterday, measured from midnight instead of 24 hours back

From the docs, not run by us

-delete switches on -depth, and -depth makes -prune do nothing, so don't combine them.

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

Skip node_modules entirely and print the other .js files

find . -type d -name node_modules -prune -o -type f -print

Skip every directory called node_modules at any depth

find . -maxdepth 1 -name 'empty.txt' -delete

Delete matches; run it with -print first to see what would go

From tightest to loosest: ( ), then !, then and (-a, or nothing between two tests), then -o, then GNU's ,. Since the implicit and binds tighter than -o, a trailing -print sticks to the last test only. Escape the parentheses so the shell passes them through.

find . \( -name '*.js' -o -name '*.ts' \) -print

Group alternatives so -print applies to both names

find . -name '*.md' -o -name '*.ts' -print

Without grouping -print binds to *.ts only, so .md files match but never print

find . -type f ! -name '*.js'

! negates the test after it; -not is the non-POSIX spelling

find . -name '*.bak' -fprint bak.txt , -name '*.log' -fprint log.txt

GNU: the comma runs both halves on every file, writing two lists in one pass

From the docs, not run by us

My shell blocks -exec, so those rows come from the man page only.

find . -name '*.tmp' -exec rm {} +

Run one rm with many file names at once, like xargs does

From the docs, not run by us
find . -name '*.sh' -exec chmod +x {} \;

Run the command once per file (slower, but some commands need it)

From the docs, not run by us
find . -name '*.png' -execdir optipng {} +

Run the command from inside each file's own directory

From the docs, not run by us
find logs -name '*.log' -print0

Print names separated by NUL bytes for xargs -0

macOS find rejected -printf as an unknown primary, so every row here is GNU only. Time directives take a second letter: %T is modification, %A access, %C status change and %B birth, followed by a strftime field such as F, H, M or @.

find . -type f -printf '%s\t%p\n' | sort -n

Size in bytes and path, smallest file first

From the docs, not run by us
find . -type f -printf '%T@ %p\n' | sort -n | tail -5

The five most recently modified files, sorted by epoch seconds

From the docs, not run by us
find . -type f -printf '%TF %TH:%TM %p\n'

Modification date and time in front of each path

From the docs, not run by us
find src -printf '%d %y %P\n'

Depth, type letter and the path with the starting point removed

From the docs, not run by us
find . -type f -printf '%m %u %f\n'

Octal mode, owner name and base name for each file

From the docs, not run by us
find . -type l -printf '%p -> %l\n'

Each symlink and the target it points to

From the docs, not run by us
find logs -name '*.log' -print0 | xargs -0 wc -l

Pass names safely even when they contain spaces or quotes

printf '1\n2\n3\n4\n' | xargs -n 1 -P 2 echo item

Run up to 2 commands at a time, one argument each

printf 'a\nb\n' | xargs -I{} echo "file-{}.txt"

Put each input line where {} appears; implies one line per command, and blanks no longer split names

printf '' | xargs -r echo ran

Skip the command when input is empty (prints nothing); without -r GNU xargs runs it once

macOS xargs accepted -r in my test.

... | xargs -t gzip

Print each command to stderr before running it

From the docs, not run by us

Worked example: Compress week-old logs without tripping over spaces

First I checked which files the filter catches, then the NUL-separated pipeline, then what happens without it. This ran on macOS in a scratch folder with three logs, one of them named with space.log and one back-dated to January.

$ find logs -type f -mtime +30
logs/old.log
$ find logs -name '*.log' -print0 | xargs -0 wc -l
       1 logs/with space.log
       1 logs/new.log
       1 logs/old.log
       3 total
$ find logs -name '*.log' | xargs ls -1
ls: logs/with: No such file or directory
ls: space.log: No such file or directory
logs/new.log
logs/old.log

The first command shows only the back-dated file, which is the list you'd hand to gzip in real use. The second shows wc receiving all three names intact, including the one with a space. The third is the trap: without -print0 and -0, xargs splits on whitespace, so ls was handed logs/with and space.log and failed on both. To do the real job, use find logs -type f -mtime +30 -print0 | xargs -0 gzip.

Common mistakes

  • Piping plain find output into xargs in a folder where any name has a space.

    Use -print0 with xargs -0, or skip xargs and use -exec cmd {} +.

  • Putting -delete before the tests, as in find . -delete -name '*.tmp'.

    find evaluates left to right, so that deletes everything. Put -delete last and dry-run with -print.

  • Expecting -mtime +1 to match a file changed 30 hours ago.

    Fractions are dropped, so 30 hours counts as 1 day and +1 means 2 or more. Use -mmin or -newer for precise cutoffs.

  • Copying a Linux script that uses -printf onto a Mac.

    macOS find rejected -printf here. Install GNU findutils, or rewrite with -print0 and a small loop that calls stat.

  • Using xargs -P 8 without -n and seeing only one process.

    The findutils manual warns that without -n, -s or -L the command will probably run only once. Add -n 1 or a batch size.

  • Writing -size -1M in GNU find to mean smaller than a megabyte.

    Sizes are rounded up to whole units first, so only empty files qualify. Use -size -1048576c instead.