Solved

Linux script to clean up files older than 30 days

Posted on 2011-03-02
5
804 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 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
Configuring Apache Camel to send and recieive from QPID Broker 2 34
How to enable sync between two yum repo? 1 55
android secure ftp 3 74
linux 13 109
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…
Fine Tune your automatic Updates for Ubuntu / Debian
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 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