Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Linux Commands»How to Delete Files Older than 30 days in Linux

    How to Delete Files Older than 30 days in Linux

    By RahulAugust 6, 20223 Mins Read

    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.

    delete files find old files
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    free Command in Linux (Check Memory Uses)

    A Practical Guide to Extracting Compressed Files in Linux

    TR Command in Linux: A Guide with Practical Examples

    TR Command in Linux: A Guide with Practical Examples

    View 15 Comments

    15 Comments

    1. Anoop on September 27, 2021 12:39 am

      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

      Reply
    2. Antonio on October 21, 2020 3:20 am

      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?

      Reply
      • Walter on January 4, 2021 1:24 pm

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

        Reply
        • mamadou on January 20, 2021 11:47 am

          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?

          Reply
    3. Antonio Reyes on October 21, 2020 3:18 am

      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?

      Reply
    4. Dave on October 15, 2020 10:08 am

      could just add the -delete flag rather than piping to rm

      Reply
    5. Junior_mahatma on July 24, 2020 8:46 am

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

      Reply
    6. JR on October 31, 2019 1:22 am

      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

      Reply
    7. Lakshmi on October 18, 2019 7:30 am

      How to find and delete files older than 90 days in linux?

      Reply
    8. Bachsau on September 27, 2019 12:19 pm

      Find has a ‘-delete’ option. You don’t need ‘-exec’ for that.

      Reply
    9. Lokesh on September 19, 2019 6:26 am

      How to pass multiple files in name variable

      example: -name “*.log”, “errorfile” , “result*”….

      Reply
    10. adhir on August 16, 2019 11:55 am

      find: missing argument to `-exec’

      Reply
      • Rahul on August 17, 2019 5:22 am

        Hi Adhir, Tutorial has been updated.

        Reply
        • Tobias Heel on April 6, 2021 2:35 pm

          I get the same error.

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

          Reply
    11. marija christofer on August 22, 2016 10:05 am

      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.

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • A Comprehensive Look at the Simple Mail Transfer Protocol (SMTP)
    • Understanding Basic Git Workflow: Add, Commit, Push
    • The Difference Between Git Reset –soft, –mixed, and –hard
    • Understanding the Staging Area in Git’s Workflow
    • Python Function with Parameters, Return and Data Types
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.