Simply invoked, date prints the date and
time to stdout. Where this command gets
interesting is in its formatting and parsing options.
Example 12-10. Using date
# Exercising the 'date' command
echo "The number of days since the year's beginning is `date +%j`."
# Needs a leading '+' to invoke formatting.
# %j gives day of year.
echo "The number of seconds elapsed since 01/01/1970 is `date +%s`."
# %s yields number of seconds since "UNIX epoch" began,
#+ but how is this useful?
suffix=$(date +%s) # The "+%s" option to 'date' is GNU-specific.
# It's great for creating "unique" temp filenames,
#+ even better than using $$.
# Read the 'date' man page for more formatting options.
The -u option gives the UTC (Universal
bash$ dateFri Mar 29 21:07:39 MST 2002bash$ date -uSat Mar 30 04:07:42 UTC 2002
The date command has quite a number of
output options. For example %N gives the
nanosecond portion of the current time. One interesting use for
this is to generate six-digit random integers.
date +%N | sed -e 's/000$//' -e 's/^0//'
# Strip off leading and trailing zeroes, if present.
There are many more options (try man date).
# Echoes day of the year (days elapsed since January 1).
# Echoes hour and minute in 24-hour format, as a single digit string.
# The 'TZ' parameter permits overriding the default time zone.
date # Mon Mar 28 21:42:16 MST 2005
TZ=EST date # Mon Mar 28 23:42:16 EST 2005
# Thanks, Frank Kannemann and Pete Sjoberg, for the tip.
SixDaysAgo=$(date --date='6 days ago')
OneMonthAgo=$(date --date='1 month ago') # Four weeks back (not a month).
OneYearAgo=$(date --date='1 year ago')
See also the very similar times command in the previous
As of version 2.0
of Bash, time became a shell reserved word,
with slightly altered behavior in a pipeline.
Utility for updating access/modification times of a
file to current system time or other specified time,
but also useful for creating a new file. The command
touch zzz will create a new file
of zero length, named zzz, assuming
that zzz did not previously exist.
Time-stamping empty files in this way is useful for
storing date information, for example in keeping track of
modification times on a project.
The touch command is
equivalent to : >> newfile
or >> newfile (for ordinary
The at job control command executes
a given set of commands at a specified time. Superficially,
it resembles cron, however,
at is chiefly useful for one-time execution
of a command set.
at 2pm January 15 prompts for a set of
commands to execute at that time. These commands should be
shell-script compatible, since, for all practical
purposes, the user is typing in an executable shell
script a line at a time. Input terminates with a Ctl-D.
Using either the -f option or input
redirection (<), at
reads a command list from a file. This file is an
executable shell script, though it should, of course,
be noninteractive. Particularly clever is including the
run-parts command in
the file to execute a different set of scripts.
bash$ at 2:30 am Friday < at-jobs.listjob 2 at 2000-10-27 02:30
The batch job control command is similar to
at, but it runs a command list when the system
load drops below .8. Like
at, it can read commands from a file with the
Prints a neatly formatted monthly calendar to
stdout. Will do current year or a large
range of past and future years.
This is the shell equivalent of a wait loop. It pauses for a
specified number of seconds, doing nothing. It can be useful
for timing or in processes running in the background,
checking for a specific event every so often (polling),
as in Example 29-6.
sleep 3 # Pauses 3 seconds.
The sleep command defaults to
seconds, but minute, hours, or days may also be specified.
sleep 3 h # Pauses 3 hours!
The watch command may
be a better choice than sleep for running
commands at timed intervals.
Microsleep (the "u"
may be read as the Greek "mu", or micro-
prefix). This is the same as sleep,
above, but "sleeps" in microsecond
intervals. It can be used for fine-grain timing, or for
polling an ongoing process at very frequent intervals.
usleep 30 # Pauses 30 microseconds.
This command is part of the Red Hat initscripts /
The usleep command does not
provide particularly accurate timing, and is therefore
unsuitable for critical timing loops.
The hwclock command accesses or
adjusts the machine's hardware clock. Some
options require root privileges. The
/etc/rc.d/rc.sysinit startup file
uses hwclock to set the system time
from the hardware clock at bootup.