Thursday, 4 April 2013

mtime, ctime and atime

It is important to distinguish between a file or directory's change time (ctime), access time (atime), and modify time (mtime).

ctime -- In UNIX, it is not possible to tell the actual creation time of a file. The ctime--change time--is the time when changes were made to the file or directory's inode (owner, permissions, etc.). The ctime is also updated when the contents of a file change. It is needed by the dump command to determine if the file needs to be backed up. You can view the ctime with the ls -lc command.

atime -- The atime--access time--is the time when the data of a file was last accessed. Displaying the contents of a file or executing a shell script will update a file's atime, for example. You can view the atime with the ls -lu command.

mtime -- The mtime--modify time--is the time when the actual contents of a file was last modified. This is the time displayed in a long directoring listing (ls -l).

Find atime, ctime and mtime with ls

The simplest way to confirm the times associated with a file is to use ls command.
Timestamps are shown when using the long-format output of ls command, ls -l:
unixmantra# ls -l /tmp/file1
-rw-r--r-- 1 greys root 9 2013-08-25 09:10 /tmp/file1
This is the default output of ls -l, which shows you the time of the last file modification – mtime. In our example, file /tmp/file1 was last changed around 9:10am.
If we want to see the last access time for this file, atime – you need to use -lu options for ls. The output will probably show some later time:
unixmantra# ls -lu /tmp/file1
-rw-r--r-- 1 rose rose 9 2013-08-25 09:32 /tmp/file111
In the example, it's 9:32am.
Lastly, ls -lc will show you the last time our file was changed,ctime:
unixmantra# ls -lc /tmp/file111
-rw-r--r-- 1 rose rose 9 2013-08-25 09:31 /tmp/file1
To show you how this works, I'll change the ownership of the file and then run the same 3 ls commands to show you that only thectime had been updated. I run the date command just before doing anything else so that you can compare the times:
unixmantra# date
Sat Apr  5 07:35:16 IST 2008
unixmantra# chown root /tmp/file1
unixmantra# ls -lc /tmp/file1
-rw-r--r-- 1 root root 9 2008-04-05 07:35 /tmp/file1
unixmantra# ls -lu /tmp/file1
-rw-r--r-- 1 root root 9 2008-04-05 07:27 /tmp/file1
unixmantra# ls -l /tmp/file1
-rw-r--r-- 1 root root 9 2008-04-05 07:10 /tmp/file1

Show atime, ctime and mtime with stat command

In Linux distributions, you will probably find a stat command, which can be used to show all of the times in a more convenient way, and among plenty of other useful information about your file:
unixmantra# stat /tmp/file1
File: `/tmp/file1'
Size: 9             Blocks: 8          IO Block: 4096   regular file
Device: 811h/2065d    Inode: 179420      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2013-08-25 07:27:51.000000000 +0100
Modify: 2013-08-25 07:10:14.000000000 +0100
Change: 2013-08-25 07:35:22.000000000 +0100

$ cat file
file's atime is updated.

$ chmod g+w file
file's ctime is updated.

$ echo "File contents" >> file
file's ctime and mtime are updated.

0 blogger-disqus:

Post a Comment