?
Solved

Linux script to clean up files older than 30 days

Posted on 2011-03-02
5
Medium Priority
?
816 Views
Last Modified: 2012-05-11
I need some help creating a script that would automatically check a folder and remove/delete all files older than 30 days from the creation date of the file.

0
Comment
Question by:ipconfig610
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 16

Expert Comment

by:santoshmotwani
ID: 35017711
Hope this helps :

find /path/to/files -mtime +30 -exec rm  {}\;
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35017740
There is no "create time" value, unfortunately.

There is only access time, change time and modification time.

To use the modification time you could do:

find /folder/name -maxdepth 1 -type f -mtime +30 -print0 | xargs -0 rm

The above will not recurse into subdirectories. If recursion is desired omit "maxdepth 1"

wmp
0
 

Author Comment

by:ipconfig610
ID: 35018030
woolmilkporc: "There is only access time, change time and modification time."

These files are never modified or changed since they are voicemail messages in WAV format on the server.  The script will be run as a daily cron job that will check folders and auto clean up messages which were left more than 30 days ago.  In this case the change time and modification time would actually be the same as the create time.

Also no need for recursively since there are no folders in the directory we are looking in.

0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 total points
ID: 35018073
OK then,

use mtime, and there is no difference between using maxdepth and not using it if there are no subdirectories.

You could try

find /folder/name -maxdepth 1 -type f -mtime +30 -print0 | xargs -0 echo rm

to have the "rm" statement(s) just printed, not executed - kind of a "dry run".

wmp


0
 

Author Closing Comment

by:ipconfig610
ID: 35018138
Fast and perfect!!
0

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

649 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