bash 在bash中查找早于X天的文件并删除

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20238183/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:27:51  来源:igfitidea点击:

find files older than X days in bash and delete

bashfind

提问by user3040975

I have a directory with a few TB of files. I'd like to delete every file in it that is older than 14 days.

我有一个包含几 TB 文件的目录。我想删除其中超过 14 天的每个文件。

I thought I would use find . -mtime +13 -delete. To make sure the command works as expected I ran find . -mtime +13 -exec /bin/ls -lh '{}' \; | grep '<today>'. The latter should return nothing, since files that were created/modified today should not be found by findusing -mtime +13. To my surprise, however, findjust spew out a list of all the files modified/created today!

我以为我会使用find . -mtime +13 -delete. 为了确保命令按预期工作,我运行了find . -mtime +13 -exec /bin/ls -lh '{}' \; | grep '<today>'. 后者不应该返回任何内容,因为今天创建/修改的文件不应该find使用-mtime +13. 然而,令我惊讶的是,find只是列出了今天修改/创建的所有文件的列表!

回答by Mindx

find your/folder -type f -mtime +13 -exec rm {} \;

回答by stones333

This works for me.

这对我有用。

$ find ./folder_name/*  -type f -mtime +13 -print | xargs rm -rf