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

x
?
Solved

How to delete files by age

Posted on 2007-10-17
8
Medium Priority
?
1,934 Views
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!
0
Comment
Question by:dkreines
8 Comments
 
LVL 12

Accepted Solution

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

Expert Comment

by:Heem14
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

0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20095784
Hi,

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).

0
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

by:Tintin
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 {} \;

The

-type f

specifies to only match files.

Heem14.

ctime is not creation time, it's change time.  Unix/Linux filesystems don't store file creation time.
0
 
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.
0
 
LVL 3

Expert Comment

by:4dimach
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 {} \;
0
 
LVL 35

Expert Comment

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

Expert Comment

by:mkuutti
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.
0

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