xargs build and execute command lines from standard input
从标准输入建立和执行命令行(从标准输入转换为命令的参数),可将多行或一行转换为特殊格式,默认命令是echo ,默认参数是空格。
常用选项:
-n 多行输出
-d 制定分隔符
实例1 多行文件输入,单行输出
[root@www ~]# cat test.txt1 2 3 45 6 7 81 4 2 32 5 6 7[root@www ~]# xargs < test.txt1 2 3 4 5 6 7 8 1 4 2 3 2 5 6 7[root@www ~]# cat test.txt | xargs1 2 3 4 5 6 7 8 1 4 2 3 2 5 6 7
实例2 将每行4列改为3列
[root@www ~]# cat test.txt1 2 3 4 56 7 8 9 101 5 6 8 92 3 5 6 8[root@www ~]# cat test.txt | xargs -n31 2 34 5 67 8 910 1 56 8 92 3 56 8[root@www ~]#
实例3 按某个字符进行分割
[root@www ~]# echo "this1is1a1test" | xargs -d1this is a test
实例4
当删除多个文件时,可能会报/bin/rm Argument list too long,可以使用xargs来解决此问题。
[root@www ~]# find ./ -type f -name '*.sh' #使用find + 路径+type指定文件类型+ name 指定文件名称 来查找处所有*.sh文件./3.sh./4.sh./5.sh./2.sh./1.sh[root@www ~]# find ./ -type f -name '*.sh' | xargs rm -f #find查找出的文件转换为一行并作为rm的参数执行。z
实例5
[root@www ~]# find ./ -type f -name '*.sh' -exec rm {} \;[root@www ~]# lsanaconda-ks.cfg install.log install.log.syslog test.txt[root@www ~]#