shellstreak

find -exec and xargs examples

Updated · Every command on this page ran in our practice shell

Short answer

find . -name "*.tmp" -exec rm {} \; runs rm once per matching file, with {} standing for its path. End with {} + to pass many files to one command: find . -name "*.log" -exec wc -l {} +. To use xargs, pipe with -print0 | xargs -0 so names with spaces survive. Run the find without the action first to preview.

find is good at locating files: by name, type, age or depth. With -exec or xargs it can also act on every match, which is how you clean up temp files, archive old logs or search a set of pages in one command. The examples below ran on a bakery's website folder with logs of different ages.

Preview the list first

Before acting on files, run the find on its own and read the list. It's the same command you're about to run, without the action.

example
learner@shellstreak:~/bakery$ find tmp -name "*.tmp"
tmp/cache-1.tmp
tmp/cache-2.tmp

Run a command per file with -exec \;

-exec command {} \; runs the command once for every match, with {} replaced by the path. The \; marks the end of the command; the backslash keeps the shell from eating the ;. Putting echo first turns it into a preview.

example
learner@shellstreak:~/bakery$ find logs -name "*.log" -mtime +7 -exec echo mv {} archive/ \;
mv logs/access-2026-01-30.log archive/
mv logs/access-2026-02-14.log archive/

Batch files into one run with -exec +

Ending with {} + instead passes as many paths as fit to a single run of the command. It's faster, and commands like wc then print one shared total.

example
learner@shellstreak:~/bakery$ find logs -name "*.log" -exec wc -l {} +
  2 logs/access-2026-01-30.log
  3 logs/access-2026-02-14.log
  1 logs/access-2026-02-24.log
  2 logs/access-2026-03-01.log
  2 logs/error.log
 10 total

Pipe into xargs

xargs reads a list from standard input and adds it to the end of a command. It works after anything that prints paths, not just find. But it splits on spaces, so here gift cards.html arrives as two names and grep complains about both.

example
learner@shellstreak:~/bakery$ find site -name "*.html" | xargs grep -l Pebble
site/index.html
site/menu.html
site/old/legacy.html
grep: site/gift: No such file or directory
grep: cards.html: No such file or directory

Handle spaces with -print0 and xargs -0

By default xargs splits on spaces and new lines, so a file called march 01.txt arrives as two arguments. find -print0 separates names with a null byte and xargs -0 splits on that instead. -exec never has this problem.

example
learner@shellstreak:~/bakery$ find site -name "*.html" -print0 | xargs -0 grep -l Pebble
site/gift cards.html
site/index.html
site/menu.html
site/old/legacy.html

Find by age with -mtime

-mtime +7 matches files last changed 8 or more whole days ago, -mtime -2 less than 2 days ago. find counts in whole 24-hour periods and drops the remainder, so a file 7.9 days old counts as 7. Pair it with -name so you only touch the files you mean.

example
learner@shellstreak:~/bakery$ find logs -name "*.log" -mtime +7
logs/access-2026-01-30.log
logs/access-2026-02-14.log

Narrow the search: -iname, -not, -maxdepth

find site -iname "*.png" ignores case (Logo.PNG matches), -not (or !) inverts the next test, and find site -maxdepth 1 -name "*.html" stays in the starting folder. Tests are combined with AND unless you add -o for OR.

example
learner@shellstreak:~/bakery$ find site -type f -not -name "*.html"
site/images/Logo.PNG
site/images/bread.jpg
site/images/cake.png

Delete safely with -delete

-delete removes each match, and it belongs at the end: find evaluates from left to right, so find tmp -delete -name "*.tmp" deletes everything before checking the name. Run the command without -delete first.

example
learner@shellstreak:~/bakery$ find logs -name "*.log" -mtime +30 -delete; ls logs
access-2026-02-14.log
access-2026-02-24.log
access-2026-03-01.log
error.log

Check yourself

One real exercise from the “Finding & bulk actions” unit. No account needed.

Delete every .tmp file in tmp using find ... -exec rm {} \;. Leave keep.txt and the folder itself alone.

What's checked: the files bakery/tmp/cache-1.tmp, bakery/tmp/cache-2.tmp, bakery/tmp/keep.txt, bakery/tmp.

Common mistakes

  • Putting -delete (or -exec rm) before the tests. Actions go last.

  • Deleting without previewing. Run the exact same find without the action first.

  • Typing ; instead of \; at the end of -exec. The shell takes the bare ; and find complains about a missing argument.

  • Piping file names with spaces into plain xargs. Use -print0 | xargs -0, or -exec ... {} +.

Questions

What's the difference between -exec {} \; and -exec {} +?+

`\;` runs the command once per file. `+` passes many files to one run, like `xargs` does. Use `+` when the command accepts several files.

How do I delete files older than 30 days?+

Preview with `find /path -name "*.log" -mtime +30`, then add `-delete` at the end: `find /path -name "*.log" -mtime +30 -delete`.

Should I use find -exec or xargs?+

`-exec ... +` handles every file name safely and needs no pipe. `xargs` is handy when the list comes from another command, or with `-P` to run jobs in parallel.

Reading is step one. Now make it stick.

Practise this in a five-minute lesson with instant feedback. No account needed.

Practise it now