On this page 6 sections
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.
printf '%s\n' file1 file2 | xargs ls -lOne argument at a time
-n controls how many input items are passed to each command execution.
cat hosts.txt | xargs -n 1 hostUse placeholders
-I defines a placeholder that can appear anywhere inside the command.
cat hosts.txt | xargs -I {} dig +short {}cat files.txt | xargs -I {} ls -lh "{}"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.
find /tmp -type f -print0 | xargs -0 ls -lhParallel 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.
cat hosts.txt | xargs -n 1 -P 4 hostAsk before execution
-p displays the generated command and asks for confirmation before running it.
cat old-files.txt | xargs -p rm