xargs Command in Linux: Usage and Examples

The xargs utility allows you to build and execute commands from standard input. It is usually used in combination with other commands through piping.
With xargs, you can provide standard input as arguments to command-line utilities like mkdir and rm
.
In this guide, we will cover the basics of using the xargs command, including the options you will use most often in day-to-day shell work.
How to Use the xargs Command
xargs reads arguments from the standard input, separated by blank spaces or newlines, and executes the specified command using the input as the command’s arguments. If no command is provided, the default is /bin/echo
.
The syntax for the xargs command is as follows:
xargs [OPTIONS] [COMMAND [initial-arguments]]The most basic example of using xargs is to pass several strings separated with whitespace using a pipe to xargs and run a command that will use those strings as arguments.
echo "file1 file2 file3" | xargs touchIn the example above, we are piping the standard input to xargs, and the touch
command is run for each argument, creating three files. This is the same as if you would run:
touch file1 file2 file3How to View the Command and Prompt the User
To print the command on the terminal before executing it, use the -t (--verbose) option:
echo "file1 file2 file3" | xargs -t touchtouch file1 file2 file3If you want to get a prompt whether to run each command before executing it, use the -p (--interactive) option:
echo "file1 file2 file3" | xargs -p touchType y or Y to confirm and run the command:
touch file1 file2 file3 ?...yThis option is useful when executing destructive commands.
How to Limit the Number of Arguments
By default, the number of arguments passed to the command is determined by the system’s limit.
The -n (--max-args) option specifies the number of arguments to be passed to the given command. xargs runs the specified command as many times as necessary until all arguments are exhausted.
In the following example, the number of arguments that are read from the standard input is limited to 1.
echo "file1 file2 file3" | xargs -n 1 -t touchAs you can see from the verbose output below, the touch command is executed separately for each argument:
touch file1
touch file2
touch file3How to Run Multiple Commands
To run multiple commands with xargs, use the -I option. It works by defining a replace-str after the -I option, and all occurrences of the replace-str are replaced with the argument passed to xargs.
The following xargs example will run two commands, first it will create the files using touch, and then it will list the files with the ls
command:
echo "file1 file2 file3" | xargs -t -I % sh -c '{ touch %; ls -l %; }'-rw-r--r-- 1 linuxize users 0 May 6 11:54 file1
-rw-r--r-- 1 linuxize users 0 May 6 11:54 file2
-rw-r--r-- 1 linuxize users 0 May 6 11:54 file3A common choice for replace-str is %. However, you can use another placeholder, for example, ARGS:
echo "file1 file2 file3" | xargs -t -I ARGS sh -c '{ touch ARGS; ls -l ARGS; }'How to Specify a Delimiter
Use the -d (--delimiter) option to set a custom delimiter, which can be either a single character or an escape sequence starting with \.
In the following example, we are using ; as a delimiter:
echo "file1;file2;file3" | xargs -d \; -t touchtouch file1 file2 file3How to Read Items from a File
The xargs command can also read items from a file instead of standard input. To do so, use the -a (--arg-file) option followed by the file name.
In the following example, the xargs command will read the ips.txt file and ping each IP address.
8.8.8.8
1.1.1.1We are also using the -L 1 option, which instructs xargs to read one line at a time. If this option is omitted, xargs will pass all IPs to a single ping
command.
xargs -t -L 1 -a ips.txt ping -c 1ping -c 1 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=50 time=68.1 ms
...
ping -c 1 1.1.1.1
PING 1.1.1.1 (1.1.1.1) 56(84) bytes of data.
64 bytes from 1.1.1.1: icmp_seq=1 ttl=59 time=21.4 msUsing xargs with find
xargs is most often used in combination with the find
command. You can use find to search for specific files and then use xargs to perform operations on those files.
To avoid issues with file names that contain newlines or other special characters, always use the find -print0 option, which causes find to print the full file name followed by a null character. This output can be correctly interpreted by xargs using the -0 (--null) option.
In the following example, find will print the full names of all files inside the /var/www/.cache directory and xargs will pass the file paths to the rm
command:
find /var/www/.cache -type f -print0 | xargs -0 rm -fFor destructive operations, you can first preview the generated command with -t or replace rm -f with echo to verify the target files.
Using xargs to Trim Whitespace Characters
xargs can also be used as a tool to remove whitespace from both sides of a given string. Simply pipe the string to the xargs command, and it will do the trimming:
echo " Long line " | xargsLong lineThis can be useful when comparing strings in shell scripts.
#!/bin/bash
VAR1=" Linuxize "
VAR2="Linuxize"
if [[ "$VAR1" == "$VAR2" ]]; then
echo "Strings are equal."
else
echo "Strings are not equal."
fi
# Using xargs to trim VAR1
if [[ $(echo "$VAR1" | xargs) == "$VAR2" ]]; then
echo "Strings are equal."
else
echo "Strings are not equal."
fiStrings are not equal.
Strings are equal.Quick Reference
| Option | Description |
|---|---|
-n N | Pass at most N arguments per command invocation |
-I REPLACE | Replace REPLACE with each input argument |
-t | Print each command before executing (--verbose) |
-p | Prompt before running each command (--interactive) |
-d DELIM | Use DELIM as the input delimiter |
-L N | Read at most N lines per command invocation |
-a FILE | Read arguments from FILE instead of standard input |
-0 | Expect null-delimited input (use with find -print0) |
-P N | Run up to N processes in parallel |
For a printable quick reference, see the xargs cheatsheet .
Troubleshooting
“Argument list too long” error
This happens when xargs passes too many arguments at once. Use -n N to limit the number of arguments per command invocation, for example xargs -n 100.
Filenames with spaces are split incorrectly
By default, xargs splits on whitespace. When processing file paths, always use find -print0 | xargs -0 to handle filenames that contain spaces, newlines, or other special characters.
Command produces no output or wrong results
Verify how arguments are being passed by adding the -t flag to print each command before it runs. This makes it easy to spot quoting or ordering issues.
FAQ
What is the difference between -n and -L?
-n N limits the number of arguments passed per invocation regardless of how they appear in the input. -L N limits the number of lines read per invocation. Use -n when input is space-separated on one line; use -L when each item is on its own line.
Why use -print0 with find and -0 with xargs?
The default whitespace splitting breaks on filenames that contain spaces or newlines. The null character (\0) is the only character that cannot appear in a filename, so using -print0 and -0 is the safe, reliable way to pass file paths between find and xargs.
What happens if I do not provide a command to xargs?
xargs uses /bin/echo as the default command, so it will print the input arguments to standard output.
Conclusion
xargs is a versatile command-line utility that lets you build and execute commands from standard input, making it especially powerful in combination with find
and other pipeline tools. For the full list of available options, see the xargs man page
.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page