BASH - Delete files older than 3 months?

19,148

If you want exact number of days for 3 months then you can use:

days=$(( ( $(date '+%s') - $(date -d '3 months ago' '+%s') ) / 86400 ))

and use it as:

find /tmp/*.log -mtime +$days -type f -delete

Or directly in find:

find /tmp/*.log -type f \
-mtime "+$(( ( $(date '+%s') - $(date -d '3 months ago' '+%s') ) / 86400 ))" -delete
Share:
19,148

Related videos on Youtube

Admin
Author by

Admin

Updated on September 15, 2022

Comments

  • Admin
    Admin over 1 year

    Delete files older than 3 months how?

    For 90 days i know:

    find /tmp/*.log -mtime +90 -type f -delete
    

    But how do i know 3 months equal to always 90 days? how many exact days? Is there more better way to tell the -mtime to follow months?

    • Leon
      Leon over 6 years
      Why is it so critical for you to use the inexact time-unit month?
    • paxdiablo
      paxdiablo over 6 years
      Make it 93 days and just accept that you may have a delayed deletion of files for short periods. Life is too short for being picky :-)