Skip to content

How to Delete Files Older than 30 days in Linux

Advertisement

Deleting old files in Linux helps keep your system tidy and saves space. If you have files you don’t need anymore that are older than a certain number of days, this guide will show you how to remove them step by step. It’s simple, whether you’re new to Linux or just want a quick reminder. We’ll begin with basic commands and then use them in your terminal.

This article explains how to find and delete files older than a set number of days. “Older” here means the file’s last change was before that many days ago.

1. Delete Files older Than 30 Days

Using the find command, you can search for and delete all files that have been modified more than X days. Also, if required you can delete them with a single command.

First of all, list all files older than 30 days under /opt/backup directory.


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

Verify the file list and make sure no useful file is listed in the above command. Once confirmed, you are good to go to delete those files with the following command.


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

2. Delete Files with Specific Extension

You can also specify more filters to locate commands rather than deleting all files. For example, you can only delete files with the “.log” extension and modified before 30 days.

For the safe side, first, do a dry run and list files matching the criteria.


find /var/log -name "*.log" -type f -mtime +30

Once the list is verified, delete those files by running the following command:


find /var/log -name "*.log" -type f -mtime +30 -delete

The above command will delete only files with a .log extension and with the last modification date older than 30 days.

3. Delete Old Directory Recursively

The -delete option may fail if the directory is not empty. In that case, we will use the Linux rm command with find to accomplish the deletion.

Searching all the directories under /var/log modified before 90 days using the command below.


find /var/log -type d -mtime +90

Here we can execute the rm command using -exec command line option. Find command output will be sent to rm command as input.


find /var/log -type d -mtime +30 -exec rm -rf {} \;

WARNING: Before removing the directory, Make sure no user directory is being deleted. Sometimes parent directory modification dates can be older than child directories. In that case, recursive delete can remove the child directory as well.

Conclusion

This article explains how to find and delete files older than a set number of days. “Older” here means the file’s last change was before that many days ago. By following these steps, you’ll free up space and keep your system organized.

Now you know how to find and remove old files in Linux using the command line. This keeps your system free of clutter and running smoothly. With these skills, you can manage your files better, avoid wasting disk space, and keep everything neat. It’s a quick and easy way to maintain your Linux setup!

Rahul

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

15 Comments

  1. find /home/*/* -name “*.tar.gz” -type f -mtime +30 -exec rm -rf {} \;

    is there any way to exclude one folder and its files on above command

  2. How do I write a script and scheduled to run monthly to delete backup files (x.tar.gz) 2-months old on located on /snap folder in a Red Hat Linux server?

    1. #!/bin/bash

      find /mnt/backup/backup -name “*.x.tar.gz” -type f -mtime “+$(( ( $(date ‘+%s’) – $(date -d ‘2 months ago’ ‘+%s’) ) / 86400 ))” -exec rm -f {} \;

      #Use crontab to schedule as required

      1. find /mnt/backup/backup -name “*.x.tar.gz” -type f -mtime “+$(( ( $(date ‘+%s’) – $(date -d ‘2 months ago’ ‘+%s’) ) / 86400 ))”
        Returns errors:
        date: invalid date ‘‘+%s\’’
        date: extra operand ‘ago\’’

        Any idea?

  3. How do I write a script and scheduled to run monthly to delete backup files (x.tar.gz) on /snap folder in a Red Hat Linux server?

  4. This only removes the files. What if you have a sub folders and you also want to delete these. What do you do then ?

  5. How to delete files/folders from a directory older than a certain date?
    Ex:- Delete all files and folders from a specified path older than 01-Jan-2018

      1. I get the same error.

        Command:
        find /volume1/DeleteOldBackUps_TEST/Dest_Old/test-02/ -type f -mtime +31 -exec rm {} \;

  6. The situation could be frustrating and can go a long way to slowing down your system’s

    speed. What do you do at this time? There is need to delete those unnecessary files.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top