什么是正确的 find -exec 语法

Saf*_*ado 11 linux find

我想删除特定文件夹中大于 2MB 的文件。所以我跑了:

find . -size +2M

我得到了两个文件的列表

./a/b/c/file1

./a/f/g/file2

所以我然后运行:

find . -size +2M -exec rm ;

我收到错误信息 Find: missing argument to -exec

我检查了手册页中的语法,它说 -exec command ;

所以我尝试

find . -size +2M -exec rm {} +

它有效。我知道 {} 使它执行命令rm file1 file2而不是rm file1; rm file2;.

那么为什么第一个不起作用呢?

回答:

我想我只需要 RTFM 几次才能最终理解它在说什么。尽管第一个示例没有显示 {},但在所有情况下都需要大括号。然后要么添加 \; 或 + 取决于所需的方法。不要只看标题。也请阅读说明。知道了。

Kha*_*led 17

您可以使用以下任何一种形式:

find . -size +2M -exec rm {} +

find . -size +2M -exec rm {} \;
Run Code Online (Sandbox Code Playgroud)

分号应该被转义!


小智 11

-exec rm {} \;
Run Code Online (Sandbox Code Playgroud)

你可以用..男人找到

-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
              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 directory.   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 command  lines.   Only  one  instance  of  `{}'  is
              allowed within the command.  The command is executed in the starting directory.
Run Code Online (Sandbox Code Playgroud)