有些命令只能以命令行参数的形式接受数据,无法通过stdin接受数据流,
此时采用命令xargs,将标准输入数据转换成命令行参数,紧跟在管道符后面
模式
command | xargs
将多行输入转换成单行输出
cat test.txt
1 2 3 4 5 6
7 8 9 10
11 12
cat test.txt | xargs
1 2 3 4 5 6 7 8 9 10 11 12
单行输入转换成多行输出,-n后加数字表示每行有几个数
cat test.txt | xargs -n 3
1 2 3
4 5 6
7 8 9
11 12 13
-d:为输入指定一个特定的定界符
echo "splitXsplitXsplitXsplit" | xargs -d X
split split split split
结合-n选项
echo "splitXsplitXsplitXsplit" | xargs -d X -n 2
split split
split split
结合find使用xargs
找出所有.txt文件,用xargs将这些文件删除
find . -type f -name "*.txt" -print0 | xargs -0 rm -rf
**统计源代码目录中所有c文件的行数
find source_code_dir_path -type f -name "*.c" -print0 | xargs -0 wc -l
网友评论