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

Marc Wagner, July 14, 2022

In this article, I explain how you can delete files in Linux/Ubuntu that are older than 30 days. This is especially useful if you create automated backups and want to delete them after 30 days.

The 30 days is the period when the file was last modified.

Delete files older than 30 days #

You can use the find command to find all files older than X days. You can also delete the found files directly with the command.

First, you should list all files that are older than 30 days. To do this, we look in the /opt/backup directory for all files that were last modified +30 days ago.

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

Note: You can of course adapt the directory to your needs. Important: In case of an incorrect call, the deletion of files can lead to a failure of the system.

Now check the list that is displayed. Once you are satisfied with it, you can delete the found files with the following command.

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