如何为每个查找结果运行特定命令?

如何为每个查找结果运行特定命令?

如何对使用该find命令找到的每个文件运行特定命令?出于问题的目的,可以说我只想删除 . 找到的每个文件find

答案1

编辑:虽然以下答案解释了一般用法,但我应该注意删除文件和目录是一种特殊情况。不使用该-execdir rm {} ';'构造,只需使用-delete1,如下所示:

find -iname '*.txt' -delete

这可以处理一些您可能没有想到的边缘情况,包括需要删除哪些顺序的文件和目录以避免出现错误以及解决一些安全问题。对于其他用例...

处理查找结果的运行命令的最佳方法通常是对命令使用各种-exec/-ok谓词find。特别是,您应该尽可能尝试使用-execdir1,因为它在找到的文件的目录内运行,并且通常比其他谓词更安全(在防止愚蠢错误造成灾难性的意义上)。

谓词-exec后面是您要运行的命令,{}表示应包含由 找到的文件的位置,并通过为每个文件运行一次命令或替换为所有匹配的参数列表来find终止。请注意,分号终止符是用引号引起来的,因此 shell 不会将其理解为通向新命令的分隔符。对于某些 shell,例如和 的衍生版本或旧版本,您可能还需要引用。根据 shell,您可以使用替代形式的引用,例如,或。对于类似 Bourne 或类似 csh 的 shell 来说是惯用的。';'+{}rcfish{}";"\;$';'\;

假设您正在查找所有文本文件:

find -iname '*.txt' -execdir rm {} ';'

以下是 GNU 手册中的相关部分findman find在 GNU 系统上):

   -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 encountered.  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 sec-
          tion for examples of the use of the -exec option.  The specified
          command is run once for each matched file.  The command is  exe-
          cuted  in  the starting directory.   There are unavoidable secu-
          rity 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 invoca-
          tions 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 command lines.  Only one instance of  ‘{}’
          is  allowed  within the command.  The command is executed in the
          starting directory.


   -execdir command ;

   -execdir command {} +
          Like -exec, but the specified command is run from the  subdirec-
          tory  containing  the  matched  file,  which is not normally the
          directory in which you started find.  This a  much  more  secure
          method  for invoking commands, as it avoids race conditions dur-
          ing resolution of the paths to the matched files.  As  with  the
          -exec action, the ‘+’ form of -execdir will build a command line
          to process more than one matched file, but any given  invocation
          of command will only list files that exist in the same subdirec-
          tory.  If you use this option, you must ensure that  your  $PATH
          environment  variable  does  not  reference  ‘.’;  otherwise, an
          attacker can run any commands they like by leaving an  appropri-
          ately-named  file in a directory in which you will run -execdir.
          The same applies to having entries in $PATH which are  empty  or
          which are not absolute directory names.

-inameor一样-execdir,这不是标准选项,但许多find实现都支持它。如果支持,则意味着-depth.GNU手册find建议-depth使用时显式添加-delete以免忘记这一事实。

答案2

另一种方法是通过管道传输输出并使用后续命令对其进行解析。唯一安全的方法是使用该-print0选项,该选项指示find使用空字符作为结果分隔符。接收命令必须能够识别空分隔输入。例子:

find /home/phunehehe -iregex '.*\.png$' -print0 | xargs -0 file

请注意,该-0选项指示xargs将输入视为空分隔。

答案3

如果您只需要执行此操作,Find 有一个内置的删除命令。

find . -name "*.txt" -delete

使用上述命令将删除找到的任何 .txt 文件。

答案4

我正在寻找这个问题的答案,我偶然发现了这个线程。这些答案给了我关于如何实现它的想法。假设您要查找mediainfo所有 JPEG 文件的

这将附加mediainfo "在每个匹配文件的开头和"结尾(为了尽可能转义特殊字符),将其放入脚本并运行脚本:

find . -name *.jpg | sed -e 's/^/mediainfo "/g;' | sed -e 's/$/"/g;' > foo.sh && sh foo.sh

如果您担心出现问题,您可以跳过将输出重定向到文件的过程,而只需在运行脚本之前在终端中查看结果。

相关内容