91

I have this command that I run every 24 hours currently.

find /var/www/html/audio -daystart -maxdepth 1 -mtime +1 -type f -name "*.mp3" -exec rm -f {} \;

I would like to run it every 1 hour and delete files that are older than 1 hour. Is this correct:

find /var/www/html/audio -daystart -maxdepth 1 -mtime **+0.04** -type f -name "*.mp3" -exec rm -f {} \;

I am not sure of my use of the decimal number??

Thanks for any corrections.

EDIT

OR could I just use -mmin 60? Is this correct?

EDIT2

I tried your test, good thing you suggested it. I got an empty result. I want all files OLDER than 60mins to be deleted! How can I do this?? Does my command actually do this?

1
  • 2
    If you are using GNU find (and you most likely are) you can also pass the -delete flag instead of the -exec rm business. I think that more clearly expresses the intent. Nov 16, 2011 at 10:32

1 Answer 1

173

What about -mmin?

find /var/www/html/audio -daystart -maxdepth 1 -mmin +59 -type f -name "*.mp3" \
    -exec rm -f {} \;

From man find:

-mmin n
        File's data was last modified n minutes ago.

Also, make sure to test this first!

... -exec echo rm -f '{}' \;
          ^^^^ Add the 'echo' so you just see the commands that are going to get
               run instead of actual trying them first.
7
  • 16
    Wouldn't -mmin 60 only find the files modified exactly 60 minutes ago? I think it needs to be -mmin +59 or such.
    – Otis
    Feb 12, 2009 at 23:17
  • I updated based on Otis' comments. Nice catch! Feb 12, 2009 at 23:21
  • Thanks. :) I'm curious if the modification needs to be 60 minutes or greater or if 59m 1s would trip it. I'm not sure it needs to be that precise for what Abs is doing.
    – Otis
    Feb 12, 2009 at 23:24
  • 3
    I'll let you know in 54 minutes and 12 seconds ;-) Otis++ on a random post of yours Feb 12, 2009 at 23:25
  • 31
    instead of -exec rm -f {} \; you can simply use -delete
    – denis2342
    Nov 26, 2013 at 9:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.