Learn to use '-mtime' in 'find' CMD like a Pro.

The -mtime option in the find command is a powerful tool in Linux for locating files based on their modification time. It helps users identify files modified a specific number of days ago, making it easier to clean up old logs or archive files.

Learn to use '-mtime' in 'find' CMD like a Pro.
Photo by yannick Coffi see +< @ yannickcoffi @ PRINTS

The -mtime option in the find command works by counting full 24-hour periods from the current time, making it ideal for managing files based on their age. This feature is particularly useful for system maintenance tasks, where handling files by modification date can help save storage space and optimize performance.

Let’s dive into it.

Removing Files Based on Their Last Modification Date

To remove files based on their age, you can use the find command with the -mtime option. Here's how you can target files using +3 or -3 option:

+3 option targets files modified more than 3 days ago.
-3 option targets files modified within the last 3 days.

Confused¿ —Let's break it down.


Using -mtime +3


$ find /path/to/dir -mtime +3 -exec rm -f {} \;


-mtime +3: Indicates files modified more than 3 days ago (at least 4 days old).
-exec rm -f {} \;: Forces the removal of each file that matches the condition.


Basically; if today is January 15, the command will delete files modified on:

January 10 ✅ Yes
January 11 ✅ Yes
January 12 ✅ Yes
January 13 ❌ No
January 14 ❌ No
January 15 ❌ No


Using -mtime -3


$ find /path/to/dir -mtime -3 -exec rm -f {} \;


-mtime -3: Indicates files modified within the last 3 days.

-exec rm -f {} \;: Removes each file that matches the condition.

Basically; if today is January 15, the command will delete files modified on:

January 11 ❌ No
January 12 ❌ No
January 13 ✅ Yes
January 14 ✅ Yes
January 15 ✅ Yes



⚠️ CAUTION: Use rm with Care!

The rm command will permanently delete files, so always use it cautiously, especially when combined with find.

Testing Before Deleting

It's a good practice to test which files will be affected before running rm. You can do this by replacing rm -f with ls to list the files first:

$ find /path/to/dir -mtime +3 -exec ls -l {} \;


This will show you which files would be deleted without actually removing them.

This approach to file management, based on modification time, is extremely helpful for system admins and anyone looking to keep their system clean and organized. By regularly deleting old files, you can ensure your system remains optimized and efficient.


We hope these insights were helpful, despite the confusing but crucial differences between the options!

Keep Us Caffeinated  ⦿ ⦿