Linux xargs Command
Complete List of Linux Commands
xargs (full English name: eXtended ARGuments) is a filter for passing parameters to commands, and also a tool for combining multiple commands.
xargs can convert pipeline or standard input (stdin) data into command-line arguments, and can also read data from file output.
xargs can also convert single-line or multi-line text input into other formats, for example multi-line to single-line, single-line to multi-line.
The default command of xargs is echo, which means that the input passed to xargs through a pipe will contain newlines and whitespace, but after xargs processing, newlines and whitespace will be replaced by spaces.
xargs is a powerful command that can capture the output of one command and pass it to another command.
The reason this command can be used is mainly because many commands do not support | pipes to pass parameters, yet there is such a need in daily work, so the xargs command came into being, 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: Read from a file as stdin
- -e flag: Note that sometimes it might be -E. flag must be a space-separated marker. When xargs analyzes and encounters this marker, it stops.
- -p: Ask the user once each time an argument is executed.
- -n num: Followed by a number, indicating the number of arguments used at one time when the command is executed; by default all are used.
- -t: Print the command first, then execute it.
- -i or -I (depending on Linux support): assign each item name of xargs, generally line by line, to {}, and {} can be used as a replacement.
- -r no-run-if-empty: When xargs input is empty, stop xargs and do not execute.
- -s num: The maximum number of characters on the command line, referring to the maximum command-line character count of the command following xargs.
- -L num: Read num lines at a time from standard input and send them to the command.
- -l: Same as -L.
- -d delim: Delimiter. The default xargs delimiter is carriage return, and the argument delimiter is a space. What is modified here is the xargs delimiter.
- -x: Means exit, mainly used in conjunction with -s. .
- -P: Modify the maximum number of processes. The default is 1; when it is 0, it is "as many as it can". I have not thought of an example for this; it is probably not used in daily work.
Examples
xargs used as a replacement tool: read input data, reformat it, and output.
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
Multi-line input, single-line 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
-n option multi-line output:
# 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
-d option allows you to define a custom delimiter:
# echo "nameXnameXnameXname" | xargs -dX name name name name
Used in combination with the -n option:
# echo "nameXnameXnameXname" | xargs -dX -n2 name name name name
Read stdin, pass the formatted arguments to the command.
Suppose there is a command sk.sh and a file arg.txt that stores parameters:
#!/bin/bash #sk.sh命令内容,打印出所有参数。 echo $*
Contents of arg.txt:
# cat arg.txt aaa bbb ccc
One option of xargs is -I. Use -I to specify a replacement string {}. This string will be replaced when xargs expands it. When -I is used in combination with xargs, the command will be executed once for each parameter.
# 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 number of 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 applications of xargs
If you have a file containing many URLs you want to download, you can use xargs to download all links:
# cat url-list.txt | xargs wget -cOther extensions