Find mtime of File in Linux
The mtime is a timestamp in Linux that tells you when the file was modified last time.
There are two ways you can find the mtime in Linux: You can either use commands that give you mtime by default or use the mtime
flag.
In this tutorial, I will walk you through 3 ways you can find mtime in Linux:
- Using the stat command (easy)
- Using the ls command
I'll also discuss using the find command with the mtime
flag.
Find mtime using the stat command
The stat command in Linux is used to get detailed information about the filesystem or file in Linux.
But the best part is it shows the mtime along with the ctime and atime by default without any additional flags so all you have to do is append the filename to the stat command as shown:
stat Filename
To learn more about the mtime, ctime and atime timestaps, refer to our detailed guide on timestamps in Linux:
Find mtime using the ls command
When you use the ls command with the -l
flag to get the list view, it also shows the mtime of the file.
The best part of using the ls command for this purpose is you can use a directory as a target and list down mtime of multiple files at once.
To use the ls command to find the mtime, all you have to do is use the following command syntax:
ls -l filename/directory
Additional Tip: Find files based on mtime with find command
While the find command may look super complex to use (cough regex cough), once you get used to it, there's no going back.
But this time, you are required to use the -mtime
flag with the find command, and being one of the most powerful commands of Linux, it is obvious that you get multiple options there.
To find files that were modified exactly n
days ago:
find /path/to/search -mtime n
To find files that were modified less than n
days ago:
find /path/to/search -mtime -n
To find files that were modified more than n
days ago:
find /path/to/search -mtime +n
So let's say I want to list down files inside the /usr
directory which was modified 5 days ago, then I will be using the following command:
find /usr -mtime 5
If you are interested, you can check our detailed guide explaining how to find directories that were modified N days ago:
Wrapping Up...
This was a quick tutorial on how you can find the modified time (mtime) in Linux in 3 different ways.
I've tried to keep this short and helpful so you can get the most out of it in the least time possible. I hope you will find this helpful and less time-consuming.
If you have questions or want to suggest what should be covered next, then leave a comment.