1. xargs
args是arguments 参数的意思。
xargs 的作用就是产生某个命令参数的意思。
xargs可以读入stdin 数据,以空格或断行分隔,将stdin 分隔成 arguments。
它的作用是将参数列表转换成小块分段传递给其他命令,以避免参数列表过长的问题。
对于不支持管道的命令,可以通过xargs提供该命令的引用stdin来使用。
2. xargs用法
xargs [-0epn] command
-0 : 如果输入的stdin包含特殊符号,如 `, \, 空格等字符,这个参数可以将它还原成一般字符。
-e : 是EOF(end of file) 的意思,后面可以接字符串,当xargs解析到这个字符串时,停止继续工作。
-p : 在执行每个命令参数时,都会询问用户意思
-n : 后面接次数,每次command命令执行时,要使用几个参数的意思。
3. xargs返回值
0 if it succeeds
123 if any invocation of the command exited with status 1-125
124 if the command exited with status 255
125 if the command is killed by a signal
126 if the command cannot be run
127 if the command is not found
1 if some other error occurred.
123 if any invocation of the command exited with status 1-125
124 if the command exited with status 255
125 if the command is killed by a signal
126 if the command cannot be run
127 if the command is not found
1 if some other error occurred.
4. 使用
将find到的数据,通过file检测文件类型。
$ find . * | file
Usage: file [-bcikLnNsvz] [-f namefile] [-F separator] [-m magicfiles] file...
file -C -m magicfiles
Try `file --help' for more information.
通过xargs
$ find . * | xargs file
.: directory
./test: directory
./ui.cpp: ASCII C program text
文件参数过长例
rm `find /path -type f`
如果path目录下文件过多就会因为“参数列表过长”而报错无法执行。但改用xargs以后,问题即获解决
find /path -type f -print0 | xargs -0 rm
grep bar `find . -name "*.foo"`
xargs可以使用下面方式
grep bar `find . -name "*.foo"`
find . -name "*.foo" -print0 | xargs -0 grep bar