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.... |
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... |
find . -mtime +60
find . -older marker touch -t YYYYMMDDMMSS |
This what I use to clean up log files older than 40 days:
Code:
#clean up log files older than 40 days 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 |
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. |