1 将多行输入转化为单行输出:
[root@localhost test]# cat text.txt123
[root@localhost test]# cat text.txt | xargs1 2 3
2将单行输入转成多行输出
[root@localhost test]# cat example.txt1 2 3 4 5 6 7 8 9 01 2 3 4 5 6 7 8 9 0[root@localhost test]# cat example.txt | xargs -n 4 -n 每行n个参数1 2 3 45 6 7 89 01 2 34 5 6 78 9 0
3 -d 以什么分隔
[root@localhost test]# echo "hello lufubo my name is haha" | xargs -d mhello lufubo y na e is haha
[root@localhost test]# echo "hello lufubo my name is haha" | xargs -d m -n 2
hello lufubo y na
e is haha
4 读取stdin,将格式化参数传递给命令
[root@localhost test]# cat test.sh#! /bin/shecho $*'#'
[root@localhost test]# cat args.txtarg1arg2arg3
[root@localhost test]# cat args.txt | xargs -n 1 ./test.sharg1#arg2#arg3#
[root@localhost test]# cat args.txt | xargs -n 2 ./test.sharg1 arg2#arg3#
[root@localhost test]# cat args.txt | xargs -I {} ./test.sh -p {} -l -I 替换-p arg1 -l#-p arg2 -l#-p arg3 -l#
5 find 与 xargs的结合使用
find . -type f -name "*.tmp" -print0 | xargs -0 rm -f
删除所有.tmp文件,xargs -0 表示以\0作为输入定界符
find . -type f -name "*.c" -print0 | xargs -0 wc -l
统计当前目录下所有.c文件的总行数