Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 07-18-2006, 08:18 AM   #1
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.

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
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?

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

Rep: Reputation: 65
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
Senior Member
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,421

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

Old 07-18-2006, 09:24 AM   #4
Senior Member
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 4,728

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

#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".


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")!


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

Original Poster
Rep: Reputation: 0

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



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 16 04-15-2015 07:25 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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:26 PM.

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