Touch (Unix)
Encyclopedia
touch is a standard Unix
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...

 program
Computer program
A computer program is a sequence of instructions written to perform a specified task with a computer. A computer requires programs to function, typically executing the program's instructions in a central processor. The program has an executable form that the computer can use directly to execute...

 used to change a file
Computer file
A computer file is a block of arbitrary information, or resource for storing information, which is available to a computer program and is usually based on some kind of durable storage. A file is durable in the sense that it remains available for programs to use after the current program has finished...

's access and modification timestamps. It is also used to create a new empty file.

History

A touch utility appeared in Version 7 AT&T UNIX. The version of touch bundled in GNU
GNU
GNU is a Unix-like computer operating system developed by the GNU project, ultimately aiming to be a "complete Unix-compatible software system"...

 coreutils was written by Paul Rubin, Arnold Robbins, Jim Kingdon, David MacKenzie

Specification

The Single Unix Specification
Single UNIX Specification
The Single UNIX Specification is the collective name of a family of standards for computer operating systems to qualify for the name "Unix"...

 (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

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
-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 c]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
Unix-like
A Unix-like operating system is one that behaves in a manner similar to a Unix system, while not necessarily conforming to or being certified to any version of the Single UNIX Specification....

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

Examples

The simplest use case
Use case
In software engineering and systems engineering, a use case is a description of steps or actions between a user and a software system which leads the user towards something useful...

 for touch is thus:

# touch myfile.txt

Touch doesn't modify the contents of myfile.txt; it just updates the timestamp
Timestamp
A timestamp is a sequence of characters, denoting the date or time at which a certain event occurred. A timestamp is the time at which an event is recorded by a computer, not the time of the event itself...

 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.

Here's how to change the date and time of a file.


# touch -t 200701310846.26 index.html
# touch -d '2007-01-31 8:46:26' index.html
# touch -d 'Jan 31 2007 8:46:26' index.html
#
(* In the case of Win32(XP) command prompt, use <""> instead of <>.
Or "touch: Invalid date format" error will appear.)


The above three are equivalent: they will change the date and time of index.html to January 31, 2007 at 8:46:26am.

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 August, 2008). It can be accomplished by the following:


# find . -exec touch {} +

Other operating systems

Programs that perform similar operations as the Unix touch utility are available for other operating systems, including Microsoft Windows
Microsoft Windows
Microsoft Windows is a series of operating systems produced by Microsoft.Microsoft introduced an operating environment named Windows on November 20, 1985 as an add-on to MS-DOS in response to the growing interest in graphical user interfaces . Microsoft Windows came to dominate the world's personal...

 and Mac OS
Mac OS
Mac OS is a series of graphical user interface-based operating systems developed by Apple Inc. for their Macintosh line of computer systems. The Macintosh user experience is credited with popularizing the graphical user interface...

.
  • Windows

File Date Touch - freeware for Windows

External links


Manual pages

  • touch — manual page from GNU
    GNU
    GNU is a Unix-like computer operating system developed by the GNU project, ultimately aiming to be a "complete Unix-compatible software system"...

     coreutils
  • touch — manual page from OpenBSD
    OpenBSD
    OpenBSD is a Unix-like computer operating system descended from Berkeley Software Distribution , a Unix derivative developed at the University of California, Berkeley. It was forked from NetBSD by project leader Theo de Raadt in late 1995...

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK