[Last Call] Learn how to a build a cloud-first strategyRegister Now


How to delete files by age

Posted on 2007-10-17
Medium Priority
Last Modified: 2013-12-16
I am running a Linux system (but I don't know much Linux) and I would like to be able to have a script that will delete all files in a directory that are older than 5 days.  Can anyone suggest a way to do this?  Thanks in advance!
Question by:dkreines
LVL 12

Accepted Solution

Heem14 earned 600 total points
ID: 20093084
find /path/to/files -mtime +5 -exec rm -f {} \;
LVL 12

Expert Comment

ID: 20093087
oh, I should note that will delete anything not modified for the past 5 days. To do when it was created, change mtime to ctime

LVL 40

Expert Comment

ID: 20095784

If you are not sure about the files to be deleted, you may add the -i option to the rm command to confirm before deleting (but this wii prompt for each file, and if the number of files to be deleted is large, then you spend some time doing so).

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 48

Expert Comment

ID: 20096594
find /path/to/files -mtime +5 -exec rm -f {} \;

should really be

find /path/to/files -mtime +5 -type f -exec rm -f {} \;


-type f

specifies to only match files.


ctime is not creation time, it's change time.  Unix/Linux filesystems don't store file creation time.
LVL 35

Assisted Solution

by:Duncan Roe
Duncan Roe earned 400 total points
ID: 20097317
-exec invokes the rm program for every file. More efficient would be:

find /path/to/files -mtime +5 -print | xargs rm

Depending on your circumstances, you might want to consider using -atime (last access time) to avoid deleting older files that are still potentially useful. You need to use --atime-preserve in your tar backups for the access time to be possibly useful.

Expert Comment

ID: 20109083
Before executing them look at 'man find' and search for -mtime, -ctime, -atime options. Read them and decide what files you want to delete. DON'T run in on any system files.

Make sure that you will delete right files:

find /path/to/files -ctime[mtime][atime] +5 -type f

If you see right files in the output, only them add -exec rm -fv {} \;
LVL 35

Expert Comment

by:Duncan Roe
ID: 20112954
Or add | xargs rm -fv

Expert Comment

ID: 20138058
I use tmpreaper package (in debian) to clear certain tmp-dirs automatically. It uses commands described above, It looks to subdirs and does not recursive delete dirs if they contain files modified/accessed desired days ago. It has also "blacklist" to prevent certain files to be deleted.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month17 days, 20 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question