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 {} \;
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!
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.