touch (Unix)

From Wikipedia, the free encyclopedia
Jump to: navigation, search

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.

History[edit]

A touch utility appeared in Version 7 AT&T UNIX. The version of touch bundled in GNU coreutils was written by Paul Rubin, Arnold Robbins, Jim Kingdon, and David MacKenzie.

Specification[edit]

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.

Usage[edit]

The SUS mandates the following options:

-a, change the access time only
-c, if the file does not exist, do not create it and do not report this condition
-d date_time, use the date_time specified to update the access and modification times
-m, change the modification time only
-r file, use the access and modification times of file
-t time, use the time specified (in the format below) to update the access and modification times

The time is specified in the format [[cc]yy]MMDDhhmm[.ss] where MM specifies the two-digit numeric month, DD specifies the two-digit numeric day, hh specifies the two-digit numeric hour, mm specifies the two-digit numeric minutes. Optionally ss specifies the two-digit seconds, cc specifies the first two digits of the year, and yy specifies the last two digits of the year.

Note that if invoked without these options, the standard specifies that the current date and time are used to change the access and modification times. This behaviour simulates an update to a file without having to change it, which may be desirable in certain situations (see the example below).

Other Unix and Unix-like operating systems may add extra options. For example, GNU touch adds a -d option, which enables time input in formats other than that specified.

Note that the dates of creation of symbolic links are not changed .

Examples[edit]

The simplest use case for touch is this:

 $ touch myfile.txt

Touch doesn't modify the contents of myfile.txt; it just updates the timestamp of the file to the computer's current date and time, whatever that happens to be. Or, if myfile.txt does not exist it is created, with zero length.

Here's an example that shows why we might want to do this. We wish to re-make a software project we are writing. We have changed the makefile and need to run make again. However, if we run make immediately we find that

 $ make
 make: nothing to be done for `all'

Since the source code file is already updated, we will need to use touch to simulate a file update, so make will run and recompile the software.

 $ touch project.c
 $ make

Then make will rebuild the project.

The creation date of links are unchanged. For example, on the following system, the date is the 20th Feb 2012, but a link was created on 25th Jan 2012. Despite touching the link, the date of this remains at 22nd Jan 2012 - it not changed to the 20th Feb 2012.

$ date
Wed Feb 20 09:45:50 GMT 2012
$ ls -l libcidn.so
lrwxrwxrwx  1 foobar foobar 22 Jan 25 01:41 libcidn.so -> ../../lib/libcidn.so.1
$ touch libcidn.so
$ ls -l libcidn.so
lrwxrwxrwx  1 foobar foobar 22 Jan 25 01:41 libcidn.so -> ../../lib/libcidn.so.1

Although commands like cp, grep, chmod etc. have a recursive switch (-r or -R or both) to apply the command recursively to the subdirectories, touch doesn't have this functionality yet (as of February, 2013). It can be accomplished by the following:

 $ find . -exec touch {} +

or to recursively set the date to be 5th July 2023, and the time 0201, the following will work

find . -exec touch  0705020123 {} +

Other operating systems[edit]

Programs that perform similar operations as the Unix touch utility are available for other operating systems, including Microsoft Windows and Mac OS.

  • Windows

touch dot exe - free and open source, for Windows.
File Date Touch - freeware for Windows
Synesis Software File Touch Shell Extension - freeware for Windows
The ntouch/dtouch page

See also[edit]

External links[edit]

Manual pages[edit]