让 xargs 对每行输入执行一次命令
如何使 xargs 对给定的每一行输入精确执行一次命令? 它的默认行为是对行进行分块并执行一次命令,将多行传递给每个实例。
来自http://en.wikipedia.org/wiki/Xargs:
查找 /path -type f -print0 | xargs -0 rm
在此示例中,find 向 xargs 的输入提供一长串文件名。 然后 xargs 将此列表拆分为子列表,并为每个子列表调用 rm 一次。 这比功能等效的版本更有效:
查找 /path -type f -exec rm '{}' \;
我知道 find 有“exec”标志。 我只是引用另一个资源中的一个说明性示例。
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.
From http://en.wikipedia.org/wiki/Xargs:
find /path -type f -print0 | xargs -0 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 '{}' \;
I know that find has the "exec" flag. I am just quoting an illustrative example from another resource.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
仅当您的输入中没有空格时,以下内容才有效:
从手册页:
The following will only work if you do not have spaces in your input:
from the man page:
在我看来,这一页上所有现有的答案都是错误的,包括标记为正确的答案。 这是因为这个问题的措辞含糊不清。
摘要: 如果您想执行命令“每行输入只执行一次”,将整行(不带换行符)作为单个参数传递给命令,那么这是最好的 UNIX 兼容方法:
如果您使用 GNU xargs 并且不需要与所有其他 UNIX(FreeBSD、Mac OS X 等)兼容,那么您可以使用GNU 特定选项
-d
:现在进行详细解释……
使用 xargs 时需要考虑两个问题:
为了测试 xargs 的行为,我们需要一个实用程序来显示它被执行了多少次以及有多少个参数。 我不知道是否有一个标准实用程序可以做到这一点,但我们可以很容易地在 bash 中对其进行编码:
假设您将其另存为
show
在当前目录中并使其可执行,以下是如何执行的它有效:现在,如果原来的问题确实是关于上面的第 2 点(正如我认为的那样,在读了几次之后),并且应该像这样阅读(更改为粗体):
那么答案是
-n 1
。让我们比较一下 xargs 的默认行为,它将输入围绕空格分割并尽可能少地调用命令:
及其与
-n 1
的行为:另一方面,如果原始问题是关于第1点.输入分割,它应该这样读(很多人来到这里似乎认为是这样,或者混淆了两个问题):
那么答案就更微妙了。
人们可能会认为
-L 1
可能会有所帮助,但事实证明它不会改变参数解析。 它只对每个输入行执行一次命令,并使用与该输入行上的参数一样多的参数:不仅如此,如果一行以空格结尾,它会附加到下一行:
显然,
-L< /code> 并不是要改变 xargs 将输入拆分为参数的方式。
以跨平台方式(不包括 GNU 扩展)执行此操作的唯一参数是
-0
,它将输入围绕 NUL 字节进行分割。然后,只需在 tr 的帮助下将换行符转换为 NUL 即可:
现在参数解析看起来没问题,包括尾随空格。
最后,如果将此技术与 -n 1 结合起来,无论您有什么输入,每个输入行都会执行一个命令,这可能是查看原始问题的另一种方式(可能是最常见的)直观,给定标题):
如上所述,如果您使用 GNU
one \ntwo\nthree and four' | xargs -L 1 ./show -> "one" "two" -> "three" "and" "four"xargs
,您可以将tr
替换为 GNU 特定选项-d
>:显然,
-L< /code> 并不是要改变 xargs 将输入拆分为参数的方式。
以跨平台方式(不包括 GNU 扩展)执行此操作的唯一参数是
-0
,它将输入围绕 NUL 字节进行分割。然后,只需在 tr 的帮助下将换行符转换为 NUL 即可:
现在参数解析看起来没问题,包括尾随空格。
最后,如果将此技术与 -n 1 结合起来,无论您有什么输入,每个输入行都会执行一个命令,这可能是查看原始问题的另一种方式(可能是最常见的)直观,给定标题):
如上所述,如果您使用 GNU
one\ntwo\nthree and four' | xargs -L 1 ./show -> "one" -> "two" -> "three" "and" "four"xargs
,您可以将tr
替换为 GNU 特定选项-d
>:不仅如此,如果一行以空格结尾,它会附加到下一行:
显然,
-L< /code> 并不是要改变 xargs 将输入拆分为参数的方式。
以跨平台方式(不包括 GNU 扩展)执行此操作的唯一参数是
-0
,它将输入围绕 NUL 字节进行分割。然后,只需在 tr 的帮助下将换行符转换为 NUL 即可:
现在参数解析看起来没问题,包括尾随空格。
最后,如果将此技术与 -n 1 结合起来,无论您有什么输入,每个输入行都会执行一个命令,这可能是查看原始问题的另一种方式(可能是最常见的)直观,给定标题):
如上所述,如果您使用 GNU
one \ntwo\nthree and four' | tr '\n' '\0' | xargs -0 -n1 ./show -> "one " -> "two" -> "three and four"xargs
,您可以将tr
替换为 GNU 特定选项-d
>:如上所述,如果您使用 GNU
one\ntwo\nthree and four' | xargs -L 1 ./show -> "one" -> "two" -> "three" "and" "four"xargs
,您可以将tr
替换为 GNU 特定选项-d
>:不仅如此,如果一行以空格结尾,它会附加到下一行:
显然,
-L< /code> 并不是要改变 xargs 将输入拆分为参数的方式。
以跨平台方式(不包括 GNU 扩展)执行此操作的唯一参数是
-0
,它将输入围绕 NUL 字节进行分割。然后,只需在 tr 的帮助下将换行符转换为 NUL 即可:
现在参数解析看起来没问题,包括尾随空格。
最后,如果将此技术与 -n 1 结合起来,无论您有什么输入,每个输入行都会执行一个命令,这可能是查看原始问题的另一种方式(可能是最常见的)直观,给定标题):
如上所述,如果您使用 GNU
one \ntwo\nthree and four' | xargs -L 1 ./show -> "one" "two" -> "three" "and" "four"xargs
,您可以将tr
替换为 GNU 特定选项-d
>:显然,
-L< /code> 并不是要改变 xargs 将输入拆分为参数的方式。
以跨平台方式(不包括 GNU 扩展)执行此操作的唯一参数是
-0
,它将输入围绕 NUL 字节进行分割。然后,只需在 tr 的帮助下将换行符转换为 NUL 即可:
现在参数解析看起来没问题,包括尾随空格。
最后,如果将此技术与 -n 1 结合起来,无论您有什么输入,每个输入行都会执行一个命令,这可能是查看原始问题的另一种方式(可能是最常见的)直观,给定标题):
如上所述,如果您使用 GNU
one\ntwo\nthree and four' | xargs -L 1 ./show -> "one" -> "two" -> "three" "and" "four"xargs
,您可以将tr
替换为 GNU 特定选项-d
>:不仅如此,如果一行以空格结尾,它会附加到下一行:
显然,
-L< /code> 并不是要改变 xargs 将输入拆分为参数的方式。
以跨平台方式(不包括 GNU 扩展)执行此操作的唯一参数是
-0
,它将输入围绕 NUL 字节进行分割。然后,只需在 tr 的帮助下将换行符转换为 NUL 即可:
现在参数解析看起来没问题,包括尾随空格。
最后,如果将此技术与 -n 1 结合起来,无论您有什么输入,每个输入行都会执行一个命令,这可能是查看原始问题的另一种方式(可能是最常见的)直观,给定标题):
如上所述,如果您使用 GNU
one \ntwo\nthree and four' | tr '\n' '\0' | xargs -0 ./show -> "one " "two" "three and four"xargs
,您可以将tr
替换为 GNU 特定选项-d
>:现在参数解析看起来没问题,包括尾随空格。
最后,如果将此技术与 -n 1 结合起来,无论您有什么输入,每个输入行都会执行一个命令,这可能是查看原始问题的另一种方式(可能是最常见的)直观,给定标题):
如上所述,如果您使用 GNU
one\ntwo\nthree and four' | xargs -L 1 ./show -> "one" -> "two" -> "three" "and" "four"xargs
,您可以将tr
替换为 GNU 特定选项-d
>:不仅如此,如果一行以空格结尾,它会附加到下一行:
显然,
-L< /code> 并不是要改变 xargs 将输入拆分为参数的方式。
以跨平台方式(不包括 GNU 扩展)执行此操作的唯一参数是
-0
,它将输入围绕 NUL 字节进行分割。然后,只需在 tr 的帮助下将换行符转换为 NUL 即可:
现在参数解析看起来没问题,包括尾随空格。
最后,如果将此技术与 -n 1 结合起来,无论您有什么输入,每个输入行都会执行一个命令,这可能是查看原始问题的另一种方式(可能是最常见的)直观,给定标题):
如上所述,如果您使用 GNU
one \ntwo\nthree and four' | xargs -L 1 ./show -> "one" "two" -> "three" "and" "four"xargs
,您可以将tr
替换为 GNU 特定选项-d
>:显然,
-L< /code> 并不是要改变 xargs 将输入拆分为参数的方式。
以跨平台方式(不包括 GNU 扩展)执行此操作的唯一参数是
-0
,它将输入围绕 NUL 字节进行分割。然后,只需在 tr 的帮助下将换行符转换为 NUL 即可:
现在参数解析看起来没问题,包括尾随空格。
最后,如果将此技术与 -n 1 结合起来,无论您有什么输入,每个输入行都会执行一个命令,这可能是查看原始问题的另一种方式(可能是最常见的)直观,给定标题):
如上所述,如果您使用 GNU
one\ntwo\nthree and four' | xargs -L 1 ./show -> "one" -> "two" -> "three" "and" "four"xargs
,您可以将tr
替换为 GNU 特定选项-d
>:不仅如此,如果一行以空格结尾,它会附加到下一行:
显然,
-L< /code> 并不是要改变 xargs 将输入拆分为参数的方式。
以跨平台方式(不包括 GNU 扩展)执行此操作的唯一参数是
-0
,它将输入围绕 NUL 字节进行分割。然后,只需在 tr 的帮助下将换行符转换为 NUL 即可:
现在参数解析看起来没问题,包括尾随空格。
最后,如果将此技术与 -n 1 结合起来,无论您有什么输入,每个输入行都会执行一个命令,这可能是查看原始问题的另一种方式(可能是最常见的)直观,给定标题):
如上所述,如果您使用 GNU
one \ntwo\nthree and four' | xargs -d\\n -n1 ./show -> "one " -> "two" -> "three and four" one\ntwo\nthree and four' | xargs -L 1 ./show -> "one" -> "two" -> "three" "and" "four"xargs
,您可以将tr
替换为 GNU 特定选项-d
>:不仅如此,如果一行以空格结尾,它会附加到下一行:
显然,
-L< /code> 并不是要改变 xargs 将输入拆分为参数的方式。
以跨平台方式(不包括 GNU 扩展)执行此操作的唯一参数是
-0
,它将输入围绕 NUL 字节进行分割。然后,只需在 tr 的帮助下将换行符转换为 NUL 即可:
现在参数解析看起来没问题,包括尾随空格。
最后,如果将此技术与 -n 1 结合起来,无论您有什么输入,每个输入行都会执行一个命令,这可能是查看原始问题的另一种方式(可能是最常见的)直观,给定标题):
如上所述,如果您使用 GNU
one \ntwo\nthree and four' | xargs -L 1 ./show -> "one" "two" -> "three" "and" "four"xargs
,您可以将tr
替换为 GNU 特定选项-d
>:显然,
-L< /code> 并不是要改变 xargs 将输入拆分为参数的方式。
以跨平台方式(不包括 GNU 扩展)执行此操作的唯一参数是
-0
,它将输入围绕 NUL 字节进行分割。然后,只需在 tr 的帮助下将换行符转换为 NUL 即可:
现在参数解析看起来没问题,包括尾随空格。
最后,如果将此技术与 -n 1 结合起来,无论您有什么输入,每个输入行都会执行一个命令,这可能是查看原始问题的另一种方式(可能是最常见的)直观,给定标题):
如上所述,如果您使用 GNU
one\ntwo\nthree and four' | xargs -L 1 ./show -> "one" -> "two" -> "three" "and" "four"xargs
,您可以将tr
替换为 GNU 特定选项-d
>:不仅如此,如果一行以空格结尾,它会附加到下一行:
显然,
-L< /code> 并不是要改变 xargs 将输入拆分为参数的方式。
以跨平台方式(不包括 GNU 扩展)执行此操作的唯一参数是
-0
,它将输入围绕 NUL 字节进行分割。然后,只需在 tr 的帮助下将换行符转换为 NUL 即可:
现在参数解析看起来没问题,包括尾随空格。
最后,如果将此技术与 -n 1 结合起来,无论您有什么输入,每个输入行都会执行一个命令,这可能是查看原始问题的另一种方式(可能是最常见的)直观,给定标题):
如上所述,如果您使用 GNU
one \ntwo\nthree and four' | tr '\n' '\0' | xargs -0 ./show -> "one " "two" "three and four"xargs
,您可以将tr
替换为 GNU 特定选项-d
>:现在参数解析看起来没问题,包括尾随空格。
最后,如果将此技术与 -n 1 结合起来,无论您有什么输入,每个输入行都会执行一个命令,这可能是查看原始问题的另一种方式(可能是最常见的)直观,给定标题):
如上所述,如果您使用 GNU
one\ntwo\nthree and four' | xargs -L 1 ./show -> "one" -> "two" -> "three" "and" "four"xargs
,您可以将tr
替换为 GNU 特定选项-d
>:不仅如此,如果一行以空格结尾,它会附加到下一行:
显然,
-L< /code> 并不是要改变 xargs 将输入拆分为参数的方式。
以跨平台方式(不包括 GNU 扩展)执行此操作的唯一参数是
-0
,它将输入围绕 NUL 字节进行分割。然后,只需在 tr 的帮助下将换行符转换为 NUL 即可:
现在参数解析看起来没问题,包括尾随空格。
最后,如果将此技术与 -n 1 结合起来,无论您有什么输入,每个输入行都会执行一个命令,这可能是查看原始问题的另一种方式(可能是最常见的)直观,给定标题):
如上所述,如果您使用 GNU
one \ntwo\nthree and four' | xargs -L 1 ./show -> "one" "two" -> "three" "and" "four"xargs
,您可以将tr
替换为 GNU 特定选项-d
>:显然,
-L< /code> 并不是要改变 xargs 将输入拆分为参数的方式。
以跨平台方式(不包括 GNU 扩展)执行此操作的唯一参数是
-0
,它将输入围绕 NUL 字节进行分割。然后,只需在 tr 的帮助下将换行符转换为 NUL 即可:
现在参数解析看起来没问题,包括尾随空格。
最后,如果将此技术与 -n 1 结合起来,无论您有什么输入,每个输入行都会执行一个命令,这可能是查看原始问题的另一种方式(可能是最常见的)直观,给定标题):
如上所述,如果您使用 GNU
one\ntwo\nthree and four' | xargs -L 1 ./show -> "one" -> "two" -> "three" "and" "four"xargs
,您可以将tr
替换为 GNU 特定选项-d
>:不仅如此,如果一行以空格结尾,它会附加到下一行:
显然,
-L< /code> 并不是要改变 xargs 将输入拆分为参数的方式。
以跨平台方式(不包括 GNU 扩展)执行此操作的唯一参数是
-0
,它将输入围绕 NUL 字节进行分割。然后,只需在 tr 的帮助下将换行符转换为 NUL 即可:
现在参数解析看起来没问题,包括尾随空格。
最后,如果将此技术与 -n 1 结合起来,无论您有什么输入,每个输入行都会执行一个命令,这可能是查看原始问题的另一种方式(可能是最常见的)直观,给定标题):
如上所述,如果您使用 GNU
one \ntwo\nthree and four' | tr '\n' '\0' | xargs -0 -n1 ./show -> "one " -> "two" -> "three and four"xargs
,您可以将tr
替换为 GNU 特定选项-d
>:如上所述,如果您使用 GNU
one\ntwo\nthree and four' | xargs -L 1 ./show -> "one" -> "two" -> "three" "and" "four"xargs
,您可以将tr
替换为 GNU 特定选项-d
>:不仅如此,如果一行以空格结尾,它会附加到下一行:
显然,
-L< /code> 并不是要改变 xargs 将输入拆分为参数的方式。
以跨平台方式(不包括 GNU 扩展)执行此操作的唯一参数是
-0
,它将输入围绕 NUL 字节进行分割。然后,只需在 tr 的帮助下将换行符转换为 NUL 即可:
现在参数解析看起来没问题,包括尾随空格。
最后,如果将此技术与 -n 1 结合起来,无论您有什么输入,每个输入行都会执行一个命令,这可能是查看原始问题的另一种方式(可能是最常见的)直观,给定标题):
如上所述,如果您使用 GNU
one \ntwo\nthree and four' | xargs -L 1 ./show -> "one" "two" -> "three" "and" "four"xargs
,您可以将tr
替换为 GNU 特定选项-d
>:显然,
-L< /code> 并不是要改变 xargs 将输入拆分为参数的方式。
以跨平台方式(不包括 GNU 扩展)执行此操作的唯一参数是
-0
,它将输入围绕 NUL 字节进行分割。然后,只需在 tr 的帮助下将换行符转换为 NUL 即可:
现在参数解析看起来没问题,包括尾随空格。
最后,如果将此技术与 -n 1 结合起来,无论您有什么输入,每个输入行都会执行一个命令,这可能是查看原始问题的另一种方式(可能是最常见的)直观,给定标题):
如上所述,如果您使用 GNU
one\ntwo\nthree and four' | xargs -L 1 ./show -> "one" -> "two" -> "three" "and" "four"xargs
,您可以将tr
替换为 GNU 特定选项-d
>:不仅如此,如果一行以空格结尾,它会附加到下一行:
显然,
-L< /code> 并不是要改变 xargs 将输入拆分为参数的方式。
以跨平台方式(不包括 GNU 扩展)执行此操作的唯一参数是
-0
,它将输入围绕 NUL 字节进行分割。然后,只需在 tr 的帮助下将换行符转换为 NUL 即可:
现在参数解析看起来没问题,包括尾随空格。
最后,如果将此技术与 -n 1 结合起来,无论您有什么输入,每个输入行都会执行一个命令,这可能是查看原始问题的另一种方式(可能是最常见的)直观,给定标题):
如上所述,如果您使用 GNU
one \ntwo\nthree and four' | tr '\n' '\0' | xargs -0 ./show -> "one " "two" "three and four"xargs
,您可以将tr
替换为 GNU 特定选项-d
>:现在参数解析看起来没问题,包括尾随空格。
最后,如果将此技术与 -n 1 结合起来,无论您有什么输入,每个输入行都会执行一个命令,这可能是查看原始问题的另一种方式(可能是最常见的)直观,给定标题):
如上所述,如果您使用 GNU
one\ntwo\nthree and four' | xargs -L 1 ./show -> "one" -> "two" -> "three" "and" "four"xargs
,您可以将tr
替换为 GNU 特定选项-d
>:不仅如此,如果一行以空格结尾,它会附加到下一行:
显然,
-L< /code> 并不是要改变 xargs 将输入拆分为参数的方式。
以跨平台方式(不包括 GNU 扩展)执行此操作的唯一参数是
-0
,它将输入围绕 NUL 字节进行分割。然后,只需在 tr 的帮助下将换行符转换为 NUL 即可:
现在参数解析看起来没问题,包括尾随空格。
最后,如果将此技术与 -n 1 结合起来,无论您有什么输入,每个输入行都会执行一个命令,这可能是查看原始问题的另一种方式(可能是最常见的)直观,给定标题):
如上所述,如果您使用 GNU
one \ntwo\nthree and four' | xargs -L 1 ./show -> "one" "two" -> "three" "and" "four"xargs
,您可以将tr
替换为 GNU 特定选项-d
>:显然,
-L< /code> 并不是要改变 xargs 将输入拆分为参数的方式。
以跨平台方式(不包括 GNU 扩展)执行此操作的唯一参数是
-0
,它将输入围绕 NUL 字节进行分割。然后,只需在 tr 的帮助下将换行符转换为 NUL 即可:
现在参数解析看起来没问题,包括尾随空格。
最后,如果将此技术与 -n 1 结合起来,无论您有什么输入,每个输入行都会执行一个命令,这可能是查看原始问题的另一种方式(可能是最常见的)直观,给定标题):
如上所述,如果您使用 GNU
one\ntwo\nthree and four' | xargs -L 1 ./show -> "one" -> "two" -> "three" "and" "four"xargs
,您可以将tr
替换为 GNU 特定选项-d
>:不仅如此,如果一行以空格结尾,它会附加到下一行:
显然,
-L< /code> 并不是要改变 xargs 将输入拆分为参数的方式。
以跨平台方式(不包括 GNU 扩展)执行此操作的唯一参数是
-0
,它将输入围绕 NUL 字节进行分割。然后,只需在 tr 的帮助下将换行符转换为 NUL 即可:
现在参数解析看起来没问题,包括尾随空格。
最后,如果将此技术与 -n 1 结合起来,无论您有什么输入,每个输入行都会执行一个命令,这可能是查看原始问题的另一种方式(可能是最常见的)直观,给定标题):
如上所述,如果您使用 GNU
xargs
,您可以将tr
替换为 GNU 特定选项-d
>:It seems to me all existing answers on this page are wrong, including the one marked as correct. That stems from the fact that the question is ambiguously worded.
Summary: If you want to execute the command "exactly once for each line of input," passing the entire line (without newline) to the command as a single argument, then this is the best UNIX-compatible way to do it:
If you are using GNU
xargs
and don't need to be compatible with all other UNIX's (FreeBSD, Mac OS X, etc.) then you can use the GNU-specific option-d
:Now for the long explanation…
There are two issues to take into account when using xargs:
To test xargs' behavior, we need an utility that shows how many times it's being executed and with how many arguments. I don't know if there is a standard utility to do that, but we can code it quite easily in bash:
Assuming you save it as
show
in your current directory and make it executable, here is how it works:Now, if the original question is really about point 2. above (as I think it is, after reading it a few times over) and it is to be read like this (changes in bold):
then the answer is
-n 1
.Let's compare xargs' default behavior, which splits the input around whitespace and calls the command as few times as possible:
and its behavior with
-n 1
:If, on the other hand, the original question was about point 1. input splitting and it was to be read like this (many people coming here seem to think that's the case, or are confusing the two issues):
then the answer is more subtle.
One would think that
-L 1
could be of help, but it turns out it doesn't change argument parsing. It only executes the command once for each input line, with as many arguments as were there on that input line:Not only that, but if a line ends with whitespace, it is appended to the next:
Clearly,
-L
is not about changing the way xargs splits the input into arguments.The only argument that does so in a cross-platform fashion (excluding GNU extensions) is
-0
, which splits the input around NUL bytes.Then, it's just a matter of translating newlines to NUL with the help of
tr
:Now the argument parsing looks all right, including the trailing whitespace.
Finally, if you combine this technique with
-n 1
, you get exactly one command execution per input line, whatever input you have, which may be yet another way to look at the original question (possibly the most intuitive, given the title):As mentioned above, if you are using GNU
one \ntwo\nthree and four' | xargs -L 1 ./show -> "one" "two" -> "three" "and" "four"xargs
you can replace thetr
with the GNU-specific option-d
:Clearly,
-L
is not about changing the way xargs splits the input into arguments.The only argument that does so in a cross-platform fashion (excluding GNU extensions) is
-0
, which splits the input around NUL bytes.Then, it's just a matter of translating newlines to NUL with the help of
tr
:Now the argument parsing looks all right, including the trailing whitespace.
Finally, if you combine this technique with
-n 1
, you get exactly one command execution per input line, whatever input you have, which may be yet another way to look at the original question (possibly the most intuitive, given the title):As mentioned above, if you are using GNU
one\ntwo\nthree and four' | xargs -L 1 ./show -> "one" -> "two" -> "three" "and" "four"xargs
you can replace thetr
with the GNU-specific option-d
:Not only that, but if a line ends with whitespace, it is appended to the next:
Clearly,
-L
is not about changing the way xargs splits the input into arguments.The only argument that does so in a cross-platform fashion (excluding GNU extensions) is
-0
, which splits the input around NUL bytes.Then, it's just a matter of translating newlines to NUL with the help of
tr
:Now the argument parsing looks all right, including the trailing whitespace.
Finally, if you combine this technique with
-n 1
, you get exactly one command execution per input line, whatever input you have, which may be yet another way to look at the original question (possibly the most intuitive, given the title):As mentioned above, if you are using GNU
one \ntwo\nthree and four' | tr '\n' '\0' | xargs -0 -n1 ./show -> "one " -> "two" -> "three and four"xargs
you can replace thetr
with the GNU-specific option-d
:As mentioned above, if you are using GNU
one\ntwo\nthree and four' | xargs -L 1 ./show -> "one" -> "two" -> "three" "and" "four"xargs
you can replace thetr
with the GNU-specific option-d
:Not only that, but if a line ends with whitespace, it is appended to the next:
Clearly,
-L
is not about changing the way xargs splits the input into arguments.The only argument that does so in a cross-platform fashion (excluding GNU extensions) is
-0
, which splits the input around NUL bytes.Then, it's just a matter of translating newlines to NUL with the help of
tr
:Now the argument parsing looks all right, including the trailing whitespace.
Finally, if you combine this technique with
-n 1
, you get exactly one command execution per input line, whatever input you have, which may be yet another way to look at the original question (possibly the most intuitive, given the title):As mentioned above, if you are using GNU
one \ntwo\nthree and four' | xargs -L 1 ./show -> "one" "two" -> "three" "and" "four"xargs
you can replace thetr
with the GNU-specific option-d
:Clearly,
-L
is not about changing the way xargs splits the input into arguments.The only argument that does so in a cross-platform fashion (excluding GNU extensions) is
-0
, which splits the input around NUL bytes.Then, it's just a matter of translating newlines to NUL with the help of
tr
:Now the argument parsing looks all right, including the trailing whitespace.
Finally, if you combine this technique with
-n 1
, you get exactly one command execution per input line, whatever input you have, which may be yet another way to look at the original question (possibly the most intuitive, given the title):As mentioned above, if you are using GNU
one\ntwo\nthree and four' | xargs -L 1 ./show -> "one" -> "two" -> "three" "and" "four"xargs
you can replace thetr
with the GNU-specific option-d
:Not only that, but if a line ends with whitespace, it is appended to the next:
Clearly,
-L
is not about changing the way xargs splits the input into arguments.The only argument that does so in a cross-platform fashion (excluding GNU extensions) is
-0
, which splits the input around NUL bytes.Then, it's just a matter of translating newlines to NUL with the help of
tr
:Now the argument parsing looks all right, including the trailing whitespace.
Finally, if you combine this technique with
-n 1
, you get exactly one command execution per input line, whatever input you have, which may be yet another way to look at the original question (possibly the most intuitive, given the title):As mentioned above, if you are using GNU
one \ntwo\nthree and four' | tr '\n' '\0' | xargs -0 ./show -> "one " "two" "three and four"xargs
you can replace thetr
with the GNU-specific option-d
:Now the argument parsing looks all right, including the trailing whitespace.
Finally, if you combine this technique with
-n 1
, you get exactly one command execution per input line, whatever input you have, which may be yet another way to look at the original question (possibly the most intuitive, given the title):As mentioned above, if you are using GNU
one\ntwo\nthree and four' | xargs -L 1 ./show -> "one" -> "two" -> "three" "and" "four"xargs
you can replace thetr
with the GNU-specific option-d
:Not only that, but if a line ends with whitespace, it is appended to the next:
Clearly,
-L
is not about changing the way xargs splits the input into arguments.The only argument that does so in a cross-platform fashion (excluding GNU extensions) is
-0
, which splits the input around NUL bytes.Then, it's just a matter of translating newlines to NUL with the help of
tr
:Now the argument parsing looks all right, including the trailing whitespace.
Finally, if you combine this technique with
-n 1
, you get exactly one command execution per input line, whatever input you have, which may be yet another way to look at the original question (possibly the most intuitive, given the title):As mentioned above, if you are using GNU
one \ntwo\nthree and four' | xargs -L 1 ./show -> "one" "two" -> "three" "and" "four"xargs
you can replace thetr
with the GNU-specific option-d
:Clearly,
-L
is not about changing the way xargs splits the input into arguments.The only argument that does so in a cross-platform fashion (excluding GNU extensions) is
-0
, which splits the input around NUL bytes.Then, it's just a matter of translating newlines to NUL with the help of
tr
:Now the argument parsing looks all right, including the trailing whitespace.
Finally, if you combine this technique with
-n 1
, you get exactly one command execution per input line, whatever input you have, which may be yet another way to look at the original question (possibly the most intuitive, given the title):As mentioned above, if you are using GNU
one\ntwo\nthree and four' | xargs -L 1 ./show -> "one" -> "two" -> "three" "and" "four"xargs
you can replace thetr
with the GNU-specific option-d
:Not only that, but if a line ends with whitespace, it is appended to the next:
Clearly,
-L
is not about changing the way xargs splits the input into arguments.The only argument that does so in a cross-platform fashion (excluding GNU extensions) is
-0
, which splits the input around NUL bytes.Then, it's just a matter of translating newlines to NUL with the help of
tr
:Now the argument parsing looks all right, including the trailing whitespace.
Finally, if you combine this technique with
-n 1
, you get exactly one command execution per input line, whatever input you have, which may be yet another way to look at the original question (possibly the most intuitive, given the title):As mentioned above, if you are using GNU
one \ntwo\nthree and four' | xargs -d\\n -n1 ./show -> "one " -> "two" -> "three and four" one\ntwo\nthree and four' | xargs -L 1 ./show -> "one" -> "two" -> "three" "and" "four"xargs
you can replace thetr
with the GNU-specific option-d
:Not only that, but if a line ends with whitespace, it is appended to the next:
Clearly,
-L
is not about changing the way xargs splits the input into arguments.The only argument that does so in a cross-platform fashion (excluding GNU extensions) is
-0
, which splits the input around NUL bytes.Then, it's just a matter of translating newlines to NUL with the help of
tr
:Now the argument parsing looks all right, including the trailing whitespace.
Finally, if you combine this technique with
-n 1
, you get exactly one command execution per input line, whatever input you have, which may be yet another way to look at the original question (possibly the most intuitive, given the title):As mentioned above, if you are using GNU
one \ntwo\nthree and four' | xargs -L 1 ./show -> "one" "two" -> "three" "and" "four"xargs
you can replace thetr
with the GNU-specific option-d
:Clearly,
-L
is not about changing the way xargs splits the input into arguments.The only argument that does so in a cross-platform fashion (excluding GNU extensions) is
-0
, which splits the input around NUL bytes.Then, it's just a matter of translating newlines to NUL with the help of
tr
:Now the argument parsing looks all right, including the trailing whitespace.
Finally, if you combine this technique with
-n 1
, you get exactly one command execution per input line, whatever input you have, which may be yet another way to look at the original question (possibly the most intuitive, given the title):As mentioned above, if you are using GNU
one\ntwo\nthree and four' | xargs -L 1 ./show -> "one" -> "two" -> "three" "and" "four"xargs
you can replace thetr
with the GNU-specific option-d
:Not only that, but if a line ends with whitespace, it is appended to the next:
Clearly,
-L
is not about changing the way xargs splits the input into arguments.The only argument that does so in a cross-platform fashion (excluding GNU extensions) is
-0
, which splits the input around NUL bytes.Then, it's just a matter of translating newlines to NUL with the help of
tr
:Now the argument parsing looks all right, including the trailing whitespace.
Finally, if you combine this technique with
-n 1
, you get exactly one command execution per input line, whatever input you have, which may be yet another way to look at the original question (possibly the most intuitive, given the title):As mentioned above, if you are using GNU
one \ntwo\nthree and four' | tr '\n' '\0' | xargs -0 ./show -> "one " "two" "three and four"xargs
you can replace thetr
with the GNU-specific option-d
:Now the argument parsing looks all right, including the trailing whitespace.
Finally, if you combine this technique with
-n 1
, you get exactly one command execution per input line, whatever input you have, which may be yet another way to look at the original question (possibly the most intuitive, given the title):As mentioned above, if you are using GNU
one\ntwo\nthree and four' | xargs -L 1 ./show -> "one" -> "two" -> "three" "and" "four"xargs
you can replace thetr
with the GNU-specific option-d
:Not only that, but if a line ends with whitespace, it is appended to the next:
Clearly,
-L
is not about changing the way xargs splits the input into arguments.The only argument that does so in a cross-platform fashion (excluding GNU extensions) is
-0
, which splits the input around NUL bytes.Then, it's just a matter of translating newlines to NUL with the help of
tr
:Now the argument parsing looks all right, including the trailing whitespace.
Finally, if you combine this technique with
-n 1
, you get exactly one command execution per input line, whatever input you have, which may be yet another way to look at the original question (possibly the most intuitive, given the title):As mentioned above, if you are using GNU
one \ntwo\nthree and four' | xargs -L 1 ./show -> "one" "two" -> "three" "and" "four"xargs
you can replace thetr
with the GNU-specific option-d
:Clearly,
-L
is not about changing the way xargs splits the input into arguments.The only argument that does so in a cross-platform fashion (excluding GNU extensions) is
-0
, which splits the input around NUL bytes.Then, it's just a matter of translating newlines to NUL with the help of
tr
:Now the argument parsing looks all right, including the trailing whitespace.
Finally, if you combine this technique with
-n 1
, you get exactly one command execution per input line, whatever input you have, which may be yet another way to look at the original question (possibly the most intuitive, given the title):As mentioned above, if you are using GNU
one\ntwo\nthree and four' | xargs -L 1 ./show -> "one" -> "two" -> "three" "and" "four"xargs
you can replace thetr
with the GNU-specific option-d
:Not only that, but if a line ends with whitespace, it is appended to the next:
Clearly,
-L
is not about changing the way xargs splits the input into arguments.The only argument that does so in a cross-platform fashion (excluding GNU extensions) is
-0
, which splits the input around NUL bytes.Then, it's just a matter of translating newlines to NUL with the help of
tr
:Now the argument parsing looks all right, including the trailing whitespace.
Finally, if you combine this technique with
-n 1
, you get exactly one command execution per input line, whatever input you have, which may be yet another way to look at the original question (possibly the most intuitive, given the title):As mentioned above, if you are using GNU
one \ntwo\nthree and four' | tr '\n' '\0' | xargs -0 -n1 ./show -> "one " -> "two" -> "three and four"xargs
you can replace thetr
with the GNU-specific option-d
:As mentioned above, if you are using GNU
one\ntwo\nthree and four' | xargs -L 1 ./show -> "one" -> "two" -> "three" "and" "four"xargs
you can replace thetr
with the GNU-specific option-d
:Not only that, but if a line ends with whitespace, it is appended to the next:
Clearly,
-L
is not about changing the way xargs splits the input into arguments.The only argument that does so in a cross-platform fashion (excluding GNU extensions) is
-0
, which splits the input around NUL bytes.Then, it's just a matter of translating newlines to NUL with the help of
tr
:Now the argument parsing looks all right, including the trailing whitespace.
Finally, if you combine this technique with
-n 1
, you get exactly one command execution per input line, whatever input you have, which may be yet another way to look at the original question (possibly the most intuitive, given the title):As mentioned above, if you are using GNU
one \ntwo\nthree and four' | xargs -L 1 ./show -> "one" "two" -> "three" "and" "four"xargs
you can replace thetr
with the GNU-specific option-d
:Clearly,
-L
is not about changing the way xargs splits the input into arguments.The only argument that does so in a cross-platform fashion (excluding GNU extensions) is
-0
, which splits the input around NUL bytes.Then, it's just a matter of translating newlines to NUL with the help of
tr
:Now the argument parsing looks all right, including the trailing whitespace.
Finally, if you combine this technique with
-n 1
, you get exactly one command execution per input line, whatever input you have, which may be yet another way to look at the original question (possibly the most intuitive, given the title):As mentioned above, if you are using GNU
one\ntwo\nthree and four' | xargs -L 1 ./show -> "one" -> "two" -> "three" "and" "four"xargs
you can replace thetr
with the GNU-specific option-d
:Not only that, but if a line ends with whitespace, it is appended to the next:
Clearly,
-L
is not about changing the way xargs splits the input into arguments.The only argument that does so in a cross-platform fashion (excluding GNU extensions) is
-0
, which splits the input around NUL bytes.Then, it's just a matter of translating newlines to NUL with the help of
tr
:Now the argument parsing looks all right, including the trailing whitespace.
Finally, if you combine this technique with
-n 1
, you get exactly one command execution per input line, whatever input you have, which may be yet another way to look at the original question (possibly the most intuitive, given the title):As mentioned above, if you are using GNU
one \ntwo\nthree and four' | tr '\n' '\0' | xargs -0 ./show -> "one " "two" "three and four"xargs
you can replace thetr
with the GNU-specific option-d
:Now the argument parsing looks all right, including the trailing whitespace.
Finally, if you combine this technique with
-n 1
, you get exactly one command execution per input line, whatever input you have, which may be yet another way to look at the original question (possibly the most intuitive, given the title):As mentioned above, if you are using GNU
one\ntwo\nthree and four' | xargs -L 1 ./show -> "one" -> "two" -> "three" "and" "four"xargs
you can replace thetr
with the GNU-specific option-d
:Not only that, but if a line ends with whitespace, it is appended to the next:
Clearly,
-L
is not about changing the way xargs splits the input into arguments.The only argument that does so in a cross-platform fashion (excluding GNU extensions) is
-0
, which splits the input around NUL bytes.Then, it's just a matter of translating newlines to NUL with the help of
tr
:Now the argument parsing looks all right, including the trailing whitespace.
Finally, if you combine this technique with
-n 1
, you get exactly one command execution per input line, whatever input you have, which may be yet another way to look at the original question (possibly the most intuitive, given the title):As mentioned above, if you are using GNU
one \ntwo\nthree and four' | xargs -L 1 ./show -> "one" "two" -> "three" "and" "four"xargs
you can replace thetr
with the GNU-specific option-d
:Clearly,
-L
is not about changing the way xargs splits the input into arguments.The only argument that does so in a cross-platform fashion (excluding GNU extensions) is
-0
, which splits the input around NUL bytes.Then, it's just a matter of translating newlines to NUL with the help of
tr
:Now the argument parsing looks all right, including the trailing whitespace.
Finally, if you combine this technique with
-n 1
, you get exactly one command execution per input line, whatever input you have, which may be yet another way to look at the original question (possibly the most intuitive, given the title):As mentioned above, if you are using GNU
one\ntwo\nthree and four' | xargs -L 1 ./show -> "one" -> "two" -> "three" "and" "four"xargs
you can replace thetr
with the GNU-specific option-d
:Not only that, but if a line ends with whitespace, it is appended to the next:
Clearly,
-L
is not about changing the way xargs splits the input into arguments.The only argument that does so in a cross-platform fashion (excluding GNU extensions) is
-0
, which splits the input around NUL bytes.Then, it's just a matter of translating newlines to NUL with the help of
tr
:Now the argument parsing looks all right, including the trailing whitespace.
Finally, if you combine this technique with
-n 1
, you get exactly one command execution per input line, whatever input you have, which may be yet another way to look at the original question (possibly the most intuitive, given the title):As mentioned above, if you are using GNU
xargs
you can replace thetr
with the GNU-specific option-d
:如果您想对来自
find
的每一行(即结果)运行命令,那么您需要xargs
做什么?尝试:
find
路径-type f -exec
你的命令{} \;
其中文字
{}
被文件名替换,并且find
需要文字\;
才能知道自定义命令在此结束。编辑:(
在编辑问题后澄清您了解
-exec
)来自
man xargs
:请注意,如果您使用
xargs
,以空格结尾的文件名会给您带来麻烦:因此,如果您不关心
-exec
选项,最好使用-print0
和-0
:If you want to run the command for every line (i.e. result) coming from
find
, then what do you need thexargs
for?Try:
find
path-type f -exec
your-command{} \;
where the literal
{}
gets substituted by the filename and the literal\;
is needed forfind
to know that the custom command ends there.EDIT:
(after the edit of your question clarifying that you know about
-exec
)From
man xargs
:Note that filenames ending in blanks would cause you trouble if you use
xargs
:So if you don't care about the
-exec
option, you better use-print0
and-0
:这两种方法也有效,并且适用于不使用 find 的其他命令!
示例用例:
将删除该目录下的所有 pyc 文件,即使 pyc 文件包含空格。
These two ways also work, and will work for other commands that are not using find!
example use case:
will delete all pyc files under this directory even if the pyc files contain spaces.
-L 1
是简单的解决方案,但如果任何文件中包含空格,则它不起作用。 这是 find 的-print0
参数的关键功能 - 用“\0”字符而不是空格分隔参数。 下面是一个示例:更好的解决方案是使用
tr
将换行符转换为空 (\0
) 字符,然后使用xargs -0
争论。 下面是一个示例:如果您需要限制调用次数,您可以使用
-n 1
参数为每个输入对程序进行一次调用:这还允许您过滤 find 的输出在将中断转换为空之前。
-L 1
is the simple solution but it does not work if any of the files contain spaces in them. This is a key function of find's-print0
argument – to separate the arguments by '\0' character instead of whitespace. Here's an example:A better solution is to use
tr
to convert newlines to null (\0
) characters, and then use thexargs -0
argument. Here's an example:If you then need to limit the number of calls you can use the
-n 1
argument to make one call to the program for each input:This also allows you to filter the output of find before converting the breaks into nulls.
另一种选择...
Another alternative...
是你所需要的全部。
is all you need.
以下命令将查找
/path
中的所有文件(-type f),然后使用cp
将它们复制到当前文件夹。 请注意,使用 if-I %
在 cp 命令行中指定占位符,以便可以将参数放置在文件名之后。查找 /path -type f -print0 | xargs -0 -I % cp % .
使用 xargs (GNU findutils) 4.4.0 进行测试
The following command will find all the files (-type f) in
/path
and then copy them usingcp
to the current folder. Note the use if-I %
to specify a placeholder character in thecp
command line so that arguments can be placed after the file name.find /path -type f -print0 | xargs -0 -I % cp % .
Tested with xargs (GNU findutils) 4.4.0
您可以分别使用 --max-lines 或 --max-args 标志来限制行数或参数数(如果每个参数之间有空格)。
You can limit the number of lines, or arguments (if there are spaces between each argument) using the --max-lines or --max-args flags, respectively.
@Draemon 的答案似乎是正确的“-0”,即使文件中有空格。
我正在尝试 xargs 命令,发现“-0”与“-L”完美配合。 甚至空格也会被处理(如果输入以 null 终止)。 下面是一个示例:
下面将分割空值并在列表中的每个参数上执行命令:
因此如果与“-0”一起使用,
-L1
将在每个空终止字符上执行参数。 要查看差异,请尝试:即使这将执行一次:
该命令将执行一次,因为“-L”现在不会在空字节上拆分。 您需要同时提供“-0”和“-L”才能工作。
@Draemon answers seems to be right with "-0" even with space in the file.
I was trying the xargs command and I found that "-0" works perfectly with "-L". even the spaces are treated (if input was null terminated ). the following is an example :
The following will split the nulls and execute the command on each argument in the list :
so
-L1
will execute the argument on each null terminated character if used with "-0". To see the difference try :even this will execute once :
The command will execute once as the "-L" now doesn't split on null byte. you need to provide both "-0" and "-L" to work.
我似乎没有足够的声誉来添加评论 Tobia 的答案上面,所以我添加这个“答案”帮助我们这些想要在 Windows 平台上以同样的方式试验
xargs
的人。这是一个 Windows 批处理文件,它与 Tobia 的快速编码的“show”脚本执行相同的操作:
It seems I don't have enough reputation to add a comment to Tobia's answer above, so I am adding this "answer" to help those of us wanting to experiment with
xargs
the same way on the Windows platforms.Here is a windows batch file that does the same thing as Tobia's quickly coded "show" script:
在您的示例中,将 find 的输出通过管道传输到 xargs 的目的是 find 的 -exec 选项的标准行为是为每个找到的文件执行一次命令。 如果您正在使用 find,并且想要它的标准行为,那么答案很简单 - 一开始就不要使用 xargs。
In your example, the point of piping the output of find to xargs is that the standard behavior of find's -exec option is to execute the command once for each found file. If you're using find, and you want its standard behavior, then the answer is simple - don't use xargs to begin with.
对当前或子文件夹中的每个 build.xml 执行 ant 任务 clean-all 。
execute ant task clean-all on every build.xml on current or sub-folder.