We're an ISO27001:2013 Certified Supplier

calendar

The touch command is one of the long-established commands that, by default, either creates a file if it doesn’t exist or updates the access and modification times of a file if it does. It’s useful for those situations where behaviour depends upon either the existence or otherwise of a file, or the date of it:

$ ls -l myfile
ls: cannot access myfile: No such file or directory
$ touch myfile
$ ls -l myfile
-rw-r--r-- 1 kae kae 0 Jan 25 09:36 myfile
$

By default, the times set are the current date and time:

$ date
Mon 15 Jan 07:21:42 GMT 2018
$ touch myfile
$ ls -l myfile
-rw-r--r-- 1 kae kae 0 Jan 15 07:21 myfile

Specify the Date

Its’s possible to set the timestamp of the file to any arbitrary value:

$ ls -l myfile
-rw-r--r-- 1 kae kae 0 Jan 15 07:21 myfile
$ touch --date="21 July 1969 02:56:15" myfile
$ ls -l myfile
-rw-r--r-- 1 kae kae 0 Jul 21  1969 myfile
$

Show the Time

In the final example above the year is shown in preference to the time because the date of this file in not the current year. One way to see the access and last modified times is with the stat command:

$ stat myfile  
 File: myfile 
 Size: 0               Blocks: 0          IO Block: 4096   regular empty file 
Device: 27h/39d Inode: 722482      Links: 1 
Access: (0644/-rw-r--r--)  Uid: ( 1000/     kae)   Gid: ( 1000/     kae) 
Access: 1969-07-21 02:56:15.000000000 +0100 
Modify: 1969-07-21 02:56:15.000000000 +0100 
Change: 2018-01-15 08:23:49.206066409 +0000 
Birth: -

We can customise the stat command to show only the modified and last access times by specifying a format:

$ stat --printf="Access time: %x\nModified time: %y\n" myfile  
Access time: 1969-07-21 02:56:15.000000000 +0100 
Modified time: 1969-07-21 02:56:15.000000000 +0100

Other Ways to Specify Timestamps

It may be more convenient to use the -t option, which allows the time/date to be specified in the format[[CC]YY]MMDDhhmm[.ss]:

$ touch -t 196907211354 myfile
$ stat --printf="Access time: %x\nModified time: %y\n" myfile  
Access time: 1969-07-21 13:54:00.000000000 +0100 
Modified time: 1969-07-21 13:54:00.000000000 +0100

By default, both the last access time and the modification time are set with touch. That can be changed by using the -a (change only the access time) or -m (only the modification time) options:

$ touch -a -t 201712250300 myfile  
$ stat --printf="Access time: %x\nModified time: %y\n" myfile  
Access time: 2017-12-25 03:00:00.000000000 +0000 
Modified time: 1969-07-21 13:54:00.000000000 +0100

In Closing

Both stat and touch are handy commands to have up your sleeve for when they’re really needed.

Could This Tech Tip Be Improved?

Let us know in the comments below.

Leave a Reply

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

Secure. Reliable. Scalable.

If that doesn't describe your current Linux systems, check out our FREE Linux Survival Guide to help you get your systems up to scratch today!

  • This field is for validation purposes and should be left unchanged.