LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash: date based file maintenance (https://www.linuxquestions.org/questions/programming-9/bash-date-based-file-maintenance-465174/)

paul.anderson 07-18-2006 07:18 AM

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

marozsas 07-18-2006 07:22 AM

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

bigearsbilly 07-18-2006 07:29 AM

find . -mtime +60
find . -older marker

touch -t YYYYMMDDMMSS

jlinkels 07-18-2006 08:24 AM

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

paul.anderson 07-19-2006 06:23 AM

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


All times are GMT -5. The time now is 02:05 PM.