Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a folder that connects to a hardware weather logger. The data is pretty huge, and I only wish to keep the last 5 hours worth of data. This folder; however, has also some config files such as myconfig.ini.

How can I set up an automatic service that automatically deletes all files in a folder save for a few?

share|improve this question
    
The real answer, of course is: don't put your config and data in the same directory. If nothing else, you could put the data in a subdirectory called "data". That would make it easier to deal with. –  Ryan Thompson Nov 1 '10 at 19:06

5 Answers 5

up vote 4 down vote accepted

Would adding a find command to cron work for you?

find /path/ -type f -mtime +5 -name '*.log'

You can layer on extra things to find just the files you want. -mtype is the date-limiting portion which finds files last modified over 5 days ago. You can change this to just look at creation or even access times (if you're not using noatime on your fs)

When you're getting the right files, just stick -exec rm {} \; on the end. You could use -delete but that will crumble if you have too many files being deleted at once. Only do this once you're sure!

When you want to schedule it, su into the right user account (the person who owns the files) and then run crontab -e. You can then write out the command and when you exit, it'll run at the schedule you set. You can read more about cron here:

https://help.ubuntu.com/community/CronHowto

share|improve this answer
    
The command should end with -name '*.log'. Without the quotes, if there is any file matching *.log in whatever directory is current when you run the command, the shell will substitute them for the pattern, which will lead to either errors or the wrong set of files being acted upon. –  Gilles Nov 1 '10 at 19:18

You can use logrotate. It's installed by default; check the manual page for instructions.

share|improve this answer

This should remove anything modified within the last day. I'd suggest placing it in a daily cronjob. Be sure to TEST before implementing... and bear in mind that since it works on files by modified date it isn't file specific (i.e. if you modify your myconfig.ini, the next time it runs it might kill your myconfig.ini.

find /directory_path -type f -mtime -1 -exec rm{} \;

This obviously has some disadvantages, but it might at least be a good start.

share|improve this answer

Red Hat/Fedora/Cent has a slick little script called tmpwatch that you can easily pull-over to Ubuntu.

share|improve this answer

Taking a slightly different tack: why not set up a 5hr frequency job to rename the current collection directory and create a new current collection directory? You could manually or automatically process/archive/delete the individual 5hr directories.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.