Solved

Linux script to clean up files older than 30 days

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
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 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…

808 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