LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 07-18-2006, 08:18 AM   #1
paul.anderson
LQ Newbie
 
Registered: Jul 2006
Posts: 3

Rep: Reputation: 0
Bash: date based file maintenance


I am trying to maintain 60 dyas of archive files in a directory. Here was my first attempt.

#!/bin/bash
#
cd /home/stuff/archive
touch -d "60 days ago" marker.file
ls >file.tmp
while read: do
if [ $REPLY -ot marker.file ]; then
rm -f $REPLY
fi
done <file.tmp

Pretty simple.... if it worked; which it doesn't.
Unfortunately, the "if" statement compares the modify date, not the create date that touch uses.
I have tried using the "-m" with touch to no avail. Nothing I have done so far has been able to back date the modify date of the marker.file; required for this script to work.

Is ther another simle way to do this function?


Paul....
 
Old 07-18-2006, 08:22 AM   #2
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,393
Blog Entries: 1

Rep: Reputation: 64
Sure. The find command has all you need in a single command !
It can compare dates of files, based on a reference file like yours markers.file, or just 60 days ago, and execute any command based on this test, rm, mv, cp, etc...
 
Old 07-18-2006, 08:29 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
find . -mtime +60
find . -older marker

touch -t YYYYMMDDMMSS
 
Old 07-18-2006, 09:24 AM   #4
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,244

Rep: Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546
This what I use to clean up log files older than 40 days:

Code:
#clean up log files older than 40 days
if : ; then
  echo Log files older than 40 days which have been purged:
  find $logdir -mtime +40 \( -name \*.dst -or -name \*.log \) -print -exec rm {} \;
fi >>$logfilename
If logs the purged files to $logfilename. If you don't want the log, use only the "find" statement and omit the "if .. fi".

BE CAREFUL:

This is a script containing "rm". One typo and you can wipe out your hard disk. Test it with a command other than "rm" (for example "ls")!

jlinkels

Last edited by jlinkels; 07-18-2006 at 09:25 AM.
 
Old 07-19-2006, 07:23 AM   #5
paul.anderson
LQ Newbie
 
Registered: Jul 2006
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks

Thanks _ The "find" command is just too simple for words. One line programming.... you just have to love Linux!
 
  


Reply

Tags
bash


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Get file modification date/time in Bash script cmfarley19 Programming 13 12-21-2014 12:05 PM
bash sort files by date in file name thedude2010 Programming 6 05-12-2006 12:07 PM
howto create a file based on date in bash rohan208 Linux - Newbie 2 05-07-2004 04:54 PM
Date-based copy AdmiralThrawn Linux - General 2 02-23-2004 10:03 AM
Set name of tar file as date using bash otisthegbs Linux - Software 5 12-18-2003 04:34 PM


All times are GMT -5. The time now is 10:23 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration