Daniel Hollands
Daniel Hollands

Reputation: 6671

Bash command to delete everything older than half a day

I've got a simple bash script, cron'd to run at midnight each night, which creates a backup or files and stores them as a .tar.gz in my Dropbox. Before this happens, however, I need the script to delete the previous night's backup.

To do this I'm currently running this command:

find ~/Dropbox/Backups/casper/* -mtime +0.5 -exec rm {} \;

Which to my mind should delete anything older than half a day - but it doesn't seem to work (it keeps the previous nights back-up, but deletes anything before this)

Can someone point me in the right direction please? Thank you

Upvotes: 1

Views: 2103

Answers (1)

Douglas Leeder
Douglas Leeder

Reputation: 53320

From the manpage for find:

-mtime n
          File's data was last modified n*24 hours ago.  See the comments for -atime to understand how rounding  affects  the
          interpretation of file modification times.

-atime n
          File  was  last  accessed  n*24  hours  ago.   When find figures out how many 24-hour periods ago the file was last
          accessed, any fractional part is ignored, so to match -atime +1, a file has to have been accessed at least two days
          ago.

From this we can see that the 0.5 is dropped, then 1 day ago is required. You probably want to use -mmin instead.

For example (from babah):

# 720 is 60 times 12
find ~/Dropbox/Backups/casper/* -mmin 720 -print -exec rm {} \;

Upvotes: 1

Related Questions