Linux xargs command
xargs (short for eXtended ARGuments) is a filter for passing arguments to commands, as well as a tool for combining multiple commands.
xargs can convert pipe or standard input (stdin) data into command-line arguments, and can also read data from a file's output.
xargs can also convert single-line or multi-line text input into other formats, such as turning multiple lines into one line, or one line into multiple lines.
The default command for xargs is echo, which means input passed to xargs through a pipe will contain newlines and whitespace; however, after processing by xargs, newlines and whitespace are replaced with spaces.
xargs is a powerful command that can capture the output of one command and pass it to another.
The reason this command is useful is that many commands do not support passing arguments via the | pipe, yet this is often needed in daily work, hence the xargs command. For example:
find /sbin -perm +700 |ls -l #这个命令是错误的
find /sbin -perm +700 |xargs ls -l #这样才是正确的
xargs is generally used together with pipes.
Command format:
somecommand |xargs -item command
Parameters:
- -a file reads from a file as stdin
- -e flag, note that it may sometimes be -E; flag must be a space-separated marker, and xargs stops when it encounters this flag.
- -p prompts the user once before each argument is executed.
- -n num specifies the number of arguments used per command execution; by default all are used.
- -t prints the command before executing it.
- -i or -I (depending on Linux support) assigns each item name from xargs, typically line by line, to {}, which can be used as a replacement.
- -r (no-run-if-empty) stops xargs when its input is empty, so it does not need to execute anything.
- -s num sets the maximum number of characters on the command line, i.e., the maximum command-line length of the command following xargs.
- -L num reads num lines at a time from standard input and sends them to the command.
- -l is the same as -L.
- -d delim sets the delimiter; the default xargs delimiter is the newline, while the argument delimiter is a space. This option changes the xargs delimiter.
- -x means exit, mainly used together with -s.
- -P changes the maximum number of processes; the default is 1, and 0 means as many as it can. I cannot think of an example for this; it is probably rarely used.
Example
xargs acts as a replacement tool, reading input data, reformatting it, and outputting it.
Define a test file containing multiple lines of text data:
# cat test.txt
a b c d e f g
h i j k l m n
o p q
r s t
u v w x y z
Multiple lines of input, single line of output:
# cat test.txt | xargs
a b c d e f g h i j k l m n o p q r s t u v w x y z
Multi-line output with the -n option:
# cat test.txt | xargs -n3
a b c
d e f
g h i
j k l
m n o
p q r
s t u
v w x
y z
The -d option can define a custom delimiter:
# echo "nameXnameXnameXname" | xargs -dX
name name name name
Used together with the -n option:
# echo "nameXnameXnameXname" | xargs -dX -n2
name name
name name
Read stdin and pass the formatted arguments to a command
Suppose there is a command sk.sh and a file arg.txt that stores the arguments:
#!/bin/bash
#sk.sh command content, prints all arguments.
echo $*
Contents of arg.txt:
# cat arg.txt
aaa
bbb
ccc
An option of xargs is -I. Use -I to specify a replacement string {}; this string is replaced when xargs expands it. When -I is used with xargs, the command is executed once for each argument:
# cat arg.txt | xargs -I {} ./sk.sh -p {} -l
-p aaa -l
-p bbb -l
-p ccc -l
Copy all image files to the /data/images directory:
ls *.jpg | xargs -n1 -I {} cp {} /data/images
Using xargs with find
When using rm to delete too many files, you may get an error message:/bin/rm Argument list too long. Use xargs to avoid this problem:
find . -type f -name "*.log" -print0 | xargs -0 rm -f
xargs -0 uses \0 as the delimiter.
Count the lines of all php files in a source code directory:
find . -type f -name "*.php" -print0 | xargs -0 wc -l
Find all jpg files and compress them:
find . -type f -name "*.jpg" -print | xargs tar -czvf images.tar.gz
Other xargs applications
If you have a file containing many URLs you want to download, you can use xargs to download all the links:
# cat url-list.txt | xargs wget -c
Linux Command Reference