Working with Dates and Times


The UNIX System includes several tools for working with dates and times. Two of these are date, which can get the current time or format an arbitrary time, and touch, which can change the modification time associated with a file.

date

The date command prints the current time and date in any of a variety of formats. It is also used by the system administrator to set or change the system time. You can use it to timestamp data in files, to display the time as part of a prompt, or simply as part of your login .profile sequence.

By itself, date prints the date in a default format, like this:

 $ date Mon Sept 18 17:19:33 PDT 2006

You can change the information that date prints with format specification options. Date format specifications are entered as arguments to date. They begin with a plus sign (+), followed by codes that tell date what information to display and how to display it. These codes use the percent sign (%) followed by a single letter to specify particular types of information. Format specifications can include ordinary text that you specify as part of the argument.

Here is one example of the type of formatting you can use with date:

 $ date "+Today is %A, %B %d, %Y" Today is Monday, September 18, 2006

Table 19–4 lists some of the more useful date format specifications.

Table 19–4: date Format Specifications

Unit

Symbol

Example

Unit

Symbol

Example

Year

Y

y

2006

06

Hour

H

I

17 (00 to 23)

5 (1 to 12)

Month

B

b

m

November

Nov

11

Minute

M

23 (00 to 59)

Day of week

A

a

Saturday

Sat

Second

S

03

Day of month

d

e

04

4

A.M./P.M.

P

P

AM

pm

Day of year

j

256

Time

T

X

14:20:15

02:20:15 PM

Date

D

03/27/79

Newline

n

 

One common use of date is to create a timestamp, a string which can be added to data in order to mark the date when it was created. For example,

 $ cat output > "logfile.$(date "+%Y.%j, %X")" $ ls log* logfile.2006.261.17.19.48

uses command substitution to create a file with the date and time appended to the filename. In some versions of date, the command

 $ date +%s 1158625190

will print the number of seconds since January 1,1970 UTC, which is a common format for a timestamp.

Like the cal command, date can be used to look up a specific day. The GNU version of date has a d option that allows you to specify a particular time or date to display:

 $ date −d 1/1/2007 Mon Jan 1 00:00:00 PST 2007 $ date +%A −d 11/23         # Find the day of the week for 11/23 this year. Thursday

touch

Chapter 3 showed how you can use the touch command to create a new empty file. But the primary purpose of touch is to change the access and modification times for each of its filename arguments.

Every file in the UNIX file system has three times associated with it, and the touch command can be used to change two of them. One is the modification time, that is, the time when the file was last changed. This is the time that is displayed with ls 1. Files also have an access time, which can be displayed with ls lu. You can use the mtime and atime options to find in order to search for files according to these times.

The command

 $ touch filename

changes both the modification time and access time of filename to the current time. The command touch m changes only the modification time, and the a option causes touch to change only the access time.

One use of touch is in working with shell scripts that perform actions according to how recently a file was changed. For example, you could write a script to back up files that used touch to mark each file after copying it. The script could use find to search for files by modification date in order to copy only those files that had changed since the last backup.




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net