xargs
Read data from standard input (stdin) for processing
- Data is separated by a space
- Can be processed one or more times according to the parameters, the default processing command is
/bin/echo
- Empty lines are not processed and are ignored
- When a command status of 255 is encountered, Xargs stops immediately, such as when an error occurs.
Let's take a look at the xargs
parameters to choose from.
2.options
-A file
: Reads data from file
$cat 1.txtaaa bbb ccc ddda b$xargs -a 1.txt aaa bbb ccc ddd a b
-0
: When a special character is entered, it is treated as a general character, such as "" and a space
"// " | xargs//$echo "// " | xargs -0//
-D
: Specify delimiter
$cat 1.txtaaa bbb ccc ddda b$cat 1.txt | xargs -d ‘c‘aaa bbb ddda b
-E eof-str
: Specifies that the end flag is eof-str
, xargs
processing to this flag will stop
$xargs -E ‘ddd‘ -a 1.txtaaa bbb ccc$xargs -E ‘dd‘ -a 1.txtaaa bbb ccc ddd a b$cat 1.txt | xargs -E ‘ddd‘aaa bbb ccc
-
-i replace-str
: Replace each line of input input with replace-str
< Span class= "hljs-variable" > $cat 1.txtaaa BBB CCC Ddda b $cat Span class= "Hljs-number" >1.txt | Xargs-t-I {} echo {} >> 1.txt echo AAA BBB CCC ddd echo a b $cat 1.txtaaa BBB CCC Ddda BAAA BBB CCC Ddda b
-
-I
: equals -i{}
$cat 1.txtaaa BBB CCC Ddda b $cat 1.txt | xargs-t-I Span class= "hljs-built_in" >echo {} >> 1.txtecho AAA BBB CCC ddd echo a b $cat 1.txtaaa BBB CCC Ddda BAAA BBB CCC Ddda b
-L max-lines
: Each read max-line
line input is referred to the xargs
processing
$cat 1.txtaaa bbb ccc ddda b$cat 1.txt |xargs -L 2aaa bbb ccc ddd a b$cat 1.txt |xargs -L 1aaa bbb ccc ddda b
-L
: Similar to -L
, the difference is that -l
you can not specify a parameter, the default is 1.
-N Max-args
: Each row executes max-args
an input, and the default is to perform all
$cat 1.txt | xargs -n 2 aaa bbbccc ddda b
-
-P
: interactive mode, asking whether to execute before executing
$cat 1.txt | xargs-p/bin/echo AAA BBB CCC ddd a B?... yaaa BBB CCC DDD A B $cat 1.txt | xargs-p/bin/echo AAA BBB CCC DDD a b?... n
-
-R
: No input stops execution, defaults to at least 1 times
$ echo " "|xargs-t mvmv m v:missing file operandtry ' mv--help ' for more information.$ echo "| Xargs-t-r mv #直接退出
-
-s max-chars
: xargs
maximum length of each execution of the command (with spaces)
$ cat 1.txtaaa BBB CCC ddd a b$ cat 1.txt | Xargs-t -s 30/bin/echo AAA BBB CCC DDD a B AAA BBB CCC DDD a b #length (/bin/echo AAA BBB CCC ddd a B) =30 $cat 1.txt |xargs-t -s 14/ Bin/echo aaa aaa/bin/echo BBB bbb/bin/echo CCC ccc/bin/echo ddd ddd/bin/echo a B a b< Span class= "Hljs-comment" > #length (/bin/echo AAA) =14
-T
: Prints the executed command first, then executes the
1.txt | xargs -t/bin/echo aaa bbb ccc ddd a baaa bbb ccc ddd a b
-X
: xargs
stops execution when the executed command length is greater than -s max-char
-P Max-procs
: Modify the number of threads, which defaults to single thread. max-procs
is 0 o'clock,as many processes as possible
3. Find and Xargs
When a matching file is processed using the-EXEC option of the Find command, the Find command passes all matching files to exec execution. However, some systems have a limit on the length of the command that can be passed to exec so that an overflow error occurs after the Find command runs for a few minutes. The error message is usually "parameter column too Long" or "parameter column overflow". This is where the Xargs command is used, especially with the Find command. The find command passes the matched file to the Xargs command, and the Xargs command takes only a subset of the files at a time instead of all, unlike the-exec option. This allows it to first process a portion of the file that was first fetched, then the next batch, and so on.
In some systems, the use of the-EXEC option initiates a corresponding process for processing each matching file, not all of the matching files are executed once as parameters, so that in some cases, there are too many processes and degraded system performance, so the efficiency is not high , while using the Xargs command, there is only one process. In addition, when using the Xargs command, whether to get all the parameters at once or to get the parameters in batches, and the number of parameters to get each time will be determined according to the command's options and the corresponding tunable parameters in the system kernel.
The pipeline is to pass the output of one command as input to another command, for example: command1 | command2
but Command2 only takes the contents of the Command1 output as input parameters. find . -name "install.log" -print
print out is install.log This string, if only use the pipeline, then Command2 can use only install.log this string, cannot treat it as a file.
Of course this command2 except Xargs. Xargs is written to be able to manipulate the files found by find. It can send a string from the pipeline as a file to the subsequent command execution.
4 Example
"1.txt" | cat./1.txt#显示从管道传来的内容,仅仅作为字符串来处理
$find . -name "1.txt" | xargs cataaa bbb ccc ddda b#将管道传来的内容作为文件,交给cat执行。也就是说,该命令执行的是如果存在1.txt,那么就打印出这个文件的内容。
$find . -perm -7 -print | xargs chmod o-w#在当前目录下查找所有用户具有读、写和执行权限的文件,并收回相应的写权限
find . -type f -print | xargs file#查找系统中的每一个普通文件,然后使用xargs命令来测试它们分别属于哪类文件
$find ~ -name ‘*.log‘ -print0 | xargs -i -0 rm -f {}#尝试用rm 删除太多的文件,你可能得到一个错误信息:/bin/rm Argument list too long. 用xargs 去避免这个问题
$find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz#查找所有的jpg 文件,并且压缩它
| xargs -n 1 -i cp {} /external-hard-drive/directory#拷贝所有的图片文件到一个外部的硬盘驱动
The Xargs of Linux