如何使用 cron 作业删除文件夹中超过 14 天的文件?到目前为止我所尝试的一切都没有奏效。
答案1
您应该能够轻松地使用find
.只需在您的目录中运行此命令crontab
(这将删除文件和子目录):
find /path/to/target -mtime +14 -delete
从man find
-mtime n
File's data was last modified n*24 hours ago.
Numeric arguments can be specified as
+n for greater than n,
-n for less than n,
n for exactly n.
-delete
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.
我不确定是否-delete
是 POSIX 但如果您的 find 实现缺乏-delete
,您也可以使用
find /path/to/target -mtime +14 -exec rm {} +