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
find src -type f -size -1024cRegular files under 1 KiB, counted in bytes (c) so rounding can't change the answer
find . -size +100MFiles bigger than 100 MiB (k, M, G are powers of 1024)
find . -maxdepth 1 -empty -type fEmpty regular files in the current directory only
find . -mindepth 2 -name '*.md'Skip the top level and match only deeper files
-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 +30Files last modified more than 30 full days ago
find logs -type f -newer refFiles 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 -15Files modified in the last 15 minutes
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 1GNU: files modified yesterday, measured from midnight instead of 24 hours back
-delete switches on -depth, and -depth makes -prune do nothing, so don't combine them.
find . -path ./node_modules -prune -o -name '*.js' -printSkip node_modules entirely and print the other .js files
find . -type d -name node_modules -prune -o -type f -printSkip every directory called node_modules at any depth
find . -maxdepth 1 -name 'empty.txt' -deleteDelete 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' \) -printGroup alternatives so -print applies to both names
find . -name '*.md' -o -name '*.ts' -printWithout 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.txtGNU: the comma runs both halves on every file, writing two lists in one pass
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
find . -name '*.sh' -exec chmod +x {} \;Run the command once per file (slower, but some commands need it)
find . -name '*.png' -execdir optipng {} +Run the command from inside each file's own directory
find logs -name '*.log' -print0Print 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 -nSize in bytes and path, smallest file first
find . -type f -printf '%T@ %p\n' | sort -n | tail -5The five most recently modified files, sorted by epoch seconds
find . -type f -printf '%TF %TH:%TM %p\n'Modification date and time in front of each path
find src -printf '%d %y %P\n'Depth, type letter and the path with the starting point removed
find . -type f -printf '%m %u %f\n'Octal mode, owner name and base name for each file
find . -type l -printf '%p -> %l\n'Each symlink and the target it points to
find logs -name '*.log' -print0 | xargs -0 wc -lPass names safely even when they contain spaces or quotes
printf '1\n2\n3\n4\n' | xargs -n 1 -P 2 echo itemRun 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 ranSkip the command when input is empty (prints nothing); without -r GNU xargs runs it once
macOS xargs accepted -r in my test.
... | xargs -t gzipPrint each command to stderr before running it
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
findoutput intoxargsin a folder where any name has a space.Use
-print0withxargs -0, or skip xargs and use-exec cmd {} +.Putting
-deletebefore the tests, as infind . -delete -name '*.tmp'.find evaluates left to right, so that deletes everything. Put
-deletelast and dry-run with-print.Expecting
-mtime +1to match a file changed 30 hours ago.Fractions are dropped, so 30 hours counts as 1 day and
+1means 2 or more. Use-mminor-newerfor precise cutoffs.Copying a Linux script that uses
-printfonto a Mac.macOS find rejected
-printfhere. Install GNU findutils, or rewrite with-print0and a small loop that callsstat.Using
xargs -P 8without-nand seeing only one process.The findutils manual warns that without
-n,-sor-Lthe command will probably run only once. Add-n 1or a batch size.Writing
-size -1Min GNU find to mean smaller than a megabyte.Sizes are rounded up to whole units first, so only empty files qualify. Use
-size -1048576cinstead.
Related cheat sheets
Sources
- https://www.gnu.org/software/findutils/manual/find.html
- https://man7.org/linux/man-pages/man1/find.1.html
- https://man7.org/linux/man-pages/man1/xargs.1.html
How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.