Bash:根据文件日期戳删除
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3676810/
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
Bash: delete based on file date stamp
提问by David Oneill
I have a folder with a bunch of files. I need to delete all the files created before July 1st. How do I do that in a bash script?
我有一个文件夹,里面有一堆文件。我需要删除 7 月 1 日之前创建的所有文件。我如何在 bash 脚本中做到这一点?
回答by bramp
I think the following should do what you want:
我认为以下应该做你想做的:
touch -t 201007010000 dummyfile
find /path/to/files -type f ! -newer dummyfile -delete
The first line creates a file which was last modified on the 1st July 2010. The second line finds all files in /path/to/file which has a date not newer than the dummyfile, and then deletes them.
第一行创建一个最后一次修改是在 2010 年 7 月 1 日的文件。第二行在 /path/to/file 中查找日期不比 dummyfile 新的所有文件,然后将其删除。
If you want to double check it is working correctly, then drop the -deleteargument and it should just list the files which would be deleted.
如果你想仔细检查它是否正常工作,然后删除-delete参数,它应该只列出将被删除的文件。
回答by Eric Wendelin
This should work:
这应该有效:
find /file/path ! -newermt "Jul 01"
To find the files you want to delete, so the command to delete them would be:
要找到要删除的文件,删除它们的命令是:
find /file/path ! -newermt "Jul 01" -type f -print0 | xargs -0 rm

