Is there a command to delete all the files in a directory that haven't been modified in N days? I need to clean up some old logs.


This will delete all files older than 5 days, you can put a -name '*log' in there too to be more precise and you might want to specify a maxdepth in the find command too.

find /some/dir -type f -mtime +5 -delete

  • 5
    Although wise sysadmins do a dry-run with -ls instead of -delete to make sure this won't delete anything they wanted kept. Yes, speaking from experience here. – Shadur May 5 '12 at 14:21
  • 2
    Speaking from experience, they also check their path. or not, since I once did the / some/dir/ version. :( – Hennes Jan 13 '13 at 22:34

With zsh and (.m+n) glob qualifiers: . selects only regular files, m+n selects files modified more than n days ago;
e.g. list the files in the current directory that were modified more than 9 days ago:

print -rl -- *(.m+9)

add D to include dot files:

print -rl -- *(D.m+9)

or if you want to recurse (and list e.g. only files ending with .log):

setopt extendedglob
print -rl -- **/*.log(.m+9)

replace print -rl with rm if you're happy with the result; though you may have to use zargs if you have many files (to avoid arguments list too long):

autoload zargs
zargs ./**/*(.m+9) -- rm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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