ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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...
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
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")!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.