0

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?

find $OUTPUTDIR -type f -mtime +7 -delete
Peter G.
  • 14,786
  • 7
  • 57
  • 75
  • 3
    Why don't you just try it? :) You can set the mtime of a file using `touch -t` as described here: http://www.unixtutorial.org/2008/11/how-to-update-atime-and-mtime-for-a-file-in-unix/ – filmor Mar 15 '13 at 11:47
  • 2
    Looks correct. Is there any reason you suspect it might be wrong, or are you just afraid to try it because there's a `-delete` in it? – Kilian Foth Mar 15 '13 at 11:47
  • @KilianFoth yes i was afraid to run that script. –  Mar 15 '13 at 12:05

1 Answers1

2

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. +7 will then remove the files older than 168 hours. I have some similar set up, please have a look on that:

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
Guardian
  • 68
  • 5