LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-18-2006, 07: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, 07:22 AM   #2
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
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, 07:29 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

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

touch -t YYYYMMDDMMSS
 
Old 07-18-2006, 08:24 AM   #4
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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 08:25 AM.
 
Old 07-19-2006, 06: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



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 16 04-15-2015 06:25 PM
bash sort files by date in file name thedude2010 Programming 6 05-12-2006 11:07 AM
howto create a file based on date in bash rohan208 Linux - Newbie 2 05-07-2004 03:54 PM
Date-based copy AdmiralThrawn Linux - General 2 02-23-2004 09:03 AM
Set name of tar file as date using bash otisthegbs Linux - Software 5 12-18-2003 03:34 PM

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

All times are GMT -5. The time now is 06:21 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
Open Source Consulting | Domain Registration