Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

is there a shell script out there that deletes all files in a folder that are older than x minutes?

share|improve this question

3 Answers 3

up vote 1 down vote accepted

This should do it, I've tested this with *.txt, but you can change it to all files using *.* instead:

#!/bin/bash
cd /usr/local/my_logs
find ./*.txt -type f -mmin +5 -exec rm {} \;
share|improve this answer
    
I don't think you can specify the directory and files at the same time in that way. –  Dennis Williamson Apr 8 '10 at 17:58
    
I just change to the target directory and then run that script and it seems to work? –  John Rabotnik Apr 8 '10 at 18:02
    
You're right. I keep forgetting that find is a lot less picky than it used to be. –  Dennis Williamson Apr 8 '10 at 18:21
    
so for my use it'll be: find usr/local/my_logs/*.txt -type -f -mmin +5 -exec rm {} \; <-- where 5 indicates 5 minutes? –  dassouki Apr 8 '10 at 18:38
    
You'll need to change to your target directory first using the cd command. I've updated the script in my original post to show this. I've tested it on my machine and it seems to work. Let me know how it goes. –  John Rabotnik Apr 8 '10 at 19:25

This will run forever, delete files modified more than three minutes prior to each iteration and wait one minute before doing it again:

while true
do
    find -type f -mmin +3 -delete
    sleep 60
done

You can add -maxdepth 1 if you don't want it to be recursive.

share|improve this answer

I use a script like this to delete backups older than 30 days:

find "/backups/mysql/" -type f -mtime +30 -print0 | xargs -0 rm -f

Based on that, I think you could do something similar:

find "/yourDir/" -type f -mmin +10 -print0 | xargs -0 rm -f

I think that would get 'em over 10 minutes

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.