mtime – file modification timestamp in Unix

mtime is one of the three timestamps in Unix that are maintained for each file in most of the filesystems.

Purpose of mtime

The real purpose of the mtime timestamp is to track the last time of changing the contents of a file. Various commands will allow you to access this information later. For example, ls command allows showing list of files along with their last modification times (it’s also possible to get ls to confirm the last access time (atime timestamp)for any file).

mtime example

Here’s how you can see mtime in real life. Let’s create a file named example.txt and get a full ls listing on it:

greys@ubuntu:~$ date
Fri Sep 28 10:25:40 IST 2012
greys@ubuntu:~$ > example.txt
greys@ubuntu:~$ ls -l example.txt
-rw-r--r-- 1 greys greys 0 2012-09-28 10:25 example.txt

As you can see, the last modification of the “example.txt” file is 10:25am.

Now let’s wait a minute:

greys@ubuntu:~$ sleep 60

…confirm the file’s mtime is still the same:

greys@ubuntu:~$ ls -l example.txt
-rw-r--r-- 1 greys greys 0 2012-09-28 10:25 example.txt

… and now make the change by adding a line “change” to our file:

greys@ubuntu:~$ echo "change" >> example.txt

And if we check the file’s mtime timestamp, it will be updated – in my case 10:27am:

greys@ubuntu:~$ ls -l example.txt
-rw-r--r-- 1 greys greys 7 2012-09-28 10:27 example.txt

More info on mtime