5

Is it possible to make a script that will delete files in a specific folder, after five days without deleting the folder(s).

I am using Ubuntu server 12 without gui interface.

3 Answers 3

11

To find files last modified more than five days ago:

find /path/to/directory -type f -mtime +4

To delete them at the same time (test the above first):

find /path/to/directory -type f -mtime +4 -delete

To automate it so that you're running this daily, run crontab -e (as the user you wish to run this task) and add:

@daily find /path/to/directory -type f -mtime +4 -delete

An experiment to show which mtime value you want to use. The manual is rubbish. I had assumed +5 was what we wanted but a comment disagreed. The documentation appears to conflict so I started by creating three files, each 1 day apart:

$ mkdir test
$ touch test/now
$ touch -d "1 day ago" test/yesterday
$ touch -d "2 days ago" test/day-before-yesterday
$ ls -l test
total 0
-rw-r--r-- 1 oli oli 0 Nov 26 13:12 day-before-yesterday
-rw-r--r-- 1 oli oli 0 Nov 28 13:11 now
-rw-r--r-- 1 oli oli 0 Nov 27 13:12 yesterday

If we want something that is between 24-48 hours old exactly, we use 1 without a sign.

$ find test -mtime 1
test/yesterday

But if we want things 24hours +, +1 doesn't work:

$ find test -mtime +1
test/day-before-yesterday

+1 seems to mean "more than what 1 shows"... So +0 is used:

$ find test -mtime +0
test/day-before-yesterday
test/yesterday

All very annoying if you're used to being correct like me... But I'll recover, I'm sure. Thanks for the poke from Adaephon.

4
  • 1
    It probably should be -mtime +4. -mtime +5 would mean 'modified more than 5 days ago' and as any fractional parts are ignore that is essentially 'modified at least 6 days ago'.
    – Adaephon
    Nov 28, 2013 at 12:39
  • @Adaephon The man find definition is really bloody confusing. The GNU docs say the same things but also give examples. 1 means “between 24 and 48 hours ago” would suggest that using +5 is correct. It means at least 5x24 hours old.
    – Oli
    Nov 28, 2013 at 12:54
  • yes it really isn't that clear. But the meaning of +n is "more than n" not "at least n" and as fractions are ignored, this means that +1 means "more than 48 hours ago". This also means that if you want to say "more than one day ago" you have to write +0. From the ubuntu man page on find: to match -atime +1, a file has to have been accessed at least two days ago.
    – Adaephon
    Nov 28, 2013 at 13:07
  • @Adaephon I decided to do a quick test to prove it one way or the other. Long story short: Congratulations, you've won an internet.
    – Oli
    Nov 28, 2013 at 13:20
1

Yes you can, use cron jobs Suppose you need to remove files in /tmp/test/ directory after five days then

crontab -e

now add these lines to the last

@weekly rm -fr /tmp/test/

Now the folder will be deleted weekly. For more info click here

1

What you need is something like

@weekly find /tmp/test -type f -exec rm '{}' +

Note: just rm -rf removes folders too. You could add -mtime for modification time.

Does it has to be five days exactly. Get your favorite days or times in:

Minute - Hour - Day of Month - Month - Day of Week

0 3 0,5,10,15,20,25 * *

Which means 3 AM each five days unless the month is 31 days.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .