Linux Touch Directory Command

As stated in the manpage, the primary use of touch is this:

Update the access and modification times of each FILE to the current time.

So it updates the access and modification times of the directory.


The example at the top doesn't make it clear that "touch" sets the time of the directory (the new time in the listing was the same as the original, simply because the touch was so soon after the directory was created)... yes, the definition of "FILE" in the documentation does include directories, so you can use touch to change their timestamps, BUT...

There are some situations where you cannot change a directory's timestamp (for some types of remotely-mounted directories) even if you have permission to do anything else. e.g. this NFS mount:

$ touch -d "2014-07-02 12:15" /public/test.dir    
touch: setting times of ‘/public/test.dir’: Operation not permitted

Also, you can get into problems with some old dates not being supported on some servers, e.g.:

$ mkdir  /Acer/kopies/test.dir
$ ls -ald /Acer/kopies/test.dir
drwxr-xr-x 2 mark aitchison 0 Mar 11 17:40 /Acer/kopies/test.dir

This works:

$ touch "2014-04-01 00:00" /Acer/kopies/test.dir
$ ls -ald /Acer/kopies/test.dir
drwxr-xr-x 2 mark aitchison 0 Apr  1  2014 /Acer/kopies/test.dir

This would work on a local dir but gives a strange date under Samba mounts:

$ touch -d "1955-07-02 12:15" /Acer/kopies/test.dir
$ ls -ald /Acer/kopies/test.dir
drwxr-xr-x 2 mark aitchison 0 Nov 26  60410 /Acer/kopies/test.dir
                              ~~~~~~~~~~~~~

touch is a standard Unix program used to change a file's access and modification timestamps. It is also used to create a new empty file.

The Single Unix Specification (SUS) specifies that touch should change the access times, modification times, or both, for a file. The file is identified by a pathname supplied as a single argument. It also specifies that if the file identified does not exist, the file is created and the access and modification times are set as specified. If no new timestamps are specified, touch uses the current time.

-Wikipedia

You can find more in-depth information on the touch command (or any other command you would like to know about) by using the man command like this:

man touch


The immediate reason for the question is the fact that the default time resolution of ls -l is in minutes, so a filesystem entry touched within the same minute will show up exactly the same in the default ls -l output.

The solution to this is described at https://superuser.com/questions/355318/how-to-have-linux-ls-command-show-second-in-time-stamp and basically involves running ls -l --time-style=full-iso instead.