bash 如何使用 find 和 rm 命令删除文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31769658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove files using find and rm command?
提问by PRK
find -mmin -19 -exec rm '{}'\;
It will find the modified files 1st and then remove them. but it gives me error as below, find: missing argument to `-exec'
它将首先找到修改过的文件,然后将其删除。但它给了我如下错误,find:缺少`-exec'的参数
Also tried various combinations like,
还尝试了各种组合,例如,
find -mmin -19 -exec rm '{}';\
find -mmin -19 -exec rm '{}'/;
回答by falsetru
You need space between the command and \;
您需要在命令和 \;
find -mmin -19 -exec rm {} \;
find
already provide -delete
option, so you don't need to use -exec rm ..
:
find
已经提供-delete
选项,所以你不需要使用-exec rm ..
:
find -mmin -19 -delete
-delete
Delete files; true if removal succeeded. If the removal failed, an error message is issued. If -delete fails, find's exit status will be nonzero (when it eventually exits). Use of -delete automatically turns on the -depth option.
Warnings: Don't forget that the find command line is evaluated as an expression, so putting -delete first will make find try to delete everything below the starting points you specified. When testing a find command line that you later intend to use with -delete, you should explicitly specify -depth in order to avoid later surprises. Because -delete implies -depth, you cannot usefully use -prune and -delete together.
-删除
删除文件; 如果删除成功,则为 true。如果删除失败,则会发出错误消息。如果 -delete 失败,find 的退出状态将为非零(当它最终退出时)。使用 -delete 会自动打开 -depth 选项。
警告:不要忘记 find 命令行被评估为表达式,因此将 -delete 放在首位将使 find 尝试删除您指定的起点以下的所有内容。在测试您以后打算与 -delete 一起使用的 find 命令行时,您应该明确指定 -depth 以避免以后出现意外。因为 -delete 暗示 -depth,所以不能有效地将 -prune 和 -delete 一起使用。
回答by Jasen
You're missing an essential space to separate the braces from the semicolon.
您缺少将大括号与分号分开的重要空间。
find -mmin -19 -exec rm '{}' \;
but this does the same ting, is easier to type, and probably executes faster.
但这也有同样的效果,更容易打字,而且可能执行得更快。
find -mmin -19 -delete
回答by Tom Inchesgrim
Here is another option. Specify the date from which we want to delete the files:
这是另一种选择。指定我们要删除文件的日期:
find /SYSADMIT/* -type f -not -newermt "AAAA:MM:DD HH:MI:SS" -delete
Extracted from: https://www.sysadmit.com/2019/08/linux-borrar-ficheros-por-fecha.html
摘自:https: //www.sysadmit.com/2019/08/linux-borrar-ficheros-por-fecha.html