find:使用 find 的 + 形式时缺少 `-exec' 参数

find:使用 find 的 + 形式时缺少 `-exec' 参数

我想要对用命令找到的路径执行一个命令find,并且我想要用它+来减少启动外部命令的次数。

至关重要的是,该命令具有固定值的最终参数。这是一个以echo命令为具体示例,尽管在实际中:

mkdir blah && cd blah
touch fooA
touch fooB
find . -name 'foo*' -exec echo {} second +

我希望打印出以下内容:

./fooA ./fooB second

但是我得到的是错误find: missing argument to-exec' . I've tried all sorts of permutations to get it to work with +. Why isn't this working? It works find with the\;` 变体:

find . -name 'foo*' -exec echo {} second \;
./fooB second
./fooA second

...但这不是我想要的。

find --version报告:

find (GNU findutils) 4.4.2
Copyright (C) 2007 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Built using GNU gnulib version e5573b1bad88bfabcda181b9e0125fb0c52b7d3b
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION FTS() CBO(level=0) 

+以下是涵盖和形式的手册页的摘录;

   -exec command ;
          Execute  command;  true  if 0 status is returned.  All following arguments to find are taken to be arguments to the command until an argument consisting of `;' is encoun‐
          tered.  The string `{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not  just  in  arguments  where  it  is
          alone,  as  in  some  versions of find.  Both of these constructions might need to be escaped (with a `\') or quoted to protect them from expansion by the shell.  See the
          EXAMPLES section for examples of the use of the -exec option.  The specified command is run once for each matched file.  The command is executed in  the  starting  direc‐
          tory.   There are unavoidable security problems surrounding use of the -exec action; you should use the -execdir option instead.

   -exec command {} +
          This  variant  of  the  -exec  action runs the specified command on the selected files, but the command line is built by appending each selected file name at the end; the
          total number of invocations of the command will be much less than the number of matched files.  The command line is built in much the same way that xargs builds its  com‐
          mand lines.  Only one instance of `{}' is allowed within the command.  The command is executed in the starting directory.

答案1

我找不到find -exec +或的解决方案find | xargs,但是GNU 并行可以完成这项工作。

mkdir blah && cd blah
touch fooA
touch fooB
find . -name 'foo*' -print0 | parallel -0 -j1 -X echo {} second

生成:

./fooA ./fooB second

请注意,该-j1选项限制parallel每次只能执行一项作业,这里仅使用此方法尝试重现所预期的行为find -exec +

答案2

以下是类似的解决方案find | xargs

find . -name 'foo*' -print0 | xargs -0 -n1 -I{} echo {} second

相关内容