x32x01

ADMINISTRATOR
It is a best practice to find and remove old or unused files from the Linux system after a certain period of time, as this will free up some space on the system, which can be used for some other purpose.

Please make sure these files are no longer needed, as it will not ask for your confirmation before deleting the files.

delete-files-older-than-x-days-linux-1a.png

Are you wondering how to delete older files in your Linux system? This quick guide show you how to find and remove files older than 30 days in Linux.

To delete files older than 30 days on your Linux server, you need to use multiple commands together such as find command, ‘mtime’ command, ‘exec’ command and rm command.

What’s mtime command
mtime (File Last Modify Time) – mtime shows when you modify, append or update a file content. The time argument consider an input value as ’24 hours’. For example, time 2 = 2*24 hours (2 days).

1) Search and Delete files older than 30 days

First, we will find out all files older than 30 days under the ‘/home/linuxgeek/Downloads’ directory.
The below output will allow you to check whether these files are needed before they are deleted. If not, delete them using the below command.
Bash:
find /home/linuxgeek/Downloads -type f -mtime +30 -print

/home/linuxgeek/Downloads/Karnan-320kbps-MassTamilan.fm.zip
/home/linuxgeek/Downloads/Teddy-320kbps-MassTamilan.io.zip
/home/linuxgeek/Downloads/Baahubali-320kbps-MassTamilan.com.zip
/home/linuxgeek/Downloads/Baahubali-2-320kbps-MassTamilan.com.zip
/home/linuxgeek/Downloads/Asuran-320kbps-MassTamilan.org.zip
/home/linuxgeek/Downloads/Irudhi-Suttru-320kbps-MassTamilan.com.zip
/home/linuxgeek/Downloads/tech_ethernet_icon_156953.png
/home/linuxgeek/Downloads/Apps-preferences-system-network-icon_31759.png
/home/linuxgeek/Downloads/preferencessystemnetwork_103783.png

Once you have decided that these files are no more requires then go ahead and delete it, using one of the following command.
Bash:
find /home/linuxgeek/Downloads -type f -mtime +30 -exec rm -f {} \;
or
find /home/linuxgeek/Downloads -type f -mtime +30 | xargs rm -f

Details:
  • find: find is a command
  • /home/linuxgeek/Downloads: Path to files (It should be replaced by yours)
  • -type f: What Type of files
  • -mtime +30: It filters 30 days old files.
  • -exec rm -f Perform a file remove action
  • {}: Represents the file found by Find command.
  • \; It will end the command.

2) Delete files older than ‘X’ days with Wildcard option

In many cases, you may have to delete files based on the name when there are many log files with different names in the log directory, I usually use this command to delete files older than 30 days on the system.

It is always recommended to do a dry run instead of removing directly. This will give you the time to check before deleting the files.
Bash:
find /home/linuxgeek/Downloads -type f -name "Trans_suc*" -mtime +30 -print

Once you verified the list, select one of the following commands to remove it.
Bash:
find /home/linuxgeek/Downloads -type f -name "Trans_suc*" -mtime +30 -exec rm -f {} \;
or
find /home/linuxgeek/Downloads -type f -name "Trans_suc*" -mtime +30 | xargs rm -f

3) Delete files older than 10 days with Specific Extension

Sometimes you may need to delete files based on file extension instead of deleting them. For instance, to delete files with “.log” extension and modified within 10 days, run:

First, let’s find and print a list of log files older than 10 days under the ‘/var/log’ directory:
Bash:
find /var/log -type f -name "*.log" -mtime +10 -print

/var/log/error.log
/var/log/access.log

Check the list and delete it using one of the following command if it is no longer needed.
Bash:
find /var/log -type f -name "*.jpeg" -mtime +10 -exec rm -f {} \;
or
$ find /var/log -type f -name "*.jpeg" -mtime +10 | xargs rm -f

Final Thoughts
In this guide, we’ve shown you how to delete files older than 30 days on Linux with three different examples. It is not limited to 30 days and days can be changed as needed like 10 days or 90 days or even 120 days.

If you have any questions or feedback, feel free to comment below.
 
Top