Touch (Unix)
From Wikipedia, the free encyclopedia
- The correct title of this article is touch (Unix). The initial letter is shown capitalized due to technical restrictions.
touch is a standard Unix program used to change a file's access and modification timestamps. It is also sometimes used to create a blank file.
Contents |
[edit] History
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, David MacKenzie and Randy Smith.
[edit] Specification
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.
[edit] 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 [[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.
[edit] Examples
The simplest use case for touch is thus:
# touch myfile.txt
Touch doesn't modify the contents of myfile.txt; it just updates the timestamp of a file to the computer's current date and time, whatever that happens to be.
Here's 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 succeeds.
Here's how to change the date and time of a file to a specific date:
# touch -t 200109110846.26 timeline.html # touch -d '2001-9-11 8:46:26' timeline.html # touch -d 'sep 11 2001 8:46:26' timeline.html
The above three are equivalent: they will change the date and time of timeline.html to September 11, 2001 at 8:46:26am.
[edit] ntouch/dtouch
ntouch is a tool made by Florian Balmer. It is a Windows version of the touch utility. It can also set the stamp to a specific time.
dtouch, also by Florian, is a companion tool to perform what touch does, but offsetting the time already on the file rather than setting it to the current time.
[edit] See also
[edit] External links
- touch specification from the Single Unix Specification
- The ntouch/dtouch page
- examples showing how to use touch
[edit] Manual pages
Unix command line programs and builtins (more) | |||
File and file system management: | cat | chattr | cd | chmod | chown | chgrp | cp | du | df | file | fsck | ln | ls | lsof | mkdir | mount | mv | pwd | rm | rmdir | split | touch | ||
Process management: | at | chroot | crontab | exit | kill | killall | nice | pgrep | pidof | pkill | ps | sleep | time | top | wait | watch | ||
User Management/Environment: | env | finger | id | mesg | passwd | su | sudo | uname | uptime | w | wall | who | whoami | write | ||
Text processing: | awk | comm | cut | ex | head | iconv | join | less | more | paste | sed | sort | tail | tr | uniq | wc | xargs | ||
Shell programming: | echo | expr | printf | unset | Printing: | lp |
Communications: inetd | netstat | ping | rlogin | traceroute |
Searching: find | grep | strings |
Miscellaneous: banner | bc | cal | man | size | yes |