如何将 xargs 与 find 一起使用?
我在磁盘上有大量文件,并尝试使用 find
进行 xargs
以获得更快的输出。 <代码>查找 . -printf '%m %p\n'|sort -nr
如果我写 find 。 -printf '%m %p\n'|xargs -0 -P 0 sort -nr
,它给出错误参数行太长
。删除 -0
选项会出现其他错误。
I have a large number of files on disk and trying to xargs
with find
to get faster output.find . -printf '%m %p\n'|sort -nr
If I write find . -printf '%m %p\n'|xargs -0 -P 0 sort -nr
, it gives error argument line is too long
. Removing -0
option gives other error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
并行命令,例如
xargs
或GNU parallel
仅当任务可以分为多个独立的任务时才适用
作业,例如使用同一命令一次处理多个文件。
无法将
sort
命令与这些并行命令一起使用。虽然
sort
有--parallel
选项,但它可能不适用于管道输入。 (未完全评估。)
作为旁注:
xargs 的机制是它从以下位置读取项目(大多数情况下是文件名):
标准输入并通过组合生成单独的命令
每个项目的参数列表(要执行的命令)。然后你就会
请参阅语法
.. | xargs .. sort
不正确,因为每个文件名作为参数传递给
sort
,然后sort
尝试对内容进行排序文件的内容。
xargs 的
-0
选项告诉 xargs 输入项是分隔的由空字符而不是换行符。输入时很有用
文件名包含特殊字符,包括换行符。
为了使用此功能,您需要连贯地处理管道
以这种方式流:将
-print0
选项添加到find
和-z
选项来
排序
。否则项目会被错误地连接并会导致参数行太长
错误。The parallelism commands such as
xargs
orGNU parallel
are applicable only if the task can be divided into multiple independent
jobs e.g. processing multiple files at once with the same command.
It is not possible to use
sort
command with these parallelism commands.Although
sort
has--parallel
option, it may not work well forpiped input. (Not fully evaluated.)
As side notes:
The mechanism of
xargs
is it reads items (filenames in most cases) fromthe standard input and generates individual commands by combining
the argument list (command to be executed) with each item. Then you'll
see the syntax
.. | xargs .. sort
is incorrect because each filenameis passed to
sort
as an argument thensort
tries to sort the contentsof the file.
The
-0
option toxargs
tells xargs that input items are delimitedby a null character instead of a newline. It is useful when the input
filenames contain special characters including a newline character.
In order to use this feature, you need to coherently handle the piped
stream in that way: putting
-print0
option tofind
and-z
optionto
sort
. Otherwise the items are wrongly concatenated and will causeargument line is too long
error.建议使用
locate
命令而不是find
命令。您可能想使用 updatedb 命令更新文件数据库。
在此处了解有关
locate
命令的更多信息。Suggesting to use
locate
command instead offind
command.You might want to update files database with
updatedb
command.Read more about
locate
command here.