Deleting old files in Linux can help keep your system clean and free up space. If you have files older than 30 days that you no longer need, this guide will show you how to remove them step-by-step. Whether you’re a beginner or just need a refresher, you’ll find easy instructions here. We’ll start by understanding the basic commands and then move on to applying them in your terminal.
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 {} \;
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.
15 Comments
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
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?
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?
could just add the -delete flag rather than piping to rm
This only removes the files. What if you have a sub folders and you also want to delete these. What do you do then ?
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
How to find and delete files older than 90 days in linux?
Find has a ‘-delete’ option. You don’t need ‘-exec’ for that.
How to pass multiple files in name variable
example: -name “*.log”, “errorfile” , “result*”….
find: missing argument to `-exec’
Hi Adhir, Tutorial has been updated.
I get the same error.
Command:
find /volume1/DeleteOldBackUps_TEST/Dest_Old/test-02/ -type f -mtime +31 -exec rm {} \;
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.