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

Marc Wagner, July 14, 2022

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
Avatar of Marc Wagner
Marc Wagner

Hi Marc here. I'm the founder of Forge12 Interactive and have been passionate about building websites, online stores, applications and SaaS solutions for businesses for over 20 years. Before founding the company, I already worked in publicly listed companies and acquired all kinds of knowledge. Now I want to pass this knowledge on to my customers.

Similar Topics

Comments

Leave A Comment