Skip to content

How to change the file timestamp on Linux

This tip might comes handy when you do a system check and you want to make sure you don’t check the same file twice.

Let’s pretend that our file1 is a conf file that needs review. As you can see the output of the command issued below shows that the file was last edited in June.
Today I want to check the file without editing it and make sure next time I won’t check it again:

$ ls -l
total 0
-rw-r--r-- 1 luca luca 290 2009-06-29 16:33 file1

Touch is an excellent tool in this case:

$ touch file1
$ ls -l
total 0
-rw-r--r-- 1 luca luca 0 2009-08-29 19:43 file1

The Modification Time has changed and so has the access time.

If you want to change just the modification time leaving the access time untouched try with the -m option

$ touch -m file1
$ ls -l
total 0
-rw-r--r-- 1 luca luca 0 2009-08-29 19:46 file1
$ stat file1
[..]
Access: 2009-08-29 19:43:45.000000000 +0100
Modify: 2009-08-29 19:46:15.000000000 +0100
Change: 2009-08-29 19:46:15.000000000 +0100

And -a is just for the Access Time.

Another interesting option is -t. It lets you set the time and the date with whatever you like. This is often used to do fishy things 🙂

$ touch -t 200701012301 file1
$ stat file1
[..]
Access: 2007-01-01 23:01:00.000000000 +0000
Modify: 2007-01-01 23:01:00.000000000 +0000
Change: 2009-08-29 19:52:26.000000000 +0100
comments powered by Disqus