Found a few similar questions but were not quite a match.

I have a directory for backups (sql) and want to delete all files in that directory older then 7 days leaving any sub-directories intact.

This is what I have:

find /var/log/mbackups -mtime +7 -type f -delete

Is this the proper way to accomplish what I am after?

  • 2
    What do you mean by "leaving any sub-directories intact"? Do you mean "don't delete files from sub-directories" or do you mean "don't delete the sub-directories themselves"?
    – Kusalananda
    Jun 1, 2018 at 17:03
  • 3
    Note the gotcha about -mtime +N. With GNU find, -mtime +7 will find files that are at least eight days old.
    – ilkkachu
    Jun 1, 2018 at 17:07
  • 1
    @ilkkachu, that's not limited to GNU find. It is a POSIX requirement (though IIRC some BSDs are/were not compliant in that regard). May 10, 2022 at 12:35
  • @StéphaneChazelas, oh, okay. Should try to remember that for the next time, thankss.
    – ilkkachu
    May 10, 2022 at 12:46

5 Answers 5


Your command will look at the top level directory /var/log/mbackups and also descend into any subdirectories, deleting files that match the seven day criterion. It will not delete the directories themselves.

If you want a command to look at files only in the /var/log/mbackups directory, and not descend into subdirectories, you need to add that restriction:

find /var/log/mbackups -maxdepth 1 -mtime +7 -type f -delete

In general you can test the find command by replacing the -delete action with something innocuous, like -print (you need -depth here too, as -delete implies -depth):

find /var/log/mbackups -depth -mtime +7 -type f -print
  • 1
    For files older than 7 days, you need -mtime +6 (or '(' -mtime 7 -o -mtime +7 ')'), not -mtime +7. May 10, 2022 at 11:53
  • 1
    Also note that -delete implies -depth. So, when testing with -print, it's best to include a -depth so you get the same thing as what -delete would do (very important if also using -prune which is ineffective when combined with -depth) May 10, 2022 at 11:58
  • Note that while -delete is a BSD extension, -maxdepth is a GNU extension, though these days both are supported by GNU and most BSDs. May 10, 2022 at 11:59
  • @StéphaneChazelas if you're using a granularity of days, then +7 will be files that are 8 days or older, which answers the question in the way that I have offered. If you're using a granularity of seconds then your suggestion would be correct. That's the wonder of English language ambiguity May 10, 2022 at 12:01

With zsh:

rm -f /var/log/mbackups/*(.m+6)

same as:

rm -f /var/log/mbackups/*(.^m-7)

Would remove the regular (with .) files that are not hidden and haven't been modified in the last 7 days. Like for find's -mtime +7, m+7 would match on files whose age rounded down to the next integer number of days is strictly greater than 7, so would match on files that are 8 days old or older (a common pitfall when working with find).

To also remove old hidden files, add the D glob qualifier.

If there's no matching file, you'll get an error message. You can avoid it by adding the N glob qualifier (and rm will then be called without filename argument, but with -f, it should not complain about it).


Disclaimer: I'm the current author of rawhide (rh) (see https://github.com/raforg/rawhide)

Almost. It's very close. The correct version of your attempt is:

find /var/log/mbackups -maxdepth 1 -mtime +6 -type f -delete

If you find find's -mtime predicate argument insufficiently intuitive, you could use rawhide (rh) instead:

rh -UUU -r /var/log/mbackups 'f && old(7*days)'

-UUU unlinks/removes/deletes matches.

-r is like find's -mindepth 1 -maxdepth 1 and only searches exactly one level below the search path(s), so sub-directories are not searched.

f matches regular files.

old(7*days) matches things last modified at least 7 days ago.


try this

find /var/log/mbackups -type f -mtime +7 -exec rm -rf {} \;
  • Any reason why you would use that instead of the command in the question? Note that the question asks "is this the proper way?", not "are there any other ways?".
    – Kusalananda
    Apr 15, 2020 at 10:48

Warning: -delete is non-standard (nonportable) and risky when using gfind.

Remember that rm has some security restrictions, but the gfind man page does not mention such a restrictions for -delete. I therefore recommend to use the official method:

find /var/log/mbackups -mtime +7 -type f -exec rm {} +

This is not even slower than the non-standard method using -delete.

  • 2
    rm will happily delete a file beginning with ./, for example touch xx; rm ./xx Jun 6, 2018 at 19:04
  • -delete is from BSD, not GNU and was especially designed to overcome the limitations and security issues of -exec rm. Also note that the OP asked files in subdirectories be left alone. May 10, 2022 at 12:33

You must log in to answer this question.

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