0

我想使用 shell 删除 Linux 中超过 7 天的文件。我正在使用此代码来做到这一点,对吗?

find $OUTPUTDIR -type f -mtime +7 -delete
4

1 回答 1

2

这是正确的。但是,我刚刚对此进行了测试,看起来它不仅依赖于日期信息,还依赖于小时和分钟。然后+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
于 2013-03-15T11:49:33.537 回答