一、命令简介

  xargs(英文全拼: extended arguments)是给命令传递参数的一个过滤器,也是组合多个命令的一个工具。xargs 可以将管道或标准输入(stdin)数据转换成命令行参数,也能够从文件的输出中读取数据。xargs 也可以将单行或多行文本输入转换为其他格式,例如多行变单行,单行变多行。xargs 默认的命令是 echo,这意味着通过管道传递给 xargs 的输入将会包含换行和空白,不过通过 xargs 的处理,换行和空白将被空格取代。xargs 是一个强有力的命令,它能够捕获一个命令的输出,然后传递给另外一个命令。

二、使用示例

1、查看xargs命令版本

[root@test3 ~]# xargs --version
xargs (GNU findutils) 4.5.11
Copyright © 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Eric B. Decker, James Youngman, and Kevin Dalley.

2、多行变单行

[root@test3 ~]# cat test.txt |xargs
1 this is a test! 2 this is the second number. 3 are you there?
[root@test3 ~]# cat test.txt
1 this is a test!
2 this is the second number.
3 are you there?
[root@test3 ~]# cat test.txt |xargs
1 this is a test! 2 this is the second number. 3 are you there?

3、单行转多行

[root@test3 ~]# cat 1.txt
hello, where are you? this is a test!
[root@test3 ~]# cat 1.txt |xargs -n3
hello, where are
you? this is
a test!

4、按照指定分隔符分割字符串

[root@test3 ~]# echo “wuhs@sunr@sunr@wuhs@wuzy” |xargs -d@
wuhs sunr sunr wuhs wuzy

[root@test3 ~]# echo “wuhs@sunr@sunr@wuhs@wuzy” |xargs -d@ -n2
wuhs sunr
sunr wuhs
wuzy

5、删除查找到的文件

[root@test3 ~]# find . -name test.txt
./test.txt
[root@test3 ~]# find . -name test.txt |xargs rm -rf
[root@test3 ~]# find . -name test.txt

6、将查找到的文件打包

[root@test3 ~]# find . -type f -name “*.cfg” |xargs tar -zcvf cfg.tar.gz
./original-ks.cfg
./anaconda-ks.cfg

7、批量复制文件到指定目录

[root@test3 ~]# find . -type f -name “.cfg" |xargs -n1 -I {} cp {} /home
[root@test3 ~]# find . -type f -name "
.txt” |xargs -n1 -I {} cp {} /home
[root@test3 ~]# ll /home/
total 20
-rw-r–r-- 1 root root 38 Jul 21 16:34 1.txt
-rw-r–r-- 1 root root 64 Jul 21 16:34 2.txt
-rw------- 1 root root 2762 Jul 21 16:34 anaconda-ks.cfg
drwx------ 7 es es 184 Jun 22 15:47 es
drwx------ 7 gzgk gzgk 198 Jul 20 14:06 gzgk
-rw------- 1 root root 2042 Jul 21 16:34 original-ks.cfg
drwx------. 15 wuhs wuhs 4096 Dec 16 2020 wuhs

8、删除文件名包含空格的文件

find命令有一个特别的参数-print0,指定输出的文件列表以null分隔。然后,xargs命令的-0参数表示用null当作分隔符。

[root@test3 ~]# touch “abc d”
[root@test3 ~]# ll
total 20
-rw-r–r-- 1 root root 38 Jul 21 16:01 1.txt
-rw-r–r-- 1 root root 64 Jul 21 15:57 2.txt
-rw-r–r-- 1 root root 0 Jul 21 16:43 abc d
-rw-------. 1 root root 2762 Dec 16 2020 anaconda-ks.cfg
-rw-r–r-- 1 root root 1513 Jul 21 16:29 cfg.tar.gz
-rw-------. 1 root root 2042 Dec 16 2020 original-ks.cfg
[root@test3 ~]# find . -type f -name abc* |xargs -0 rm
rm: cannot remove ‘./abc d\n’: No such file or directory
[root@test3 ~]# find . -type f -name abc* -print0 |xargs -0 rm
[root@test3 ~]# ll
total 20
-rw-r–r-- 1 root root 38 Jul 21 16:01 1.txt
-rw-r–r-- 1 root root 64 Jul 21 15:57 2.txt
-rw-------. 1 root root 2762 Dec 16 2020 anaconda-ks.cfg
-rw-r–r-- 1 root root 1513 Jul 21 16:29 cfg.tar.gz
-rw-------. 1 root root 2042 Dec 16 2020 original-ks.cfg

9、打印待执行命令确认后执行

使用-p参数打印待执行的命令,输入y或yes回车确认后执行,直接回车不执行命令。

[root@test3 ~]# find . -type f -name 1.txt |xargs -p rm
rm ./1.txt ?..
[root@test3 ~]# find . -type f -name 2.txt |xargs -p rm
rm ./2.txt ?..yes
[root@test3 ~]# ll
total 16
-rw-r–r-- 1 root root 38 Jul 21 16:01 1.txt
-rw-------. 1 root root 2762 Dec 16 2020 anaconda-ks.cfg
-rw-r–r-- 1 root root 1513 Jul 21 16:29 cfg.tar.gz
-rw-------. 1 root root 2042 Dec 16 2020 original-ks.cfg

10、打印最终执行命令但不做确认

-t参数则是打印出最终要执行的命令,然后直接执行,不需要用户确认。

[root@test3 ~]# find . -type f -name 1.txt |xargs -t rm
rm ./1.txt

11、批量创建目录

[root@test3 ~]# echo “one two there” |xargs mkdir
[root@test3 ~]# ll
total 16
-rw-r–r-- 1 root root 64 Jul 21 16:53 2.txt
-rw-------. 1 root root 2762 Dec 16 2020 anaconda-ks.cfg
-rw-r–r-- 1 root root 1513 Jul 21 16:29 cfg.tar.gz
drwxr-xr-x 2 root root 6 Jul 21 16:58 one
-rw-------. 1 root root 2042 Dec 16 2020 original-ks.cfg
drwxr-xr-x 2 root root 6 Jul 21 16:58 there
drwxr-xr-x 2 root root 6 Jul 21 16:58 two

12、将命令参数传递给多个命令

使用-I参数将命令行参数传给多个命令,-I file指定每一项命令行参数的替代字符串。选是将每一行参数传递给echo和mkdir命令,即显示并创建目录。

[root@test3 ~]# cat 3.txt
one
two
three
[root@test3 ~]# cat 3.txt |xargs -I file sh -c ‘echo file;mkdir file’
one
two
three

三、使用语法及参数说明

1、使用语法

command | xargs [选项] command

2、参数说明

参数参数说明
-0, --null项目之间用null分隔,而不是空格。禁用引号和反斜杠处理
-a, --arg-file=FILE从文件中读入作为 stdin
-d, --delimiter=CHARACTER自定义分隔符
-E ENDxargs分析到含有flag这个标志的时候就停止
-e [END], --eof[=END]注意有的时候可能会是-E,flag必须是一个以空格分隔的标志,当xargs分析到含有flag这个标志的时候就停止。
–help打印帮助选项
-I R将xargs的每项名称,一般是一行一行赋值给 {},可以用 {} 代替
-i,–replace=[R]将xargs的每项名称,一般是一行一行赋值给 {},可以用 {} 代替
-L,-l, --max-lines=MAX-LINES如果标准输入包含多行,-L参数指定多少行作为一个命令行参数。
-l从标准输入一次读取 num 行送给 command 命令。
-n, --max-args=MAX-ARGS后面加次数,表示命令在执行的时候一次用的argument的个数,默认是用所有的
-P, --max-procs=MAX-PROCSxargs默认只用一个进程执行命令。如果命令要执行多次,必须等上一次执行完,才能执行下一次,0表示不限制进程数。
-p, --interactive当每次执行一个argument的时候询问一次用户。
–process-slot-var=VAR在子进程中设置环境变量var过程
-r, --no-run-if-empty当xargs的输入为空的时候则停止xargs,不用再去执行了。
-s, --max-chars=MAX-CHARS命令行的最大字符数,指的是 xargs 后面那个命令的最大命令行字符数。
–show-limits显示命令行长度的限制
-t, --verbose在执行之前详细打印命令
–version打印版本号信息
-x, --exitexit的意思,主要是配合-s使用
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐