LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-23-2011, 02:34 PM   #1
coop59
LQ Newbie
 
Registered: Mar 2011
Posts: 6

Rep: Reputation: 0
Bash Scripting with file date comparison


Hey Guys,

I am new to bash scripting and I need some help. I need to be able to compare a file date with system date and delete files older than 30 days.

the file name is basically
error_log.03222011

of course the extension is the date the file was created.

Any ideas?

Oh and before i get hammered I looked everywhere but am unable to make sense of what I found.

Thanks again,
Michael
 
Old 03-23-2011, 02:51 PM   #2
SL00b
Member
 
Registered: Feb 2011
Location: LA, US
Distribution: SLES
Posts: 375

Rep: Reputation: 112Reputation: 112
Here's a very simple script I made for this purpose that is very easily portable, since all you have to do is change the BACKUPDIR and DAYSOLD variables to suit your needs.

It's going to indiscriminately delete anything found in the given folder that was created thirty days ago, which makes it simpler than trying to parse on the date stamp. I can do this because part of the process that creates the backups I wrote this script for moves the old ones to a folder reserved just for them. If there are other files in that folder you want to keep, then this wouldn't be the way to go... you could either move them with another command, or modify the find command so it only comes up with the ones you're looking for, by adding the -name switch.

Code:
#!/bin/bash

####################
BACKUPDIR="/dbhome/backup/retain"
DAYSOLD="30"
####################

find $BACKUPDIR -maxdepth 1 -mtime +$DAYSOLD -exec rm {} \;

#eof
 
Old 03-24-2011, 04:11 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
firstly, always use ISO timestamps, YYYYMMDD
as they are sortable and most sensible people use it.

the above find works as long as the timestamp matches the filename.
if a body has opened a file and saved it the timestamp may be wrong


what I do usually is grab the timestamp of the filename and
actually stamp the file to match using touch

then the find is guaranteed and you get no surprises.
 
Old 03-31-2011, 08:02 AM   #4
coop59
LQ Newbie
 
Registered: Mar 2011
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks guys I got what I needed from this.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH: Keep original mod date of a file and 'date' it back to same file? SilversleevesX Programming 4 07-16-2010 11:12 AM
Bash scripting question (CAL & DATE) Coldmiser Other *NIX 8 05-10-2010 03:08 PM
Date comparison with 'string date having slashes and time zone' in Bash only TariqYousaf Programming 2 10-08-2009 07:37 AM
Bash scripting using date pueblonative Programming 3 01-16-2007 09:34 AM
File Modification Date Comparison MadTurki Programming 7 03-08-2004 04:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration