start page | rating of books | rating of authors | reviews | copyrights

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 21.6 A File's Inode Change (not  "Creation"!) TimeChapter 21
More About Managing Files
Next: 21.8 The MAILCHECK and mail Variables Check More than Mail
 

21.7 Setting File Modification Time with touch

How can you make a file quickly (often for some kind of test)? In the Bourne shell, use the command below. Because this command uses a built-in (1.10) operator, it's fast and efficient. This creates a new file or empties an existing file:

$ > filename

The C shell doesn't allow that. From the C shell, you can empty a file by copying /dev/null onto it (24.1). The easiest way to create an empty file is with the touch command. touch is also useful from any shell to change an existing file's modification time to "now"-without changing the file's contents (usually for an automatic file time comparison (17.8, 28.13, 21.9)). You can touch more than one file at a time. Just type:

% touch filename1 filename2 ...

touch
Some versions of touch (including the GNU version on the CD-ROM) can create a file with an arbitrary timestamp. That is, you can use touch to make a file that's backdated to any point in the past (or, for that matter, postdated to some point in the future). If your version can do that, the syntax is probably like:

% touch date filename1 filename2 ...

where date has the form:

modyhrmiyy

and:

mo

is two digits, representing the month.

dy

is two digits, representing the day of the month.

hr

is two digits, representing the hour (on a 24-hour clock).

mi

is two digits, representing the minute within the hour.

yy

is two digits, representing the year (within the twentieth century). These two digits are optional; if you omit them, date assumes the current year.

For example, to create a file dated 4 p.m., March 20 of this year, give the command:

% touch 03201600 foo

If you don't want to use a different timestamp (that is, you want the current time) and the filename starts with a digit, touch might think that the filename is a time and complain "date: bad conversion." To start a filename with a digit, use a relative pathname that starts with a . (dot) (1.21). For example, to make a file123456 in the current directory:

% touch ./123456

Article 22.16 explains cpmod, a program on the CD-ROM for copying dates and permissions from file to file.

- ML, JP


Previous: 21.6 A File's Inode Change (not  "Creation"!) TimeUNIX Power ToolsNext: 21.8 The MAILCHECK and mail Variables Check More than Mail
21.6 A File's Inode Change (not "Creation"!) TimeBook Index21.8 The MAILCHECK and mail Variables Check More than Mail

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System