Linux/Ubuntu: Delete files older than 30 days via SSH

Marc Wag­ner

July 14, 2022

1 min read|

In this artic­le, I explain how you can dele­te files in Linux/Ubuntu that are older than 30 days. This is espe­ci­al­ly useful if you crea­te auto­ma­ted back­ups and want to dele­te them after 30 days.

The 30 days is the peri­od when the file was last modi­fied.

Delete files older than 30 days #

You can use the find com­mand to find all files older than X days. You can also dele­te the found files direct­ly with the com­mand.

First, you should list all files that are older than 30 days. To do this, we look in the /opt/backup direc­to­ry for all files that were last modi­fied +30 days ago.

find /opt/backup -type f -mtime +30

Note: You can of cour­se adapt the direc­to­ry to your needs. Important: In case of an incor­rect call, the dele­ti­on of files can lead to a fail­ure of the sys­tem.

Now check the list that is dis­play­ed. Once you are satis­fied with it, you can dele­te the found files with the fol­lo­wing com­mand.

find /opt/backup -type f -mtime +30 -delete
88e86fcb816eff22bc917094df2862d8dd5c0e978b333e6dd5f36f808990c261 96

Arti­kel von:

Marc Wag­ner

Hi Marc here. I’m the foun­der of Forge12 Inter­ac­ti­ve and have been pas­sio­na­te about buil­ding web­sites, online stores, appli­ca­ti­ons and SaaS solu­ti­ons for busi­nesses for over 20 years. Befo­re foun­ding the com­pa­ny, I alre­a­dy work­ed in publicly lis­ted com­pa­nies and acqui­red all kinds of know­ledge. Now I want to pass this know­ledge on to my cus­to­mers.

Hast du eine Fra­ge? Hin­ter­lass bit­te einen Kom­men­tar