Difference between Access, Modification and Change Time in Linux?

Have you ever used the stat command? If yes, then a common question asked is what is the difference between access, modification and change time?

Difference between Access, Modification and Change Time in Linux?

Access Time: is the time when the file was last accessed or read. For example, using the cat, head or an editor. But remember you did not modify the contents.

Modification Time: is the time when the contents of the file was last modified. For example, you used an editor to add new content or delete some existing content.

Change Time: is the time when the file’s inode has been changed. For example, by changing permissions, ownership, file name, number of hard links.

How to update the Modification Time of a File in Linux?

Now let’s try to change the modification time of a file. Below is shown the modification time of file access which is 2020-08-13 22:24

Next, we use the cat command to redirect some new content into file access. Hence, we have modified the existing content with the new one. Use stat again and we can see that the modification time is changed to 2020-08-14 00:20:43 which is the current system time.

modify time in linux

How to update the Change Time of a File in Linux?

To update just the Change Time of a file a simple method is just to change the file permissions. The steps are:

First, check the original Change time using stat command.
Next, use chmod command to change the permissions
Finally, use stat again and you will see the Change time is updated

How to update only the Access Time?

This one is most tricky and I am pretty sure you will not find an example of it easily on the net. (Don’t say $touch -a <filename> because here you did not actually accessed the file, just fooled the system that you did). This is because by default the system does not update the Access Time after every access to the file because of performance reasons. For example, on a server a particular file may be accessed by many people. Now to update the access time after each access will take those many disk access operations which will lead to low system performance.

So, even if you access the file to read using cat or head command, it won’t update the Access Time.

The way around is to change this default behaviour. To do this you need to use strictatime during mount.

So, I will do it my mounting a pendrive to my system and during mounting will change allow updating the Access Time after each access using strictatime option.

Step1: Plugin your pendrive
Step2: Locate it in /dev. It will be with the name sdb1 or sdc1 or something like that (the name is not fixed. in example below it is sdb1)


Step3: Create a mount point (i.e., a directory where you want to mount the pendrive)

$mkdir mount1


Step4: mount as below (root permissions will be required)

$mount -o strictatime <drive> <mount_point>

Step5: Now create a file inside this mount point (pen-drive).
Step6: check its Access Time using stat
Step7: View the contents of file

$head <filename>

Step8: Check the updated Access Time using stat

Relevant Commands

mkdir
head
chmod

Leave a Reply

Your email address will not be published. Required fields are marked *