Modifying timestamps in Unix

The touch command is one of those often used tools in Unix that you have probably used hundreds of times, but I bet you don’t realize it’s full potential. Did you know that you can use it to update a file’s timestamp to any time and date, not just the current time and date?

Why modify file timestamps?

There are quite a few legitimate reasons why you may want to update timestamps on a certain file. Ranging from source control approaches to storage usage analysis, there are processes out there which rely on the timestamps associated with each file and directory of yours.

Changing timestamps of a time to the current system time

The default behaviour of touch command is to change all three timestamps associated with a file to the current system time.

You simply specify the filename as a command line parameter, no other options are needed. If there isn't a file with the specified name, touch command will create it for you if permissions allow it:

server808> ls -l newfile
newfile: No such file or directory

server808> touch newfile

server808> ls -l newfile
-rw-r--r--   1 oracle   oinstall       0 May 14 15:58 newfile

As you can see from the example, the file which isn't originally found, gets created by the touch command and has its timestamps set to the current system time and date.

Changing file timestamps to a specific date and time

If you would like to set a specific time and date for the timestamp of a file or directory, then touch command will gladly accept one via the -t option (to update the last modified and last accessed times) or –m option (to update the modification time only). You can specify a timestamp in both the past and the future.

The template for the timestamp is [[CC]YY]MMDDhhmm[.ss]

This example resets the date to October 16th:

server808> ls -l myfile
-rw-r--r--   1 oracle   oinstall       0 May 14 15:58 myfile

server808> touch -t 10161000 myfile

server808> ls -l myfile
-rw-r--r--   1 oracle   oinstall  432710 Oct 16  2010 myfile

Note that the change time (ctime) is set to a different date because this field reflects the last update to the inode behind a file, as a result it always reflects the current time.

Changing file timestamps to the same as another file

Finally, the really useful option for synchronizing access and modification time fields between multiple files is to use reference file. A reference file is the file which already has the timestamps you'd like to copy and can be specified via the –r or –f command line options.

In the example below I decide to makes some changes to my .profile, so before I begin I make a backup copy to a new file called .profile.old. Because I want the backup file to reflect the time and date of the .profile before I make any changes to it I use the touch –r command to copy the timestamp values:

server808> cp .profile .profile.old
server808> ls -l .profile*
-rw-r--r--   1 oracle   oinstall     982 Dec 10  2008 .profile
-rw-r--r--   1 oracle   oinstall     982 May 14 16:09 .profile.old

server808> touch -r .profile .profile.old

server808> ls -l .profile*
-rw-r--r--   1 oracle   oinstall     982 Dec 10  2008 .profile
-rw-r--r--   1 oracle   oinstall     982 Dec 10  2008 .profile.old
Of course you could achieve the same result simply by using  the copy command with the –p option: cp –p .profile .profile.old