Linuxlab

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 name

How 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. xargs bridges 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 is find ... -print0 | xargs -0, where names are separated by a null byte, not a space. find -exec often does the same job, but xargs runs 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.