A Comprehensive Guide on How To Delete Files Older Than X on Linux

Discover the commands for deleting files older than 1 hour, 7 days, or even 30 days on Linux.

It is common practice for users to exchange log dumps for additional analysis, as you are well aware. Though it's no longer necessary, some individuals save the record for future reference. You're trying to delete files older than 7 days or 30 days on Linux or update them, and there are a lot of files to choose from when you look at the file. This post will be helpful if you, too, need this one.

Command to Delete Files Older Than X on Linux

It's recommended that you purge your server of unneeded files regularly. There will be a lot of unnecessary data on the server if, for instance, we back up files and databases every day or every hour. As such, keep up a consistent cleaning schedule. You can locate and erase older files in the backup directory.

The find command is helpful in such a situation since it will initially look for the file in one of the following ways: to end in -amin, -atime, -cmin, -ctime, -mmin, and -mtime.

When the search command returns a match for the specified criteria, you may delete it using the -delete option or xargs.

Not that complicated, huh? First, let's try out some of the situation-specific instructions.

Examples to Delete Files Older Than X on Linux

Here, we'll look at how to delete files in Linux that are more than x days old. Most often, this is used to get rid of backups that are too old or to get rid of rotational logs that are older than a specific number of days.

This material is designed for novice and advanced Linux users since it covers a topic essential to the operating system: locating and removing files based on their mtime (Modification/Creation time). You may use any Linux distribution using the provided examples.

1. Delete Files Older Than X Hours / Days on Linux

With find, you may look for files that have yet to be touched in more than X days and then delete them all. Additionally, you may remove them with a single command if that becomes necessary.

  • To begin, create a list of everything in the /opt/backup directory that is more than 7 days old.

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

  • Check the file list by checking the above command doesn't include any help files. After doing so, you may use the following control to remove the files permanently.

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

2. Delete Files That Has Been Modified Within X Minutes

Sometimes, you may want to purge data based on when it was last edited; for example, you may delete anything in the current directory that was accessed during the previous X minutes.

  • Take the case where I wish to remove all files that have been updated during the previous half-hour. In such a case, you may erase all recently changed files using the below command.

find . -mmin -30 -type f -delete

3. Delete Files Older Than X Days With a Prompt Confirmation

You should only execute rm or use the -delete option once you verify that the result returned by a search is correct.

Since the search command looks for the supplied pattern recursively along the specified path, it is easy to accidentally delete every file in the current and sub-dirs by mistyping the filename or providing incorrect options.

  • Hopefully, my meaning is clear. It is recommended to use -print to verify the output before executing the following command, which will prompt you for confirmation before erasing the file.

find . -atime +30 -exec rm -i {} \; -print

  • Files having modification time that is more than 30 days old will be removed from your system when you run the command mentioned above.

4. Delete Files Older Than X Days With Specific Extension

Rather than erasing everything, you may use many filters to help you find the instructions you need. For instance, you can't get rid of anything edited more than 30 days ago if it has the ".log" extension.

  • First, to be safe, do a dry run and compile a list of files that meet the requirements.

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

  • Once the list has been double-checked, the files on it may be removed using the command:

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

  • Files with the.log extension and a modification time of more than 30 days old will be removed from your system when you run the command mentioned above.

5. Delete Old Directory Recursively

If the directory is not empty, the -delete option may fail. In such a situation, we'll use the find command in conjunction with Linux's rm command to delete the unwanted data.

  • Using the command below, I am searching all subdirectories of /var/log that have been updated in the last 90 days.

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

  • The rm command's -exec command-line argument allows us to delete files. The results from the find command will be sent into the rm command.

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

There comes a time that you delete the files mistakenly. We've all been in situations when we lost data and wished to regain it. It's a relief that Linux makes it simpler to recover lost data than other operating systems. Here is an overview of recovering deleted files on Linux. You can download Recoverit Linux data recovery here for a quick solution.

Wondershare Recoverit - Your Safe and Reliable Linux Recovery Software

5,481,435 people have downloaded it.

Recovers lost or deleted documents, photos, videos, music, emails, and other 1000+ file types effectively, safely, and completely.

Compatible with all mainstream Linux distros, including Ubuntu, Linux Mint, Debian, Fedora, Solus, Opensuse, Manjaro, etc.

Assists in 500+ data loss scenarios, such as deletion, disk formatting, OS crash, power outage, virus attack, lost partition, and many more.

The simple point-and-click interface allows you to recover data from Linux hard drives in just a few clicks.

Works through a remote connection. You can recover lost data even when your Linux device is crashed.

Security Tips When Cleaning Up Files on Linux

It is well-known that Google uses Linux to operate its servers, which powers its popular search engine, applications, and Chromebook computers.

Linux is the operating system for many engineers and programmers working on cutting-edge computers. While Linux computers' key selling points are their open-source nature, flexibility, and minimal maintenance requirements, they nevertheless need regular tweaking to function at their full potential. Some tips and tricks to keep your Linux system trim and running well are provided below:

Tip #1: Verify the Free and Used Space On Your Computer's Disks

In most cases, a Linux computer will reach its total capacity at 95%. Above that, you will notice a slowdown in performance. Reviewing your disk utilization regularly can allow you to discover which files are hogging the most storage space and will enable you to remove them. Select Scan Filesystem from the Disk Usage analyzer menu under Applications > Accessories.

Tip #2: Get Rid of Unused Software and Erase Temporary Files

After determining how healthy your hard drive is, the following step is to uninstall any unused software and delete any unnecessary temporary files. The more room you create by doing this, your computer will be quicker. However, when clearing up temporary files, you should exercise caution when using the "-exec" option since deleting data in this way is irreversible.

Tip #3: Always Make Sure You Have a Backup Of Your Important Data

If you delete files through the -exec command by mistake, you will only be able to get them back if you have a backup. Reason enough to make sure you regularly back up your data. You can do this by hand, using local automation, or remotely.

If your Linux computer is only for your use, then manual backups on your schedule are fine. On the other hand, local automatic backups may be the best option if safety and dependability are your top priorities. Since it is automated, you can rest confident that your data will always be backed up and that you will always have a method to restore them if anything goes wrong.

Tip #4: Update Your Software to the Latest Version

Keeping the kernel patched to the newest version is also essential for a healthy Linux system. Taking these measures increases the system's security and stability, making it less vulnerable to attacks from hackers and faults. The newest open-source drivers have been updated, the system is faster, and there are new kernel functions that boost performance.

Tip #5: Examine Records and Data

Lastly, you should check your system's weekly and daily monitoring statistics and logs. You should do this to see if there are any faults or defects that you have overlooked in the past but now need to fix.

These tasks, which may seem daunting or tiresome at first, will become second nature if you make them a regular part of your routine. Additionally, automation systems make the procedure much more accessible. Linux is a secure, low-maintenance platform that requires little to no help from outside sources. It is cost-free, resistant to viruses and spyware, and seldom experiences downtime or crashes. However, the preceding guidelines may help ensure that even the most cutting-edge equipment operates at peak efficiency.

Summing Up

As a result of this guide, you now know how to use the Linux command line to locate and remove files that have not been updated during a specific time frame. That will aid in clearing up your computer's junk folder.

You May Also Like

How To Remove Files or Directories Recursively in Linux

Here are the steps to recursively remove files or directories/folders in Linux

How To Use Foremost to Recover Files on Linux and Its Alternative

Here’s how to install and use Foremost to recover data in Linux and its best alternative.

How To Install and Use TestDisk on Linux and Its Alternative

Detailed here are the ways to install and use the TestDisk Linux data recovery tool, plus its alternative.

Top 10 Linux Partition Recovery Tools

Restore your deleted or lost data with the 10 best Linux partition recovery software.

How to Wipe Your Disk in Linux?

Linux wipe disk is necessary when you’re facing storage issues. Users can check the disk space status in Linux and wipe Linux hard drive.