xargs in Linux: build a command from input
Updated July 3, 2026
xargs takes lines from standard input and passes them as arguments to another command.
Syntax
... | xargs [command]Common cases
echo a b c | xargs mkdir # create directories a, b, c
find . -name '*.tmp' | xargs rm # delete every .tmp found
find . -name '*.log' -print0 | xargs -0 rm # safe with spaces
cat urls.txt | xargs -n1 echo # one argument at a time
ls | xargs -I{} mv {} {}.bak # {} stands for the nameHow it works: why xargs when you have a pipe
A pipe
|feeds output to the standard INPUT of the next command. But many commands (rm,mkdir,cp) take their targets from ARGUMENTS, not from input.xargsbridges the two: it reads lines and appends them to a command as arguments. The main trap is spaces and newlines in file names. The safe way isfind ... -print0 | xargs -0, where names are separated by a null byte, not a space.find -execoften does the same job, butxargsruns the command fewer times and so is faster on big lists.
In the book
This topic is covered in full in the chapter Streams, redirection, pipelines.
See also
Pipes and filters, The find command, Regular expressions.
Try it
Open the sandbox: a real throwaway Linux terminal in your browser. Run the commands above by hand - the container is deleted when you leave.