Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 04-25-2006, 12:57 PM   #1
Registered: Oct 2003
Location: Ohio , USA
Distribution: up to Suse 13.1
Posts: 71

Rep: Reputation: 25
Cron job files by date


I am looking for a way to read from and delete the oldest directory on a daily basis.

I am using this server as a security camera recorder. We rename the daily folder to the days' date and copy the back-up folder in with the operational name. We rename the files as month and day "0425".
There is still figuring to do. there is 200 gig of drive space available, approximately 7 gigs are used each day. I want to keep the files for as long as I can to view as needed but I want to delete the oldest folders to make running room for the new.

Any thoughts please??


Old 04-25-2006, 01:20 PM   #2
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975
how many is the "oldest folders" ? how do you wish to decide which stay and which go? essentially you could use an ls output sorted by date, e.g. "ls -1t /var/backup | head 3" would get the oldest three files / directories in there... or you might want everything older than a certain date, which is much nicer: "find -mtime +240 /var/backup/" will list all files older than 10 days...
Old 04-25-2006, 02:22 PM   #3
Senior Member
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,402
Blog Entries: 1

Rep: Reputation: 64
You can use the logrotate tool. It has several options to handle, but I think this could be easier than crafting your own script to do this job, specially if you plan to add safety features, idiot-proof things.

logrotate can compress your files, rotation, removal, run pre and post scripts (stop and restart the camera recorder), e-mail you...

take a look: man logrotate

Last edited by marozsas; 04-25-2006 at 02:26 PM.
Old 04-25-2006, 03:56 PM   #4
Registered: Oct 2003
Location: Ohio , USA
Distribution: up to Suse 13.1
Posts: 71

Original Poster
Rep: Reputation: 25
I would need to delete just one folder everyday - automatically so it will not crash by being out of drive space.

Using the find command with the switch seems just the ticket. It would allow for a fresh start up of the server and if I check for folders over 20 days old, just delete them. That would do the trick!! Then also, if I add more cameras and the drives space per days available changes, I can just change how many days to allow to be kept!

Thanks Acid! Can I ask how you would go about using it to remove those full directories?
rm -f find -mtime +480 /var/backup/ -- I am not that good at assembling layered commands, yet anyway. I am trying to learn

Marozasa - hmmmmmm, the tools and commands I don't know that exsist. I try to apropos words that have to do with what I am trying, but I don't seem to find all these niffty things. Thank you. I have been looking at a note page and will dig into the man page. Cool since it runs from Cron already.

I use a web / PHP page to read the directory and create links ... hmmm kk, things to try.

Thanks much for the help!! if there are any other thoughts ....

BTW, it needs to be an automatic / hands off thing, for primary function at least. I hope to not have to do a personal dailly maintanace on it.

Last edited by Kumado; 04-26-2006 at 07:04 AM.
Old 04-26-2006, 05:02 PM   #5
Registered: Oct 2003
Location: Ohio , USA
Distribution: up to Suse 13.1
Posts: 71

Original Poster
Rep: Reputation: 25
Acid, seems you have helped before. I am so pinched on time most of my day, I am a teacher and do this stuff for the school.

I was needing to write a cron job to rename the previous day's recordings ( folder called record ) to that days date, after midnight, make a new record folder and delete folders older than 20 days.

Can someone check me, help fill in the blanks? It would be most helpful and have my many thanks.

I had thought to have cron get the date >> to some file before midnight,
then have cron do the rename, copy, delete about 2AM next day.

I have only altered but never written scripts. Long way to go but is fun


date +%b_%d >> /path/filedate ; #not really needed as a script here ?


str=$(less filedate ) ;

mv /path/record /path/$str #( date here - by month_day filedate ?? ) ;
find -mtime +240 /path/ -exec rm -f {} \; # can do more than one file with this ??
cp /path/record_bak /path/record ;

Am I anywhere near what should work?

Thanks to all in advance

Last edited by Kumado; 04-26-2006 at 06:02 PM.


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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
cron job to detect new files rblampain Programming 8 12-28-2005 08:51 AM
cron job? dr_zayus69 Linux - Newbie 14 12-01-2004 06:16 PM
cron job to delete files based on attributes alpha21 Linux - General 3 11-09-2004 12:06 PM
cron job sanjith11 Linux - General 5 07-14-2004 01:06 PM
howto delete files via ftp from cron job ? cccc Linux - Networking 2 01-31-2004 06:49 PM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:53 AM.

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