Skip to content

Changing time stamp of a file in linux

Most of us would have wanted to know how we can change the timestamp of a file, well it's done using a command that we would have used several times, but might have never thought of checking its manual.

I was surprised when I saw it for the first time, as I have been using this command for years, but never knew, what it was intended for.

We can change the timestamp of a file using touch command.

  • Lets create a file and check its timestamp.

    [root@suresh-kumar21 test]# touch noname
    [root@suresh-kumar21 test]# stat noname 
    File: ‘noname’
    Size: 0             Blocks: 0          IO Block: 4096   regular empty file
    Device: ca01h/51713d    Inode: 75539074    Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:admin_home_t:s0
    Access: 2017-01-02 05:36:53.666076255 +0000
    Modify: 2017-01-02 05:36:53.666076255 +0000
    Change: 2017-01-02 05:36:53.666076255 +0000
    Birth: -
    [root@suresh-kumar21 test]#
    
  • Now we would change the access time of the file, for this, we would use the following flags.

    Flag/Argument Description
    -a To change access time
    -t To provide time
    [root@suresh-kumar21 test]# touch -a -t 201605050200 noname 
    [root@suresh-kumar21 test]# stat noname
    File: ‘noname’
    Size: 0             Blocks: 0          IO Block: 4096   regular empty file
    Device: ca01h/51713d    Inode: 75539074    Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:admin_home_t:s0
    Access: 2016-05-05 02:00:00.000000000 +0000
    Modify: 2017-01-02 05:36:53.666076255 +0000
    Change: 2017-01-02 05:39:23.425680365 +0000
    Birth: -
    [root@suresh-kumar21 test]#
    
  • Here, we can see that only access time has been changed.

  • Similarly, we can change the Modify time as well, but for this, we would have to execute the command without -a flag.
    [root@suresh-kumar21 test]# touch -t 201605050200 noname 
    [root@suresh-kumar21 test]# stat noname
    File: ‘noname’
    Size: 0             Blocks: 0          IO Block: 4096   regular empty file
    Device: ca01h/51713d    Inode: 75539074    Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:admin_home_t:s0
    Access: 2016-05-05 02:00:00.000000000 +0000
    Modify: 2016-05-05 02:00:00.000000000 +0000
    Change: 2017-01-02 05:40:08.788043092 +0000
    Birth: -
    [root@suresh-kumar21 test]# 
    
  • For more details, do refer to the man page for touch command.
    man touch
    
Back to top