Jump to content

How to Delete files older than 30 Days in Linux


aum

Recommended Posts

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.

 

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.

 

$ 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.

 

$ 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 30 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.

 

$ 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.

 

$ 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 30 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 30 days, run:

 

First, let’s find and print a list of log files older than 30 days under the ‘/var/log’ directory:

 

$ find /var/log -type f -name "*.log" -mtime +30 -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.

 

$ find /var/log -type f -name "*.jpeg" -mtime +1 -exec rm -f {} \;
or
$ find /var/log -type f -name "*.jpeg" -mtime +1 | 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.

 

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

 

Source

Link to comment
Share on other sites


  • Views 1.9k
  • Created
  • Last Reply

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...