How to Search and Delete Files older than “X” Days and “X” Hours in Linux?

Implementing the best practice in the IT infrastructure is one of the important activity for Linux admin.

It will streamline the process as well as it will improve the productivity, also it help us to save good amount of time and money.

There are lot of best practices are there as per the ITIL process and we can implement suitable things for our environment.

Removing old or unused files from server is one of the best practice that will free up some space on server.

Make sure those files are no longer necessary before perform this action. It allow us to use that space for some other purpose.

For instance, you can remove 30 days old backup files if you are taking daily backup.

Also, you can remove old log files if it’s not necessary.

This can be done with help of the find command. The find command allow us to input multiple arguments together to perform this.

It can be achieved by using the following options with find command.

  • atime – The last time the file (or directory) data was accessed
  • mtime – The last time the file (or directory) data was modified
  • mmin – File’s data was last modified less than, more than or exactly n minutes ago.
  • amin – File was last accessed less than, more than or exactly n minutes ago.

What is find command?

find is a powerful command-line utility that searches one or more directory trees of a file system, locates files based on the condition. We can pass multiple arguments in find command to achieve many requirements.

I have just created some files for this testing and those are below.

# ls -lh /home/sudha/
total 0
-rw-r--r--. 1 root root 0 Mar 19 10:02 magi1.txt
-rw-r--r--. 1 root root 0 Mar 20 09:05 magi2.txt
-rw-r--r--. 1 root root 0 Mar 21 06:24 magi3.txt
-rw-r--r--. 1 root root 0 Apr 29 02:05 magi4.png
-rw-r--r--. 1 root root 0 Apr 29 06:05 magi4.txt
-rw-r--r--. 1 root root 0 Apr 29 03:02 magi5.png
-rw-r--r--. 1 root root 0 Apr 29 07:02 magi5.txt
-rw-r--r--. 1 root root 0 Apr 29 08:05 magi6.jpeg
-rw-r--r--. 1 root root 0 Apr 29 04:05 magi6.jpg
-rw-r--r--. 1 root root 0 Apr 29 09:24 magi7.jpeg
-rw-r--r--. 1 root root 0 Apr 29 05:24 magi7.jpg
-rw-r--r--. 1 root root 0 Mar 18 12:05 magi.txt

1) Search and Delete files based on the file modified time (mtime)

Example-1: In this example, we will search and print files that are older than 30 days using file modified time (mtime). This output will allow you to verify if these files are needed or not before deleting them.

# find /home/sudha -type f -mtime +30 -print
/home/sudha/magi1.txt
/home/sudha/magi3.txt
/home/sudha/magi.txt
/home/sudha/magi2.txt

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/sudha -type f -mtime +30 -delete;
or
# find /home/sudha -type f -mtime +30 -exec rm -f {} \;
or
# find /home/sudha -type f -mtime +30 | xargs rm -f

Details:

  • find: find is a command
  • /home/sudha: 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.

Example-2: In this example, we are going to find and print older than 1 day files based on extension. This output will help us to verify these files before performing deletion.

# find /home/sudha -type f -name "*.jpeg" -mtime +1 -print
/home/sudha/magi2.jpeg
/home/sudha/magi1.jpeg

Once you have confirmed that these files are no more requires then go ahead and delete it, using one of the following command.

# find /home/sudha -type f -name "*.jpeg" -mtime +1 -delete;
or
# find /home/sudha -type f -name "*.jpeg" -mtime +1 -exec rm -f {} \;
or
# find /home/sudha -type f -name "*.jpeg" -mtime +1 | xargs rm -f

2) Search and Delete files based on the file accessed time (atime)

Example-1: If you want to delete files older than 5 days based on the file accessed time instead of file modified time. Use the following example.

The below example will search and print the files older than 5 days.

# find /home/sudha -type f -atime +5 -print
/home/sudha/magi1.txt
/home/sudha/magi3.txt
/home/sudha/magi.txt
/home/sudha/magi2.txt

You can delete all the above files if it’s not needed using one of the following command. But if you want to keep any of the file for future reference then delete other files manually.

# find /home/sudha -type f -atime +5 -delete;
or
# find /home/sudha -type f -atime +5 -exec rm -f {} \;
or
# find /home/sudha -type f -atime +5 | xargs rm -f

Example-2: In this example, we are going to find and print older than 10 day files based on extension. This output will help us to verify these files before performing deletion.

# find /home/sudha -type f -name "*.jpeg" -atime +10 -print
/home/sudha/magi2.jpeg
/home/sudha/magi1.jpeg

Once you have confirmed that these files are no more requires then go ahead and delete it, using one of the following command.

# find /home/sudha -type f -name "*.jpeg" -atime +10 -delete;
or
# find /home/sudha -type f -name "*.jpeg" -atime +10 -exec rm -f {} \;
or
# find /home/sudha -type f -name "*.jpeg" -atime +10 | xargs rm -f

3) Search and Delete files based on file modified minutes (mmin)

Example-1: In this example, we are going to search and print older than 40 minutes files based on the file modified minutes (mmin). This output will help us to verify these files before performing deletion.

# find /home/sudha -type f -mmin -40 -print
/home/sudha/magi7.jpeg
/home/sudha/magi6.jpeg

Verify the above files and delete them if these are no more required using one of the following command.

# find /home/sudha -type f -mmin -40 -delete;
or
# find /home/sudha -type f -mmin -40 -exec rm -f {} \;
or
# find /home/sudha -type f -mmin -40 | xargs rm -f

Example-2: If you want delete files older than 5 hours based on their extension with file modified minutes (mmin).  Use the below example.

Use the below command print files older than 5 hours based on their extension.

# find /home/sudha -type f -name "*.txt" -mmin -300 -print
/home/sudha/magi4.txt
/home/sudha/magi5.txt

You can delete the above files using the below command.

# find /home/sudha -type f -name "*.txt" -mmin -300 -delete;
or
# find /home/sudha -type f -name "*.txt" -mmin -300 -exec rm -f {} \;
or
# find /home/sudha -type f -name "*.txt" -mmin -300 | xargs rm -f

4) Search and Delete files based on file accessed minutes (amin)

Example-1: If you want to delete files over 2 hours old based on the file accessed minutes (amin) instead of file modified minutes (mmin). Use the following example.

The below example will search and print the files older than 2 hours.

# find /home/sudha -type f -amin -120 -print
/home/sudha/magi7.jpeg
/home/sudha/magi6.jpeg

You can simple delete all of them in one go using the below commands if these are not needed.

# find /home/sudha -type f -amin -120 -delete;
or
# find /home/sudha -type f -amin -120 -exec rm -f {} \;
or
# find /home/sudha -type f -amin -120 | xargs rm -f

Example-2: This is similar to the above one but uses file accessed minutes (amin) instead of file modified minutes (mmin).
Run the below command to search them.

# find /home/sudha -type f -name "*.txt" -amin -540 -print
/home/sudha/magi4.txt
/home/sudha/magi5.txt

Run the below command to delete them.

# find /home/sudha -type f -name "*.txt" -amin -540 -delete;
or
# find /home/sudha -type f -name "*.txt" -amin -540 -exec rm -f {} \;
or
# find /home/sudha -type f -name "*.txt" -amin -540 | xargs rm -f

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

Leave a Reply

Your email address will not be published. Required fields are marked *