Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [148]

By Root 794 0
its last-modification time. Clearly, the safe way to do that job is with touch, because typing > when you meant >> would inadvertently destroy the file contents.

touch is sometimes used in shell scripts to create empty files: their existence and possibly their timestamps, but not their contents, are significant. A common example is a lock file to indicate that a program is already running, and that a second instance should not be started. Another use is to record a file timestamp for later comparison with other files.

By default, or with the -m option, touch changes a file's last-modification time, but you can use the -a option to change the last-access time instead. The time used defaults to the current time, but you can override that with the -t option, which takes a following argument of the form [[CC]YY]MMDDhhmm[.SS], where the century, year within the century, and seconds are optional, the month of the year is in the range 01 through 12, the day of the month is in the range 01 through 31, and the time zone is your local one. Here is an example:

$ touch -t 197607040000.00 US-bicentennial

Create a birthday file

$ ls -l US-bicentennial

List the file

-rw-rw-r-- 1 jones devel 0 Jul 4 1976 US-bicentennial

touch also has the -r option to copy the timestamp of a reference file:

$ touch -r US-bicentennial birthday

Copy timestamp to the new birthday file

$ ls -l birthday

List the new file

-rw-rw-r-- 1 jones devel 0 Jul 4 1976 birthday

The touch command on older systems did not have the -r option, but all current versions support it, and POSIX requires it.

For the time-of-day clock, the Unix epoch starts at zero at 00:00:00 UTC[1] on January 1, 1970. Most current systems have a signed 32-bit time-of-day counter that increments once a second, and allows representation of dates from late 1901 to early 2038; when the timer overflows in 2038, it will wrap back to 1901. Fortunately, some recent systems have switched to a 64-bit counter: even with microsecond granularity, it can span more than a half-million years! Compare these attempts on systems with 32-bit and 64-bit time-of-day clocks:

$ touch -t 178907140000.00 first-Bastille-day

Create a file for the French Republic

touch: invalid date format `178907140000.00' A 32-bit counter is clearly inadequate

$ touch -t 178907140000.00 first-Bastille-day

Try again on system with 64-bit counter

$ ls -l first-Bastille-day

It worked! List the file

-rw-rw-r-- 1 jones devel 0 1789-07-14 00:00 first-Bastille-day

Future dates on systems with 64-bit time-of-day clocks may still be artificially restricted by touch, but that is just a software limit imposed by the shortsighted POSIX requirement that the century have two digits:

$ touch -t 999912312359.59 end-of-9999

This works

$ ls -l end-of-9999

List the file

-rw-rw-r-- 1 jones devel 0 9999-12-31 23:59 end-of-9999

$ touch -t 1000001010000.00 start-of-10000

This fails

touch: invalid date format `1000001010000.00'

Fortunately, GNU touch provides another option that avoids the POSIX restriction:

$ touch -d '10000000-01-01 00:00:00' start-of-10000000

Into the next millionenium!

$ ls -l start-of-10000000

List the file

-rw-rw-r-- 1 jones devel 0 10000000-01-01 00:00 start-of-10000000

* * *

[1] UTC is essentially what used to be called GMT; see the glossary entry for Coordinated Universal Time.

Creating and Using Temporary Files

While pipes eliminate much of the need for them, temporary files are still sometimes required. Unlike some operating systems, Unix has no notion of scratch files that are somehow magically removed when they are no longer needed. Instead, it provides two special directories, /tmp and /var/tmp (/usr/tmp on older systems), where such files are normally stored so that they do not clutter ordinary directories in the event that they are not cleaned up. On most systems, /tmp is cleared when the system boots, but /var/tmp must survive reboots because some text editors place backup files there to allow data recovery after a system crash.

Because /tmp is so heavily

Return Main Page Previous Page Next Page

®Online Book Reader