This question already has an answer here:
I'm new to shell scripts, can anyone help? I want to delete scripts in a folder from the current date back to 10 days. The scripts looks like:
2012.11.21.09_33_52.script
2012.11.21.09_33_56.script
2012.11.21.09_33_59.script
The script will run in every 10 day with Crontab, that's why I need the current date.
If you can afford working via the file data, you can do
find
is the common tool for this kind of task :EXPLANATIONS
./my_dir
your directory (replace with your own)-mtime +10
older than 10 days-type f
only files-delete
no surprise. Remove it to test yourfind
filter before executing the whole commandAnd take care that
./my_dir
exists to avoid bad surprises !Just spicing up the shell script to delete older files
The code build on sputnick's answer and adds a few more things.