LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Remove files older then 30 mins of certain size with certain xtension? (https://www.linuxquestions.org/questions/programming-9/remove-files-older-then-30-mins-of-certain-size-with-certain-xtension-712094/)

stefaandk 03-16-2009 07:03 PM

Remove files older then 30 mins of certain size with certain xtension?
 
How could I run a job that removes files from /tmp dir that have the following conditions:

older then 30 mins
gz or zip extension
larger then 1mb

This is on FreeBSD.

THanks

jschiwal 03-16-2009 07:14 PM

Use the find command in your job.
/usr/bin/find /tmp -type f -cmin +30 \( -iname "*.gz" -o iname "*.zip" \) -execdir rm '{}' \;

I'm not at my home computer & can't test it. I'm not absolutely certain about the -o part. I think that the filename tests need to be grouped so that the prior tests still hold. Otherwise, -iname "*.zip" will be enough for a match even if 100K or just 5 minutes old.

You may want to be careful with your script. Are you certain you aren't including some files that you don't want to delete. Maybe you want to include an owner test such as "-not -user root" to only delete temporary files by regular users.

Another potential problem is if a file is a zip or gz or bzip2 file but doesn't have an extension. A web browser may do that. In Linux, I might use the find command to locate large files older than 30 minutes, and pipe the output to "xargs -0 file". Does FreeBSD have the file command?

Telemachos 03-16-2009 07:14 PM

Either a Bash script using find or a Perl script using File::Find should do the job nicely. (If you're more comfortable with a different scripting language (Python, Ruby, whatever), that should work just fine too.)

paulsm4 03-16-2009 07:36 PM

Yup - "find" is your guy!

(Try doing *that* under Windows ;-))

Biddle 03-17-2009 06:53 AM

Quote:

Originally Posted by paulsm4 (Post 3477674)
(Try doing *that* under Windows ;-))

Windows bashing becomes very tiresome and makes you no better than a windows zealot.
You do realise cygwin works under windows? and that windows has commands?

charlitos 03-17-2009 03:33 PM

the command -stat- might be helpful, check its man page.


All times are GMT -5. The time now is 11:08 PM.