Steve88 Steve88 - 1 year ago 106
Bash Question

Delete files older than 10 days using shell script in Unix

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:


The script will run in every 10 day with Crontab, that's why I need the current date.

Answer Source

find is the common tool for this kind of task :

find ./my_dir -mtime +10 -type f -delete


  • ./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 your find filter before executing the whole command

And take care that ./my_dir exists to avoid bad surprises !

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download