之前一直没用过这个命令,今天用到了,简单记录一下
xargs命令的作用是将参数列表转换成小块分段传递给其他命令,以避免参数列表过长的问题
例如,在家目录下新建一tmp文件,然后查看其内容时两者的区别
kai@kai-pc:~$ cat tmp
1 2 3
4 5 6
7 8 9
kai@kai-pc:~$ cat tmp|xargs
1 2 3 4 5 6 7 8 9
加入 -n 传参可将其多行输出
kai@kai-pc:~$ cat tmp|xargs -n 1
1
2
3
4
5
6
7
8
9
删除某个字符来做分隔
kai@kai-pc:~$ cat tmp|xargs -d 5
1 2 3
4 6
7 8 9
与find命令结合使用
kai@kai-pc:~/tmp$ ls
1.c 1.cpp 2.c 2.cpp 3.c 3.cpp
kai@kai-pc:~/tmp$ find -name '*.c'|xargs rm
kai@kai-pc:~/tmp$ ls
1.cpp 2.cpp 3.cpp
统计当前目录及其子目录下的cpp文件有多少行,并按行数降序排列
find -name '*.cpp' -print0|xargs -0 wc -l|sort -nr
只是统计出了该文件一共多少行,无法对空行注释行有效行进行分类
找到哪个文件中有所要想找的内容
find *.cpp -type f -print|xargs grep "内容"