Regularly cleaning out old unused files from your server is the best practice. For example, if we are running a daily/hourly backup of files or databases on the server then there will be much junk created on the server. So clean it regularly. To do it you can find older files from the backup directory and clean them.

Advertisement

This article describes you to how to find and delete files older than 30 days. Here 30 days older means the last modification date is before 30 days.

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

You have learned how to find and delete files in the Linux command line that have been modified more than a specified number of days ago. That will help you clean up your system from unwanted files.

Share.

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?

    • #!/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

      • 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. Junior_mahatma on

    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

  6. marija christofer on

    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


Exit mobile version