xargs
| Since: | All Linux | |
|---|---|---|
| macOS(2001 Cheetah) | ||
| Bash 1.0(1989) |
xargs is a command that takes values from standard input and passes them as arguments to another command. Combined with pipes, it lets you feed file lists or search results into another command all at once.
Syntax
Pass standard input as arguments to a command.
command | xargs target-command
Use the -I option to insert input values into a placeholder {}.
command | xargs -I{} target-command {}
Use the -n option to limit the number of arguments passed at once.
command | xargs -n count target-command
Use the -P option to run in parallel.
command | xargs -P num-processes -I{} target-command {}
Handle filenames with spaces or newlines safely (-0).
find . -name "*.txt" -print0 | xargs -0 target-command
Options
| Option | Description |
|---|---|
| -I{} | Replaces {} with each input value. You can use any symbol as the placeholder. |
| -n count | Sets the maximum number of arguments passed to each command invocation. |
| -P count | Specifies the number of processes to run in parallel. |
| -0 | Reads input delimited by null characters. Use with find -print0. |
| -t | Prints each command before executing it. Useful for debugging. |
| -p | Prompts for confirmation before each command is executed. |
| -r | Does not run the command if the input is empty (GNU xargs only). |
| -L lines | Passes the specified number of input lines as arguments per invocation. |
Sample Code
The examples below use this directory structure.
Pass the results of find to another command via xargs. This example deletes all .log files.
find . -name "*.log" | xargs rm
If file paths may contain spaces or special characters, use -print0 with -0.
find . -name "*.log" -print0 | xargs -0 rm
Use -I{} with a placeholder to rename files one by one.
ls *.txt | xargs -I{} mv {} {}.bak
Combine find and grep to search across multiple files.
find . -name "*.php" | xargs grep -l "TODO" ./src/index.php ./src/utils.php
Use -n N to limit how many arguments are passed at once.
echo "a b c d e f" | xargs -n 2 echo a b c d e f
Use -t to print each command before it runs. This is handy for debugging.
echo "file1 file2 file3" | xargs -t -n 1 echo echo file1 file1 echo file2 file2 echo file3 file3
Use -P N to process files N at a time in parallel. This example compresses files using 4 parallel processes.
ls *.csv | xargs -P 4 -I{} gzip {}
This example downloads URLs from a list using 5 parallel processes.
cat urls.txt | xargs -P 5 -I{} wget -q {}
Common Mistakes
Common Mistake 1: Filenames with spaces are split into multiple arguments
By default, xargs splits input on whitespace. A filename like my file.txt is treated as two separate arguments: my and file.txt.
find . -name "*.txt" | xargs rm rm: cannot remove 'my': No such file or directory rm: cannot remove 'file.txt': No such file or directory
Use -print0 and -0 to separate entries with null characters instead of whitespace.
find . -name "*.txt" -print0 | xargs -0 rm
Common Mistake 2: xargs still runs the command even when input is empty
When the input to xargs is empty, it runs the command with no arguments by default. This can have unexpected side effects depending on the command.
echo "" | xargs rm rm: missing operand
Use --no-run-if-empty (GNU) or -r to skip execution when there is no input.
find . -name "*.tmp" -print0 | xargs -0 --no-run-if-empty rm
Notes
When file paths may contain spaces or special characters, find -print0 | xargs -0 is essential. Using null characters as delimiters lets you handle any filename safely.
The -P option makes full use of your CPU by running tasks in parallel. It is especially effective for independent tasks such as compressing, converting, or downloading multiple files. For more on pipes and redirection, see Pipe (|).
If you find any errors or copyright issues, please contact us.