Solved

Linux script to clean up files older than 30 days

Posted on 2011-03-02
5
796 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
Comment Utility
Hope this helps :

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

Expert Comment

by:woolmilkporc
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Fast and perfect!!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this tutorial I will explain how to make squid prevent malwares in five easy steps: Squid is a caching proxy for the Web supporting HTTP, HTTPS, FTP, and more. It reduces bandwidth and improves response times by caching and reusing frequently-…
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.…
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…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

763 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

6 Experts available now in Live!

Get 1:1 Help Now