如何为每一个给定的输入行使xargs执行一次命令? 它的默认行为是将行块化并执行一次命令,将多行传递给每一个实例。 less
来自http://en.wikipedia.org/wiki/Xargs : spa
find / path -type f -print0 | xargs -0 rm 命令行
在此示例中,查找使用长文件名列表输入xargs。 而后xargs将此列表拆分为子列表,并为每一个子列表调用rm一次。 这比这个功能相同的版本更有效: code
find / path -type f -exec rm'{}'\\; ip
我知道find有“exec”标志。 我只是引用另外一个资源的说明性示例。 ci
另外一种选择...... 资源
find /path -type f | while read ln; do echo "processing $ln"; done
只有在输入中没有空格时,如下内容才有效: get
xargs -L 1 xargs --max-lines=1 # synonym for the -L option
从手册页: input
-L max-lines Use at most max-lines nonblank input lines per command line. Trailing blanks cause an input line to be logically continued on the next input line. Implies -x.
您能够分别使用--max-lines或--max-args标志限制行数或参数(若是每一个参数之间有空格)。 it
-L max-lines Use at most max-lines nonblank input lines per command line. Trailing blanks cause an input line to be logically continued on the next input line. Implies -x. --max-lines[=max-lines], -l[max-lines] Synonym for the -L option. Unlike -L, the max-lines argument is optional. If max-args is not specified, it defaults to one. The -l option is deprecated since the POSIX standard specifies -L instead. --max-args=max-args, -n max-args Use at most max-args arguments per command line. Fewer than max-args arguments will be used if the size (see the -s option) is exceeded, unless the -x option is given, in which case xargs will exit.
在您的示例中,将find的输出传递给xargs的点是find的-exec选项的标准行为是为每一个找到的文件执行一次命令。 若是你正在使用find,而且你想要它的标准行为,那么答案很简单 - 不要使用xargs开头。
若是你想为来自find
每一行(即结果)运行命令,那么你须要xargs
用于什么?
尝试:
find
path -type f -exec
your-command {} \\;
文字{}
被文件名和文字\\;
取代\\;
是须要find
要知道,自定义命令到此为止。
(在您的问题编辑以后,澄清您了解-exec
)
来自man xargs
:
-L max-lines
每一个命令行最多使用max-lines nonblank输入行。 尾随空白致使输入行在下一个输入行上逻辑上继续。 意味着-x。
请注意,若是您使用xargs
以空格结尾的文件名会致使您遇到麻烦:
$ mkdir /tmp/bax; cd /tmp/bax $ touch a\ b c\ c $ find . -type f -print | xargs -L1 wc -l 0 ./c 0 ./c 0 total 0 ./b wc: ./a: No such file or directory
所以,若是您不关心-exec
选项,最好使用-print0
和-0
:
$ find . -type f -print0 | xargs -0L1 wc -l 0 ./c 0 ./c 0 ./b 0 ./a