bash 如何使用 shell 在 Linux 中删除超过 7 天的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15431587/
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 delete files older than 7 days in Linux using the shell
提问by Guardian
I want to delete the files older than 7 days in Linux using the shell. I am using this code to do that is that correct?
我想使用 shell 删除 Linux 中超过 7 天的文件。我正在使用此代码来做到这一点,对吗?
find $OUTPUTDIR -type f -mtime +7 -delete
回答by Guardian
It's correct. However, I've just tested that and it looks like it doesn't only rely on the date info, but also on the hours and minutes. +7will then remove the files older than 168hours. I have some similar set up, please have a look on that:
这是正确的。但是,我刚刚对其进行了测试,看起来它不仅依赖于日期信息,还依赖于小时和分钟。+7然后将删除早于168小时的文件。我有一些类似的设置,请看一下:
root@it-pbx01:/var/lib/asterisk/backups/BACKUP# date
Fri Mar 15 12:51:03 CET 2013
root@it-pbx01:/var/lib/asterisk/backups/BACKUP# ls -l
total 872780
-rw-rw-r-- 1 asterisk asterisk 128513903 Mar 8 18:01 20130308.18.00.02.tar.gz
-rw-rw-r-- 1 asterisk asterisk 128517514 Mar 9 18:01 20130309.18.00.01.tar.gz
-rw-rw-r-- 1 asterisk asterisk 128517659 Mar 10 18:01 20130310.18.00.01.tar.gz
-rw-rw-r-- 1 asterisk asterisk 126791825 Mar 11 18:01 20130311.18.00.01.tar.gz
-rw-rw-r-- 1 asterisk asterisk 126791573 Mar 12 18:01 20130312.18.00.01.tar.gz
-rw-r--r-- 1 asterisk asterisk 126791404 Mar 13 18:01 20130313.18.00.02.tar.gz
-rw-r--r-- 1 asterisk asterisk 126871966 Mar 14 18:01 20130314.18.00.01.tar.gz
root@it-pbx01:/var/lib/asterisk/backups/BACKUP# find . -type f -mtime +7
root@it-pbx01:/var/lib/asterisk/backups/BACKUP# find . -type f -mtime +6
root@it-pbx01:/var/lib/asterisk/backups/BACKUP# find . -type f -mtime +5
./20130308.18.00.02.tar.gz
root@it-pbx01:/var/lib/asterisk/backups/BACKUP# find . -type f -mtime +4
./20130309.18.00.01.tar.gz
./20130308.18.00.02.tar.gz
root@it-pbx01:/var/lib/asterisk/backups/BACKUP# find . -type f -mtime +3
./20130309.18.00.01.tar.gz
./20130310.18.00.01.tar.gz
./20130308.18.00.02.tar.gz
root@it-pbx01:/var/lib/asterisk/backups/BACKUP# find . -type f -mtime +2
./20130309.18.00.01.tar.gz
./20130311.18.00.01.tar.gz
./20130310.18.00.01.tar.gz
./20130308.18.00.02.tar.gz
root@it-pbx01:/var/lib/asterisk/backups/BACKUP# find . -type f -mtime +1
./20130309.18.00.01.tar.gz
./20130311.18.00.01.tar.gz
./20130312.18.00.01.tar.gz
./20130310.18.00.01.tar.gz
./20130308.18.00.02.tar.gz
root@it-pbx01:/var/lib/asterisk/backups/BACKUP# find . -type f -mtime +0
./20130309.18.00.01.tar.gz
./20130311.18.00.01.tar.gz
./20130312.18.00.01.tar.gz
./20130313.18.00.02.tar.gz
./20130310.18.00.01.tar.gz
./20130308.18.00.02.tar.gz
root@it-pbx01:/var/lib/asterisk/backups/BACKUP# find . -type f -mtime 0
./20130314.18.00.01.tar.gz

