让 xargs 对每一行输入执行一次命令 [英] Make xargs execute the command once for each line of input

查看:31
本文介绍了让 xargs 对每一行输入执行一次命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让 xargs 为给定的每一行输入只执行一次命令?默认行为是将行分块并执行一次命令,将多行传递给每个实例.

How can I make xargs execute the command exactly once for each line of input given? It's default behavior is to chunk the lines and execute the command once, passing multiple lines to each instance.

来自http://en.wikipedia.org/wiki/Xargs:

find/path -type f -print0 |xargs -0 rm

find /path -type f -print0 | xargs -0 rm

在本例中, find 将 xargs 的输入提供给一长串文件名.xargs 然后将此列表拆分为子列表并为每个子列表调用 rm 一次.这比这个功能等效的版本更有效:

In this example, find feeds the input of xargs with a long list of file names. xargs then splits this list into sublists and calls rm once for every sublist. This is more efficient than this functionally equivalent version:

find/path -type f -exec rm '{}' \;

find /path -type f -exec rm '{}' \;

我知道 find 有exec"标志.我只是从另一个资源中引用了一个说明性的例子.

I know that find has the "exec" flag. I am just quoting an illustrative example from another resource.

推荐答案

以下内容仅在您的输入中没有空格时有效:

The following will only work if you do not have spaces in your input:

xargs -L 1
xargs --max-lines=1 # synonym for the -L option

来自手册页:

-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.

这篇关于让 xargs 对每一行输入执行一次命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆