如何使用 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 {} +