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

Fai*_*Dev 62 command-line find

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

Cal*_*leb 73

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

find -iname '*.txt' -delete
Run Code Online (Sandbox Code Playgroud)

这处理了一堆您可能没有考虑过的边缘情况,包括需要删除哪些顺序文件和目录才能避免出错。对于其他用例...

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

-exec选项后面的命令,你想与运行{}表示,其中通过查找发现该文件应包含由要么被终止点\;运行的命令一次为每个文件或+更换{}与所有的匹配参数列表. 请注意,分号终止符已转义,因此 shell 不会将其理解为通向新命令的分隔符。

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

find -iname '*.txt' -execdir rm {} \;
Run Code Online (Sandbox Code Playgroud)

这是查找手册 ( man find) 中的相关部分:

   -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.
Run Code Online (Sandbox Code Playgroud)


phu*_*ehe 10

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

find /home/phunehehe -iregex '.*\.png$' -print0 | xargs -0 file
Run Code Online (Sandbox Code Playgroud)

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


Sau*_*Don 5

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

find . -name "*.txt" -delete

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