What are mtime, atime and ctime in Unix?

mtime, atime, and ctime are timestamps associated with the modification, access and change of any file in unix. A common misunderstanding is ‘ctime is file creation time’, but this is not true. ctime means inode/file change time.

File created time is not tracked anywhere in unix, so you can’t really determine it. The below mentioned 3 times is all we have in unix.

mtime

mtime (modification time) indicates the timestamp that the contents of the file has been changed.

Remember,

only the contents. Not the attributes.

For instance, if you open a file and change some (or all) of its content, its mtime gets updated. If you change a file’s attribute (like read-write permissions, metadata), it’s mtime doesn’t change, but ctime will be updated.

Most of the times ctime and mtime will be the same, unless only the file attributes are updated. In that case only the ctime gets updated.

atime

atime (access time) is the timestamp that indicates the time that a file has been accessed.

The file may have been opened by you, or may have been accessed by some other program such as issuing commands or a remote machine.

Anytime a file has been accessed, file access time changes.

ctime

ctime (change time) is the timestamp of a file that indicates the time that it was changed.

Now, the modification can be in terms of its content or in terms of its attributes. Whenever anything about a file changes (except its access time), it’s ctime changes.

The ctime gets updated when the file attributes are changed, like changing the owner, changing the permission or moving the file to another filesystem but will also be updated when you modify a file.

I will demonstrate this by the following example.

[wot@unix ~] $ touch unix_time.txt
[wot@unix ~] $ stat unix_time.txt
File: ‘unix_time.txt’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd02h/64770d Inode: 72285 Links: 1
Access: (0664/-rw-rw-r–) Uid: ( 1003/wot) Gid: ( 1003/wot)
Access: 2016-05-16 19:50:39.673266579 +0530
Modify: 2016-05-16 19:50:39.673266579 +0530
Change: 2016-05-16 19:50:39.673266579 +0530
[wot@unix ~]

See that above timestamps, all are same when we touch the file.

I will try to print the contents of the file using cat command.

[wot@unix ~] $ cat unix_time.txt
[wot@unix ~] $ stat unix_time.txt
File: ‘unix_time.txt’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd02h/64770d Inode: 72285 Links: 1
Access: (0664/-rw-rw-r–) Uid: ( 1003/wot) Gid: ( 1003/wot)
Access: 2016-05-16 19:58:03.651258452 +0530
Modify: 2016-05-16 19:50:39.673266579 +0530
Change: 2016-05-16 19:50:39.673266579 +0530
[wot@unix ~] $

Since the unix_text.txt file is empty, it didn’t print anything to output. But looking at file stats, we see the access time has changed, while the mtime and ctime are remain unchanged.

Now, I will write something into the file.

[wot@unix ~] $ stat unix_time.txt
File: ‘unix_time.txt’
Size: 18 Blocks: 8 IO Block: 4096 regular file
Device: fd02h/64770d Inode: 72285 Links: 1
Access: (0664/-rw-rw-r–) Uid: ( 1003/wot) Gid: ( 1003/wot)
Access: 2016-05-16 20:02:03.165714605 +0530
Modify: 2016-05-16 20:02:03.165714605 +0530
Change: 2016-05-16 20:02:03.165714605 +0530
[wot@unix ~] $

See, all of its timestamps have been changed. Since the file had to be accessed to modify it, the atime has been changed. And since the contents of the file changed, its mtime and ctime both got changed.

Now, instead of contents, what happens if we change its attributes?

[wot@unix ~] $ ls -ltr
-rw-rw-r–. 1 wot wot 22 May 16 20:05 unix_time.txt
[wot@unix ~] $ chmod 777 unix_time.txt
[wot@unix ~] $ ls -ltr
-rwxrwxrwx. 1 wot wot 22 May 16 20:05 unix_time.txt
[wot@unix ~] $ stat unix_time.txt
File: ‘unix_time.txt’
Size: 22 Blocks: 8 IO Block: 4096 regular file
Device: fd02h/64770d Inode: 72287 Links: 1
Access: (0777/-rwxrwxrwx) Uid: ( 1003/wot) Gid: ( 1003/wot)
Access: 2016-05-16 20:05:06.225298858 +0530
Modify: 2016-05-16 20:05:06.225298858 +0530
Change: 2016-05-16 20:08:01.580898526 +0530
[wot@unix ~] $

Here I have changed the permission to the file and see how it’s ctime has been changed. Since only the attributes were changed and not the contents, the ctime changed while mtime remained as it is.

Comments

comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: