Answers:
编辑:虽然以下答案解释了一般情况,但我应该注意删除文件和目录是一种特殊情况。除了使用-execdir rm {} \;
构造以外,还可以使用-delete
,例如:
find -iname '*.txt' -delete
这处理了您可能不会考虑的一些极端情况,包括需要删除哪些顺序文件和目录以免出错。对于其他用例...
处理查找结果的运行命令的最佳方法通常-exec
是对find
命令使用各种选项。特别是,您应该尽可能使用-execdir
它,因为它运行在找到的文件目录中,并且比其他选项更安全(从防止愚蠢的错误造成灾难的意义上来说)。
这些-exec
选项后跟您要运行的命令,该命令{}
表示应包含find查找文件所在的位置,并通过\;
为每个文件运行一次命令或+
替换{}
为所有匹配项的参数列表而终止。注意,分号终止符被转义,因此外壳不认为它是导致新命令的分隔符。
假设您正在查找所有文本文件:
find -iname '*.txt' -execdir rm {} \;
这是查找手册(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.
一种替代方法是通过管道传输输出并使用后续命令对其进行解析。唯一安全的方法是使用-print0
选项,该选项告知find
使用空字符作为结果定界符。接收命令必须具有识别以空分隔的输入的能力。例:
find /home/phunehehe -iregex '.*\.png$' -print0 | xargs -0 file
请注意,该-0
选项告诉xargs
您将输入视为空定界。
-exec
如果以+
而不是结尾,则可以包含更多文件;
。请参阅caleb的答案。
您可以使用xargs
命令完成此操作。xargs
本质上来说,针对其标准输入的每条指令运行一次命令。因此,例如,如果您需要删除.jpg
目录中的所有文件,那么在命令行上的一种快速方法是:
$ find ./ -name "*.jpg" | xargs rm
您还可以使用反引号(“ Tab”按钮上方)进行此操作(请注意,这是反引号字符,而不是单引号字符):
$ rm `find ./ -name "*.jpg"`
请注意,由于xargs
外壳程序处理输入的方式不同,xargs方法仅在所涉及的文件名和目录名都不包含空格或以下任何一种时才有效\"'
。仅当所涉及的文件名和目录名都不包含空格或任何空格时,backquote方法才有效\[?*
。
find
,这-exec
通常是一个更好的解决方案。如果要指定这些替代名称,请至少说明如何find -print0 | xargs -0
用于安全文件名中断处理,并详细说明何时应注意反引号。