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

I'd like to run a nightly cron job that deletes all the files in a folder that haven't been accessed in a week or more. What is the most efficient way to do this in bash?

share|improve this question

migrated from stackoverflow.com Jul 13 '11 at 1:01

This question came from our site for professional and enthusiast programmers.

find can filter based on atime. The "correctness" of atime depends upon configuration (see noatime), at the very least. – pst Jul 12 '11 at 23:40
up vote 15 down vote accepted

You want the find tool.

find folder -depth -type f -atime +7 -delete

(This will delete all files (only regular ones, no pipes, special devices, directories, symbolic links) in the given folder and all subdirectories (recursively) where the last access time is longer than 7 days ago.)

share|improve this answer
You might want to add -type f in there to ensure it only deletes files, not entire directories. – Shadur Jul 13 '11 at 4:42

You may want to check out tmpwatch which can be put into a cron job. No need to roll-your-own with find. On RHEL it's in the tmpwatch RPM.

share|improve this answer
This is definitely the more elegant solution, but I prefer the portability of find. – bloudermilk Jul 15 '11 at 3:21

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.