Skip to lesson

Linux Command Line · Lesson 21 of 43

Building commands from input with xargs

Turn lines of input into command arguments with xargs: batch many files into one command, handle odd file names safely and run jobs in parallel.

Beginner to intermediate·12 min·Text, redirection and pipes·Updated 2026-11-24

UNDERSTAND IT. THEN TRY IT.

What you can do after this lesson

You will connect find, grep and ls to commands that need arguments instead of input. The pipe carries text to the standard input of the next program, but many programs, such as rm, mv, cp and mkdir, ignore standard input and only look at their arguments. xargs is the adapter between the two worlds.

The problem xargs solves

This does not do what it seems to do:

text
ls *.tmp | rm

rm receives the names on its standard input and complains that it has no files to remove. xargs reads standard input, splits it into words and calls the command with those words as arguments. The command becomes a program that expects arguments again. Let us look at the idea with the harmless echo:

bash
cd ~/project
printf 'one\ntwo\nthree\n' | xargs echo "items:"
result
items: one two three

xargs ran echo "items:" one two three once, with all three words attached. Without a command, xargs uses echo by default.

One at a time, or in groups

By default, xargs packs as many arguments as it can into each call. Use -n to limit how many it passes each time:

bash
printf 'a b c d e\n' | xargs -n 2 echo
printf 'a b c\n' | xargs -n 1 echo "file:"
result
a b
c d
e
file: a
file: b
file: c

Running a command once per item is slower than packing them all together, but it is useful when the command only handles one argument, such as many wget downloads.

Choosing where the item goes: -I

When the item must appear in the middle of a command, use -I with a placeholder. xargs then runs the command once per input line, replacing every placeholder:

bash
ls docs | xargs -I{} echo "docs/{} is a file"
printf 'notes\ntasks\n' | xargs -I{} cp docs/{}.txt docs/{}.bak
ls docs
result
docs/notes.txt is a file
docs/tasks.txt is a file
notes.bak
notes.txt
tasks.bak
tasks.txt

The placeholder can be any word. {} is the usual one, and it matches the {} of find -exec.

find and xargs

The classic combination finds files and then runs a command on all of them. To search for a word inside every .txt file:

bash
find . -name "*.txt" | xargs grep -l "Buy"
result
./docs/tasks.txt

This works well until a name contains a space, because xargs splits on spaces too. The safe form uses NUL characters, which can never appear in a file name. find writes them with -print0 and xargs reads them with -0:

bash
touch "my notes.txt"
find . -name "my*" -print0 | xargs -0 ls
result
./my notes.txt

If you only need to run a command on each file found, find ... -exec command {} + does the same without xargs. xargs is still the better tool when the input comes from something other than find, or when you need parallel runs.

Safety: preview first

Because xargs builds commands from text, a typo can run a destructive command on the wrong names. Insert echo in front of the command to see exactly what would run:

bash
touch a.tmp b.tmp
find . -name "*.tmp" -print0 | sort -z | xargs -0 echo rm
find . -name "*.tmp" -print0 | sort -z | xargs -0 rm -v
result
rm ./a.tmp ./b.tmp
removed './a.tmp'
removed './b.tmp'

If the preview looks right, remove echo. Another useful option is -p, which asks before each command, and -t, which prints each command to the error output as it runs. With no input at all, GNU xargs still runs the command once; add -r (--no-run-if-empty) to prevent that, especially with rm.

Running jobs in parallel

-P runs several commands at the same time. On a machine with several cores, this speeds up work such as compressing many files. -P 4 -n 1 runs four jobs at once, one file each:

bash
printf '1\n2\n3\n4\n' | xargs -P 2 -n 1 echo "job"
text
job 1
job 2
job 3
job 4

The order of the output may vary because the jobs finish in no fixed order, which is why the output above is only an example.

A different separator

-d sets the input separator. -d '\n' splits only on newlines, so names with spaces survive even without find -print0, as long as no name contains a newline:

bash
printf 'my notes.txt\nother file.txt\n' | xargs -d '\n' -n 1 echo "name:"
result
name: my notes.txt
name: other file.txt

Common mistakes

Splitting names on spaces. Use -print0 and -0, or -d '\n'.

Running destructive commands without a preview. Put echo in front first.

Forgetting that -I changes the batching. With -I, the command runs once per line.

Using BSD and GNU options interchangeably. On macOS, xargs has no -d, so use -0 or -I; it accepts -r but ignores it, because BSD xargs already skips the command when there is no input.

Exercise

List the .txt files with find, then count the lines of all of them with xargs wc -l. Use -I{} to copy each .csv and .log file to the same name plus .old. Create a file name with a space and prove that -print0 | xargs -0 handles it. Finally, preview the removal of the .old files with echo before deleting them.

KNOWLEDGE CHECK

A quick check. You’ve got this.

Try each question. Every answer comes with an explanation.

0 / 4 understood
01 Why does ls | rm not delete the listed files?
02 What does the -I{} option of xargs do?
03 Which pair is the safe way to pass file names containing spaces?
04 What is a good first step before xargs rm ...?

Ready for the next small win?

Mark the lesson complete when you are ready.

Course navigation

Tools and Workflow

Linux Command Line tutorial

Learn to work confidently in the terminal, from your first command to pipes, permissions, scripts and a real log-analysis project.

Your progress 0/43
Learning something new?Explore all courses