Tuesday, June 28, 2011

3 different time stamps associated with a file in Unix



  Any file present or created in Unix has 3 different time stamps associated with it . The 3 different timings are:

1. Last modified time - This refers to the timing when the file has been modified last. For a newly created file, this refers to the time when the file is created.

2. Last accessed time -  This refers to the timing when the file has been accessed last. Access can be either when the file is read, or the file is written to or even if the file has been executed. The last accessed time gets updated when any of the above file operations is being done on a file.

3. Last changed time - This refers to the timing when the file status has been changed last. The change in file status could be due to any of the following reasons:
   i) File ownership being changed.
  ii) Group to which the file belongs to is changed.
 iii) File permissions such as read, write or execute permissions has been applied or removed from the file.
 iv) Advanced file permissions such as SUID, SGID or Sticky bit has been applied or removed on the file.
  v) When the file content is updated.

   As seen in one of our earlier inode articles, all these 3 time stamps are saved as part of the inode information.

Example: 
Let us try to understand the above time stamps with an example by creating a file, say f1:
$ touch f1
Now, let us see the different time stamps:

The timing shown in the ls -l command is the last modified time of a file. In this case, it is the file creation time since the file is new:
$ ls -l f1
-rw-r--r-- 1 guru None 0 Jun 25 16:40 f1
The last accessed time can be seen by using the command: ls -lu
$ ls -lu f1
-rw-r--r-- 1 guru None 0 Jun 25 16:40 f1
The last changed time can be seen using: ls -lc
$ ls -lc f1
-rw-r--r-- 1 guru None 0 Jun 25 16:40 f1
   As seen above, in this case, all the time stamps are equal since the file is new.

1. Let us update the file contents and see how these 3 timings changes:
$ date
Sat Jun 25 16:45:45 IST 2011
Writing the "welcome" word to the file:
$ echo "welcome" > f1
The last modified time:
$ ls -l f1
-rw-r--r-- 1 guru None 8 Jun 25 16:45 f1
The last accessed time:
$ ls -lu f1
-rw-r--r-- 1 guru None 8 Jun 25 16:45 f1
The last changed time:
$ ls -lc f1
-rw-r--r-- 1 guru None 8 Jun 25 16:45 f1
   As shown above, when the file contents are changed, all the 3 time stamps get updated. The last modification time gets changed since the file is being modified. The last access time is changed since the file is being written. The last changed time is updated since the file status is being changed. Hence, all the 3 time stamps get updated when the file content is being updated.

2. Let us try to read the file f1:
$ date
Sat Jun 25 16:50:34 IST 2011
$ cat f1
welcome
The last modified time:
$ ls -l f1
-rw-r--r-- 1 guru None 8 Jun 25 16:48 f1
The last accessed time:
$ ls -lu f1
-rw-r--r-- 1 guru None 8 Jun 25 16:50 f1
The last changed time:
$ ls -lc f1
-rw-r--r-- 1 guru None 8 Jun 25 16:48 f1
    As above, when the file content is being read, only the file access time is changed since the file is just being read. The same would have happened even if we would have executed the file.

3. Let us try to change the file permission of the file f1 by giving write permission to the group:
$ date
Sat Jun 25 16:53:34 IST 2011

$ chmod g+w f1
The last modified time:
$ ls -l f1
-rw-rw-r-- 1 guru None 8 Jun 25 16:48 f1
The last accessed time:
$ ls -lu f1
-rw-rw-r-- 1 guru None 8 Jun 25 16:50 f1
The last changed time:
$ ls -lc f1
-rw-rw-r-- 1 guru None 8 Jun 25 16:53 f1
    As shown, only the last changed time is being updated since only file status(file permission in this case) is being changed.

No comments:

Post a Comment