is there a shell script out there that deletes all files in a folder that are older than x minutes?
3 Answers
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 {} \;
-
I don't think you can specify the directory and files at the same time in that way. Apr 8, 2010 at 17:58
-
I just change to the target directory and then run that script and it seems to work? Apr 8, 2010 at 18:02
-
You're right. I keep forgetting that
find
is a lot less picky than it used to be. Apr 8, 2010 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?– dassoukiApr 8, 2010 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. Apr 8, 2010 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.
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