Solved

Linux script to clean up files older than 30 days

Posted on 2011-03-02
5
800 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
  • 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
nagios remote hosts 9 43
nagios alerts 3 32
Which is more secure: EAP or machine certificate for IKEv2 VPN? 1 40
VMware Workstation 12 Player 16 35
I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now