[bash] How to delete files older than X hours

I'm writing a bash script that needs to delete old files.

It's currently implemented using :

find $LOCATION -name $REQUIRED_FILES -type f -mtime +1 -delete

This will delete of the files older than 1 day.

However, what if I need a finer resolution that 1 day, say like 6 hours old? Is there a nice clean way to do it, like there is using find and -mtime?

This question is related to bash

The answer is


Here is what one can do for going on the way @iconoclast was wondering about in their comment on another answer.

use crontab for user or an /etc/crontab to create file /tmp/hour:

# m h dom mon dow user  command
0 * * * * root /usr/bin/touch /tmp/hour > /dev/null 2>&1

and then use this to run your command:

find /tmp/ -daystart -maxdepth 1 -not -newer /tmp/hour -type f -name "for_one_hour_files*" -exec do_something {} \;

-mmin is for minutes.

Try looking at the man page.

man find

for more types.


Here is the approach that worked for me (and I don't see it being used above)

$ find /path/to/the/folder -name *.* -mmin +59 -delete > /dev/null

deleting all the files older than 59 minutes while leaving the folders intact.


Here is the approach that worked for me (and I don't see it being used above)

$ find /path/to/the/folder -name *.* -mmin +59 -delete > /dev/null

deleting all the files older than 59 minutes while leaving the folders intact.


If one's find does not have -mmin and if one also is stuck with a find that accepts only integer values for -mtime, then all is not necessarily lost if one considers that "older than" is similar to "not newer than".

If we were able to create a file that that has an mtime of our cut-off time, we can ask find to locate the files that are "not newer than" our reference file.

To create a file that has the correct time stamp is a bit involved because a system that doesn't have an adequate find probably also has a less-than-capable date command that could do things like: date +%Y%m%d%H%M%S -d "6 hours ago".

Fortunately, other old tools that can manage this, albeit in a more unwieldy way.

Consider that six hours is 21600 seconds. We want to find the time that is six hours ago in a format that is useful:

$ date && perl -e '@d=localtime time()-21600; \
  printf "%4d%02d%02d%02d%02d.%02d\n", $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]'
> Thu Apr 16 04:50:57 CDT 2020
202004152250.57

The perl statement did produce a useful date, but it has to be put to better use:

$ date && touch -t `perl -e '@d=localtime time()-21600; \
  printf "%4d%02d%02d%02d%02d.%02d\n", \
  $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]'` ref_file && ls -l ref_file
Thu Apr 16 04:53:54 CDT 2020
-rw-rw-rw-   1 root     sys            0 Apr 15 22:53 ref_file

Now the solution for this old UNIX is something along the lines of:

$ find . -type f ! -newer ref_file -a ! -name ref_file -exec rm -f "{}" \;

It might also be a good idea to clean up our reference file...

$ rm -f ref_file

You could to this trick: create a file 1 hour ago, and use the -newer file argument.

(Or use touch -t to create such a file).


For SunOS 5.10

 Example 6 Selecting a File Using 24-hour Mode


 The descriptions of -atime, -ctime, and -mtime use the  ter-
 minology n ``24-hour periods''. For example, a file accessed
 at 23:59 is selected by:


   example% find . -atime -1 -print




 at 00:01 the next day (less than 24 hours  later,  not  more
 than one day ago). The midnight boundary between days has no
 effect on the 24-hour calculation.

If one's find does not have -mmin and if one also is stuck with a find that accepts only integer values for -mtime, then all is not necessarily lost if one considers that "older than" is similar to "not newer than".

If we were able to create a file that that has an mtime of our cut-off time, we can ask find to locate the files that are "not newer than" our reference file.

To create a file that has the correct time stamp is a bit involved because a system that doesn't have an adequate find probably also has a less-than-capable date command that could do things like: date +%Y%m%d%H%M%S -d "6 hours ago".

Fortunately, other old tools that can manage this, albeit in a more unwieldy way.

Consider that six hours is 21600 seconds. We want to find the time that is six hours ago in a format that is useful:

$ date && perl -e '@d=localtime time()-21600; \
  printf "%4d%02d%02d%02d%02d.%02d\n", $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]'
> Thu Apr 16 04:50:57 CDT 2020
202004152250.57

The perl statement did produce a useful date, but it has to be put to better use:

$ date && touch -t `perl -e '@d=localtime time()-21600; \
  printf "%4d%02d%02d%02d%02d.%02d\n", \
  $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]'` ref_file && ls -l ref_file
Thu Apr 16 04:53:54 CDT 2020
-rw-rw-rw-   1 root     sys            0 Apr 15 22:53 ref_file

Now the solution for this old UNIX is something along the lines of:

$ find . -type f ! -newer ref_file -a ! -name ref_file -exec rm -f "{}" \;

It might also be a good idea to clean up our reference file...

$ rm -f ref_file

You could to this trick: create a file 1 hour ago, and use the -newer file argument.

(Or use touch -t to create such a file).


-mmin is for minutes.

Try looking at the man page.

man find

for more types.


Here is what one can do for going on the way @iconoclast was wondering about in their comment on another answer.

use crontab for user or an /etc/crontab to create file /tmp/hour:

# m h dom mon dow user  command
0 * * * * root /usr/bin/touch /tmp/hour > /dev/null 2>&1

and then use this to run your command:

find /tmp/ -daystart -maxdepth 1 -not -newer /tmp/hour -type f -name "for_one_hour_files*" -exec do_something {} \;

If you do not have "-mmin" in your version of "find", then "-mtime -0.041667" gets pretty close to "within the last hour", so in your case, use:

-mtime +(X * 0.041667)

so, if X means 6 hours, then:

find . -mtime +0.25 -ls

works because 24 hours * 0.25 = 6 hours


If you do not have "-mmin" in your version of "find", then "-mtime -0.041667" gets pretty close to "within the last hour", so in your case, use:

-mtime +(X * 0.041667)

so, if X means 6 hours, then:

find . -mtime +0.25 -ls

works because 24 hours * 0.25 = 6 hours


find $PATH -name $log_prefix"*"$log_ext -mmin +$num_mins -exec rm -f {} \;


-mmin is for minutes.

Try looking at the man page.

man find

for more types.


For SunOS 5.10

 Example 6 Selecting a File Using 24-hour Mode


 The descriptions of -atime, -ctime, and -mtime use the  ter-
 minology n ``24-hour periods''. For example, a file accessed
 at 23:59 is selected by:


   example% find . -atime -1 -print




 at 00:01 the next day (less than 24 hours  later,  not  more
 than one day ago). The midnight boundary between days has no
 effect on the 24-hour calculation.

You could to this trick: create a file 1 hour ago, and use the -newer file argument.

(Or use touch -t to create such a file).


-mmin is for minutes.

Try looking at the man page.

man find

for more types.


You could to this trick: create a file 1 hour ago, and use the -newer file argument.

(Or use touch -t to create such a file).


find $PATH -name $log_prefix"*"$log_ext -mmin +$num_mins -exec rm -f {} \;