How to Build and Run Commands with xargs

Published 2026-08-05 · Learn Linux

The xargs command reads items from standard input and runs another command with them as arguments. find . -name "*.tmp" | xargs rm deletes every matching file. It turns pipes into powerful batch operations.

Pass files to a command

The classic pattern: find produces a list of files, and xargs hands them to a command all at once.

$ find . -name *.log | xargs rm

Process items in batches with -n

Commands like rm accept many files, but others take one at a time. -n 1 runs the command separately for each item:

$ find . -name *.jpg | xargs -n 1 echo Processing:

Substitute arguments with -I

Use -I placeholder to place each item anywhere in the command, not just at the end. This copies each file to a backup name:

$ ls *.conf | xargs -I {} cp {} {}.bak

Quote and safety with -0

File names with spaces break xargs because it splits on whitespace. Pair find with -print0 and xargs with -0 to handle any file name safely.

$ find . -name *.log -print0 | xargs -0 rm

When to use xargs

Reach for xargs when a command's output is too long for one command line, or when you want to batch items. For running one command per result, a shell for loop can be clearer. See xargs for the full reference.

Keep learning

Related guides: How to Find Files and Directories with the Linux find Command · Linux Redirection and Pipes: A Practical Guide · How to Delete Files Safely with the Linux rm Command. Read all tutorials on the Learn Linux blog.