How To Find 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 the space for some other purpose.

Some of the examples are, we can remove 30 days old backup files if we are taking daily backup.

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

This can be achieved with help of find command. The find command allow us to input multiple arguments together to achieve this.

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

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.

What Is atime?

File Last Access Time – Access time shows the last time the data from a file or directory was accessed by any of the process such as command or script, etc,.

What Is ctime?

File Last Change Time – ctime shows when your file or directory metadata got changed. It means when the file attributes are changed like ownership or group, etc,.

What Is mtime?

File Last Modify Time – mtime shows when you modify a file contents or save a file. Most of the times ctime and mtime will be the same, unless the file attributes are updated.

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

Method-1 : How To Find And Delete Files Older Than 30 Days In Linux Using mtime?

In this example, we are going to find and print older than 30 days files using mtime option. This output will help us to verify these files before performing deletion.

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

Method-2 : How To Find And Delete Files Older Than 30 Days In Linux Using atime?

In this example, we are going to find and print older than 30 days files using atime option. This output will help us to verify these files before performing deletion.

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

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

Method-3 : How To Find And Delete Files Older Than 5 Hours In Linux Using mmin?

In this example, we are going to find and print older than 5 hours files. This output will help us to verify these files before performing deletion.

# find /home/sudha -type f -mmin -300 -print
/home/sudha/magi7.jpeg
/home/sudha/magi6.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 -mmin -300 -delete;
or
# find /home/sudha -type f -mmin -300 -exec rm -f {} \;
or
# find /home/sudha -type f -mmin -300 | xargs rm -f

Method-4 : How To Find And Delete Files Older Than 5 Hours In Linux Using amin?

In this example, we are going to find and print older than 5 hours files. This output will help us to verify these files before performing deletion.

# find /home/sudha -type f -amin -300 -print
/home/sudha/magi7.jpeg
/home/sudha/magi6.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 -amin -300 -delete;
or
# find /home/sudha -type f -amin -300 -exec rm -f {} \;
or
# find /home/sudha -type f -amin -300 | xargs rm -f

Method-5 : How To Find And Delete Files Based On Extension Older Than 9 Hours In Linux Using mmin?

In this example, we are going to find and print older than 9 hours files based on extension. This output will help us to verify these files before performing deletion.

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

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 "*.txt" -mmin -540 -delete;
or
# find /home/sudha -type f -name "*.txt" -mmin -540 -exec rm -f {} \;
or
# find /home/sudha -type f -name "*.txt" -mmin -540 | xargs rm -f

Method-6 : How To Find And Delete Files Based On Extension Older Than 9 Hours In Linux Using amin?

In this example, we are going to find and print older than 9 hours files based on extension. This output will help us to verify these files before performing deletion.

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

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 "*.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

Method-7 : How To Find And Delete Files Based On Extension Older Than 1 Day In Linux Using mtime?

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

Method-8 : How To Find And Delete Files Based On Extension Older Than 1 Day In Linux Using atime?

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" -atime +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" -atime +1 -delete;
or
# find /home/sudha -type f -name "*.jpeg" -atime +1 -exec rm -f {} \;
or
# find /home/sudha -type f -name "*.jpeg" -atime +1 | xargs rm -f

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...