使用“-exec rm -f {}\”时“查找:缺少`-exec'的参数”

Aks*_*tel 12 command-line find

我运行这个命令:

~/shell_temp$ find . -type f -name "IMAG1806.jpg" -exec rm -f {}\
Run Code Online (Sandbox Code Playgroud)

我得到以下输出:

> IMAG1806.jpg

Error:
find: missing argument to `-exec'
Run Code Online (Sandbox Code Playgroud)

从当前目录中查找任何文件并删除的确切命令是什么-exec

hee*_*ayl 30

你错过了一个;末(和之间存在太多的空间{};)。正确的命令是:

find . -type f -name "IMAG1806.jpg" -exec rm -f {} \;
Run Code Online (Sandbox Code Playgroud)

;表示 的-exec谓词结束find

另请注意,我们在前面使用了\;ie来逃避shell的解释,否则 shell 将视为整个命令的结尾并抛出相同的错误。您也可以使用代替。\;;;findfind';'\;

\在最后使用,这表明您的 shell 将继续通过PS2(由 表示>)接受输入,您IMAG1806.jpg再次键入,因此整个命令变为:

find . -type f -name "IMAG1806.jpg" -exec rm -f {}IMAG1806.jpg
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,这根本不是一个有效的命令,IMAG1806.jpg在结尾处,没有-exec谓词的结束,并且在{}和之间没有空格\;


小智 8

你可以简单地

find . -type f -name 'IMAGE1806.jpg' -delete
Run Code Online (Sandbox Code Playgroud)

从手册页:

Delete files; true if removal succeeded.  If the removal failed,
an  error message is issued.  If -delete fails, find's exit sta?
tus will be nonzero (when it eventually exits).  Use of  -delete
automatically turns on the -depth option.
Run Code Online (Sandbox Code Playgroud)