xargs(1)
Build and execute commands from standard input, passing lines as arguments.
Synopsis
xargs [OPTION]... [COMMAND [INITIAL-ARGS]...]Description
xargs reads items from standard input, separated by blanks or newlines, and executes a command with those items as arguments. This is useful for converting stream data into command arguments, especially when piping output from commands like find that produce one item per line.
By default, xargs passes items as trailing arguments to the command. If no command is specified, echo is used. The command is run as many times as needed to consume all input items.
Common options
| Flag | What it does |
|---|---|
-0 | Use null character (\0) as delimiter instead of spaces/newlines; safe for filenames with spaces |
-n NUM | Pass at most NUM arguments to each command invocation |
-I REPLACE | Replace REPLACE string with each input item; allows positioning arguments anywhere |
-p | Prompt user for confirmation before executing each command |
-t | Print the command line to stderr before executing it |
-x | Exit immediately if a command fails or would exceed system limits |
-P NUM | Run up to NUM command processes in parallel |
-d DELIM | Use DELIM as the item delimiter instead of whitespace |
-s SIZE | Limit command line to SIZE characters (default ~128KB) |
-r | Do not run command if there is no input |
Examples
Find all .log files in /tmp and delete them by passing them to rm
find /tmp -name '*.log' | xargs rmSearch for 'pattern' in files, safely handling filenames with spaces using null delimiters
find . -type f -print0 | xargs -0 grep 'pattern'Count lines in multiple files, running wc once per file (-n1)
echo -e 'file1\nfile2\nfile3' | xargs -n1 wc -lCopy each .txt file to a .txt.backup using {} as placeholder for each filename
ls *.txt | xargs -I {} cp {} {}.backupFetch 10 URLs in parallel (4 at a time) using -P for parallel execution
seq 1 10 | xargs -P 4 -I {} curl -s 'http://api.example.com/item/{}'Interactively delete files listed in file_list.txt, prompting before each deletion
xargs -p -n1 rm < file_list.txtDownload URLs from a file, with -t showing each wget command before execution
cat urls.txt | xargs -t wgetSafely handle empty input with -r flag (no command executed if input is empty)
xargs -r bash -c 'echo "Total: $#"' _ < /dev/null