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.
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:
ls *.tmp | rmrm 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:
cd ~/project
printf 'one\ntwo\nthree\n' | xargs echo "items:"items: one two threexargs 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:
printf 'a b c d e\n' | xargs -n 2 echo
printf 'a b c\n' | xargs -n 1 echo "file:"a b
c d
e
file: a
file: b
file: cRunning 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:
ls docs | xargs -I{} echo "docs/{} is a file"
printf 'notes\ntasks\n' | xargs -I{} cp docs/{}.txt docs/{}.bak
ls docsdocs/notes.txt is a file
docs/tasks.txt is a file
notes.bak
notes.txt
tasks.bak
tasks.txtThe 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:
find . -name "*.txt" | xargs grep -l "Buy"./docs/tasks.txtThis 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:
touch "my notes.txt"
find . -name "my*" -print0 | xargs -0 ls./my notes.txtIf 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:
touch a.tmp b.tmp
find . -name "*.tmp" -print0 | sort -z | xargs -0 echo rm
find . -name "*.tmp" -print0 | sort -z | xargs -0 rm -vrm ./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:
printf '1\n2\n3\n4\n' | xargs -P 2 -n 1 echo "job"job 1
job 2
job 3
job 4The 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:
printf 'my notes.txt\nother file.txt\n' | xargs -d '\n' -n 1 echo "name:"name: my notes.txt
name: other file.txtCommon 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.
Ready for the next small win?
Mark the lesson complete when you are ready.