Delete Files Older Than X Days in Linux

Delete Files Older Than X Days in Linux

One of the ways to free up disk space on the system is to delete old files which are no longer needed after a certain period of time. It can be backup files, log files, and other temporary files. This tutorial shows how to delete files older than X days in Linux.

The find command can be used to find files older than X days and delete them. First of all, check which files will be deleted. For example, to find files older than 7 days run the following command:

sudo find /var/tmp -type f -mtime +7

Once confirmed, add the -delete option to delete files:

sudo find /var/tmp -type f -mtime +7 -delete

Files can be filtered by specific extension. For example, to delete .gz files older than 7 days run the following command:

sudo find /var/log -name "*.gz" -type f -mtime +7 -delete

Leave a Comment

Cancel reply

Your email address will not be published.