Delete files older than 3 months.(read date from the name of the file)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete files older than 3 months.(read date from the name of the file)
# 1  
Old 07-14-2008
Delete files older than 3 months.(read date from the name of the file)

Guys,
My log files stored in the date format format below(log_20080714072942):

TIMESTAMP=`date +%Y%m%d%H%M%S`
LOG=/log/log_${TIMESTAMP}.log

I'm looking for a shell script which deletes all files which is older than 3 months from today.

Regards,
Bhagat
# 2  
Old 07-14-2008
IM not gonna write the whole script for you but this should get you started:

#Current Day of Month
d=`date +%d`

#current Month
m=`date +%m`

#current Year on 4 digit format
y=`date +%Y`


TARGET=$(date -u -d '2008-07-13 00:00:00' '+%F %T.%N %Z')
CURRENT=$(date -u -d'$Y-$m-$d 00:00:00' '+%F %T.%N %Z')

#function to get the diff of 2 dates
diff () {
printf '%s' $(( $(date -u -d"$TARGET" +%s) -
$(date -u -d"$CURRENT" +%s)))
# %d = day of month.
}

# $DAYS will equal the diff of $CURRENT and $TARGET, Below numbers are 60 = Minutes per hour, 24 = Hours perday
DAYS=$(( $(diff) / 60 / 60 / 24 ))
# 3  
Old 07-14-2008
Also just an example, with mysql Smilie

Code:
mirus scripts # logdate=`echo log_20080214072942 | egrep  -o 'log_[0-9]{6}' | egrep -o '[0-9]{6}'` 
mirus scripts # echo $logdate
200802
mirus scripts # month_diff=`mysql -u root -sABNe "SELECT PERIOD_DIFF(DATE_FORMAT(CURDATE(), '%Y%m'), '$logdate')"`
mirus scripts # echo $month_diff
5

# 4  
Old 07-15-2008
If the date on the file name is the same as the last modified time you may use.

find . -name '*.log' -mtime +90 -exec rm {} \;



-rw-r--r-- 1 owner group 10322 Jul 14 23:47 log.071408
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to delete all the files older than a date?

Hi, I need a command for deleting all the compress files *.Z that are older than the current date - 5 days. Basically I have a directory where daily I meet some back up files and I want to remove automatically the ones 5 days (or more) older than the current date. How can I write a 'rm' command... (1 Reply)
Discussion started by: Francy
1 Replies

2. Shell Programming and Scripting

Move log files with date and delete older than 3 weeks

I have written a script which generate one logfile on every sunday and thursday I want to move the older log files into /tmp directory befor generating new one so i used mv command like mv usr/sbin/appl/logfile.txt usr/sbin/appl/tmp 2) But when i move this file to /tmp it will... (1 Reply)
Discussion started by: Nakul_sh
1 Replies

3. Shell Programming and Scripting

Deleting files and directory's older than 3 months

I have a qnap TS259 that is running ubuntu. Have successfully setup back scripts that are initiated by cron. I would like to create a couple scrypts that would operate on the recycle bins for both drives. Just want to be able to run the script manually that would walk through both directories... (13 Replies)
Discussion started by: mackconsult
13 Replies

4. Red Hat

Need Script to ZIP/SAVE & then DELETE Log file & DELETE ZIPS older than 12 months

ENVIROMENT Linux: Fedora Core release 1 (Yarrow) iPlanet: iPlanet-WebServer-Enterprise/6.0SP1 Log Path: /usr/iplanet/servers/https-company/logs I have iPlanet log rotation enabled rotating files on a daily basis. The rotated logs are NOT compressed & are taking up too much space. I... (7 Replies)
Discussion started by: zachs
7 Replies

5. Shell Programming and Scripting

Delete log files content older than 30 days and append the lastest date log file date

To delete log files content older than 30 days and append the lastest date log file date in the respective logs I want to write a shell script that deletes all log files content older than 30 days and append the lastest log file date in the respective logs This is my script cd... (2 Replies)
Discussion started by: sreekumarhari
2 Replies

6. Shell Programming and Scripting

Command to Count the files which is Older than 3 months?

Hi Gurus the count of files in a particular Directory... ls -lrth | grep -c ^- can any one share the command to Count the files which is Older than 3 months So please help me out in this Thanks in Advance (2 Replies)
Discussion started by: SeenuGuddu
2 Replies

7. Shell Programming and Scripting

need a code for moving the log files older than 2 months

Hi Viewer, I need logic code for moving the logs files from one directory to another directory. source :/xxxxxx/ xxxxxx / xxxxxx / xxxxxx / log --- under log directory we have so many files from last two years Here I need to check the files older than two months and I need to move in... (5 Replies)
Discussion started by: munna_su
5 Replies

8. Shell Programming and Scripting

gzip files older than 1 months

Hello, I want to gzip files in a folder in bulk i.e. all files older than 1 month should be in *.gz format. Could you please suggest how this can be achieved. Thanks. Regards, Alok (1 Reply)
Discussion started by: alok1301
1 Replies

9. Shell Programming and Scripting

Delete 6 months old files

Hi, Iam trying to delete 6 months old files. Iam using the following script -ctime +190 -type f -exec echo rm {} \; Iam getting an error saying -ctime not found. (6 Replies)
Discussion started by: Sompalle Jalaja
6 Replies

10. Shell Programming and Scripting

Listing files older than 2 months

A script or command to list files older than 2 months in a specified directory and remove it. (3 Replies)
Discussion started by: pbekal
3 Replies
Login or Register to Ask a Question