On this page 6 sections
  1. What xargs solves
  2. One argument at a time
  3. Use placeholders
  4. Handle filenames safely
  5. Parallel execution
  6. Ask before execution
01

What xargs solves

Many commands print information to standard output while other commands expect filenames or values as arguments. xargs bridges that gap by converting input lines into command-line arguments.

Simple example
printf '%s\n' file1 file2 | xargs ls -l
02

One argument at a time

-n controls how many input items are passed to each command execution.

One item per execution
cat hosts.txt | xargs -n 1 host
03

Use placeholders

-I defines a placeholder that can appear anywhere inside the command.

Resolve multiple hosts
cat hosts.txt | xargs -I {} dig +short {}
Check files
cat files.txt | xargs -I {} ls -lh "{}"
04

Handle filenames safely

Whitespace and special characters make plain xargs unsafe for arbitrary filenames. find -print0 combined with xargs -0 uses null characters as delimiters and correctly handles spaces and newlines.

Safe file handling
find /tmp -type f -print0 | xargs -0 ls -lh
05

Parallel execution

-P controls how many command processes may run simultaneously. Parallelisation can greatly speed up independent operations but should be used carefully against constrained systems or services.

Four parallel workers
cat hosts.txt | xargs -n 1 -P 4 host
06

Ask before execution

-p displays the generated command and asks for confirmation before running it.

Interactive confirmation
cat old-files.txt | xargs -p rm