IT is Fun

May 27, 2011

Linux/Unix Shell Script – Delete files from a folder based on date

Filed under: Linux/Unix — themamakcorner @ 4:22 am

Say there’s are alot files being created in a folder and it causes full disk space problem, here’s one of the example to remove the files that is older that 2 days:

find /home/user/yourpath/ -mtime +2 -type f -exec rm -rf {} \;

Or if you want to maintain that as a scheduled cron job, here’s the example of full code that you can use:

#!/bin/sh

###############################################
## Shell script to run housekeep glassfish   ##
## delete server.log older than x days ago   ##
## Date      : 24-May-11                     ##
###############################################

##declaring variables
echo “HOUSEKEEP started…”
A_LOG_PATH=~/opt/yourpath/
PAST=2

find $A_LOG_PATH -mtime +$PAST -type f -exec rm -rf {} \;

ehco “HOUSEKEEP ends…”

Advertisement

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Banana Smoothie. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.