Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 14 down vote accepted

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

share|improve this answer
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
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
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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