Here are some commonly used options for xargs:
-
-n: Limits the number of arguments passed to each command execution.echo {1..10} | xargs -n 2 echo "Processing batch:" -
-P: Specifies the number of processes to run in parallel.ls *.txt | xargs -P 4 -I {} sh -c 'echo "Processing {}"; sleep 1;' -
-I {}: Replaces occurrences of{}in the command with the input item.echo "file1 file2" | xargs -I {} mv {} /destination/ -
-p: Prompts the user before executing each command.echo "file1 file2" | xargs -p rm -
-r: Prevents running the command if there is no input (also known as--no-run-if-empty).echo "" | xargs -r echo "Output:" -
-0: Reads input items terminated by a null character, useful for handling filenames with spaces.find . -name "*.txt" -print0 | xargs -0 rm -
--max-args=N: Similar to-n, specifies the maximum number of arguments per command line.echo {1..10} | xargs --max-args=3 echo "Processing batch:"
These options can be combined to create powerful command-line workflows.
