? ? Xargs is the abbreviation for execute arguments, and the main function is to read the content from the standard input and pass it to the command it wants to assist and execute as a parameter to assist the command .
Basic syntax
xargs [选项] [命令]
The common options are as follows:
Options |
Description |
--null,-0 |
Allow NULL as a delimiter |
-A file |
Reading items from a file rather than standard input |
-D Delim |
Specify delimiter |
-P,--Interactive |
Swap mode, in which the user is required to confirm whether to execute the command |
-N Max-args |
Used to specify how many arguments each pass to the following command |
-E Eof-str |
Specify the command end identifier |
-E Eof-str |
Same-e Eof-str |
-I {REPLACE-STR} |
Replace the REPLACE-STR with the name read in from the standard input |
-I {REPLACE-STR} |
function with-i {REPLACE-STR} |
The difference from the pipe
Let's take a look at two examples:
[[email protected] ~]# cat test.txtthis is test text.[[email protected] ~]# echo test.txt | cattest.txt[[email protected] ~]# echo test.txt | xargs catthis is test text.
[[email protected] ~]# echo "--help" | cat--help[[email protected] ~]# echo "--help" | xargs cat用法:cat [选项]... [文件]...将[文件]或标准输入组合输出到标准输出。 -A, --show-all 等于-vET -b, --number-nonblank 对非空输出行编号 -e 等于-vE -E, --show-ends 在每行结束处显示"$" -n, --number 对输出的所有行编号 -s, --squeeze-blank 不输出多行空行 -t 与-vT 等价 -T, --show-tabs 将跳格字符显示为^I -u (被忽略) -v, --show-nonprinting 使用^ 和M- 引用,除了LFD和 TAB 之外
From the above example, we can summarize the following conclusions:
- Pipelines can implement the standard input of the previous standard output as a subsequent command
- The pipeline cannot implement the command parameters that use the preceding standard output as the following command
Many of the commands in Linux can be obtained from the command line parameters, then read from the standard input, and finally display the results through the standard output. If you want to implement the command arguments that make the previous standard output the command that follows, you need to use the command xargs
Example usage
1,-D option
[[email protected] ~]# echo ‘2018-08-11‘ | xargs echo2018-08-11[[email protected] ~]# echo ‘2018-08-11‘ | xargs -d ‘-‘ echo2018 08 11
2,-P option
[[email protected] ~]# echo ‘2018-08-11‘ | xargs -d ‘-‘ -p echoecho 2018 08 11 ?...y2018 08 11
3,-N option
[[email protected] ~]# echo ‘2018-08-11‘ | xargs -d ‘-‘ -n 1 echo20180811
The above example indicates that Xargs only passes one parameter to the subsequent command from the standard input at a time, and the separated argument is 3, so it is displayed as 3 rows.
4,-e option
[[email protected] ~]# echo ‘2018 08 11‘ | xargs -E ‘08‘ echo2018[[email protected] ~]# echo ‘2018-08-11‘ | xargs -d ‘-‘ -E ‘08‘ echo2018 08 11
When xargs resolves multiple command-line arguments, it terminates and exits if the command-line arguments specified by-e are searched. Note that the-e parameter is valid only if you do not specify-D
5,-0 options
-0 option to use '/' as a delimiter, usually used in conjunction with find
[[email protected] test]# find . -name ‘*.txt‘./1.txt./2.txt./3.txt./4.txt./test.txt# 默认情况find的结果中每条记录中会添加一个换行符[[email protected] test]# find . -name ‘*.txt‘ -print0./1.txt./2.txt./3.txt./4.txt./test.txt# print0表示显示的输出结果后面增加‘\0‘而不是换行符[[email protected] test]# find . -name ‘*.txt‘ -print0 | xargs -0 echo./1.txt ./2.txt ./3.txt ./4.txt ./test.txt[[email protected] test]# find . -name ‘*.txt‘ -print0 | xargs -d ‘\0‘ echo./1.txt ./2.txt ./3.txt ./4.txt ./test.txt# xargs中的-0和-d ‘\0‘表示从标准输入读取内容以‘\0‘进行分隔,因find的结果中是以‘\0‘进行分隔,所以xargs使用‘\0‘将find的结果分隔之后得到5个参数,而且参数中间有空格做为间隔。
6,-i option
[[email protected] test]# find ./ -name ‘*.txt‘ | xargs -i cp {} /tmp/temp/[[email protected] test]# ll /tmp/temp/总用量 20-rw-r--r-- 1 root root 6 8月 12 00:10 1.txt-rw-r--r-- 1 root root 6 8月 12 00:10 2.txt-rw-r--r-- 1 root root 6 8月 12 00:10 3.txt-rw-r--r-- 1 root root 6 8月 12 00:10 4.txt-rw-r--r-- 1 root root 19 8月 12 00:10 test.txt
7,-i option
[[email protected] test]# find ./ -name ‘*.txt‘ | xargs -I {} -i cp {} /tmp/temp/[[email protected] test]# ll /tmp/temp/总用量 20-rw-r--r-- 1 root root 6 8月 12 00:14 1.txt-rw-r--r-- 1 root root 6 8月 12 00:14 2.txt-rw-r--r-- 1 root root 6 8月 12 00:14 3.txt-rw-r--r-- 1 root root 6 8月 12 00:14 4.txt-rw-r--r-- 1 root root 19 8月 12 00:14 test.txt
The differences between the-I and-I options are as follows:
- -I: The content in standard output before the pipeline can be replaced directly with {}
- -I: Need to pre-refer to the replacement character
This article is posted on the subscription number, such as your friends like my article, you can also follow my subscription number: Woaitest, or scan the following QR code to add attention:
Linux Basic Tutorial 42-xargs command