我想删除特定文件夹中大于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; rm file1; rm file2; 。
那么为什么没有第一个工作?
回答:
我想我只需要RTFM几次,终于明白这是什么意思。 即使第一个示例没有显示{},但在所有情况下都需要大括号。 然后添加\; 或+取决于所需的方法。 不要只读标题。 阅读说明以及。 得到它了。
你可以使用任何forms:
find . -size +2M -exec rm {} + find . -size +2M -exec rm {} \;
分号应该逃脱!
-exec rm {} \;
你可以用..找人
-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.
为了提高效率,通常使用xargs更好:
$ find /path/to/files -size +2M -print0 | xargs -0 rm
如文档所述,-exec需要{}作为find的输出的占位符。
使用bash和GNU工具的权威指南就在这里
正如你所看到的,它明确地显示了你用作例子的第二个命令。
我根本不用-exec。 find也可以删除文件本身:
find . -size +2M -delete
(尽pipe这可能是一个GNUism,不知道你是否可以在non-gnufind这个)