GNU shell utilities - 10. System context

Go to the first, previous, next, last section, table of contents.

# 10. System context

This section describes commands that print or change system-wide information.

## 10.1 `date`: Print or set system date and time

`date` with no arguments prints the current time and date, in the format of the ``%c'` directive (described below). Synopses:

    date [option]... [+format]
    date [-u|--utc|--universal]
    [ MMDDhhmm[[CC]YY][.ss] ]

If given an argument that starts with a ``+'`, `date` prints the current time and date (or the time and date specified by the `--date` option, see below) in the format defined by that argument, which is the same as in the `strftime` function. Except for directives, which start with ``%'`, characters in the format string are printed unchanged. The directives are described below.

### 10.1.1 Time directives

`date` directives related to times.

``%H'`
  hour (00...23)
``%I'`
  hour (01...12)
``%k'`
  hour ( 0...23)
``%l'`
  hour ( 1...12)
``%M'`
  minute (00...59)
``%p'`
  locale's AM or PM
``%r'`
  time, 12-hour (hh:mm:ss [AP]M)
``%s'`
  seconds since the epoch, i.e., 1 January 1970 00:00:00 UTC (a GNU extension). Note that this value is the number of seconds between the epoch and the current date as defined by the localtime system call. It isn't changed by the ``--date'` option.
``%S'`
  second (00...61)
``%T'`
  time, 24-hour (hh:mm:ss)
``%X'`
  locale's time representation (%H:%M:%S)
``%z'`
  RFC-822 style numeric time zone (e.g., -0600 or +0100), or nothing if no time zone is determinable. This value reflects the current time zone. It isn't changed by the ``--date'` option.
``%Z'`
  time zone (e.g., EDT), or nothing if no timezone is determinable. Note that this value reflects the current time zone. It isn't changed by the ``--date'` option.

### 10.1.2 Date directives

`date` directives related to dates.

``%a'`
  locale's abbreviated weekday name (Sun...Sat)
``%A'`
  locale's full weekday name, variable length (Sunday...Saturday)
``%b'`
  locale's abbreviated month name (Jan...Dec)
``%B'`
  locale's full month name, variable length (January...December)
``%c'`
  locale's date and time (Sat Nov 04 12:02:33 EST 1989)
``%d'`
  day of month (01...31)
``%D'`
  date (mm/dd/yy)
``%h'`
  same as %b
``%j'`
  day of year (001...366)
``%m'`
  month (01...12)
``%U'`
  week number of year with Sunday as first day of week (00...53). Days in a new year preceding the first Sunday are in week zero.
``%V'`
  week number of year with Monday as first day of the week as a decimal (01...53). If the week containing January 1 has four or more days in the new year, then it is considered week 1; otherwise, it is week 53 of the previous year, and the next week is week 1. (See the ISO 8601: 1988 standard.)
``%w'`
  day of week (0...6) with 0 corresponding to Sunday
``%W'`
  week number of year with Monday as first day of week (00...53). Days in a new year preceding the first Monday are in week zero.
``%x'`
  locale's date representation (mm/dd/yy)
``%y'`
  last two digits of year (00...99)
``%Y'`
  year (1970....)

### 10.1.3 Literal directives

`date` directives that produce literal strings.

``%%'`
  a literal %
``%n'`
  a newline
``%t'`
  a horizontal tab

### 10.1.4 Padding

By default, `date` pads numeric fields with zeroes, so that, for example, numeric months are always output as two digits. GNU `date` recognizes the following numeric modifiers between the ``%'` and the directive.

``-'`
  (hyphen) do not pad the field; useful if the output is intended for human consumption.
``_'`
  (underscore) pad the field with spaces; useful if you need a fixed number of characters in the output, but zeroes are too distracting.

These are GNU extensions.

Here is an example illustrating the differences:

    date +%d/%m -d "Feb 1"
    => 01/02
    date +%-d/%-m -d "Feb 1"
    => 1/2
    date +%_d/%_m -d "Feb 1"
    =>  1/ 2

### 10.1.5 Setting the time

If given an argument that does not start with ``+'`, `date` sets the system clock to the time and date specified by that argument (as described below). You must have appropriate privileges to set the system clock. The ``--date'` and ``--set'` options may not be used with such an argument. The ``--universal'` option may be used with such an argument to indicate that the specified time and date are relative to Coordinated Universal Time rather than to the local time zone.

The argument must consist entirely of digits, which have the following meaning:

MM
  month
DD
  day within month
hh
  hour
mm
  minute
CC
  first two digits of year (optional)
YY
  last two digits of year (optional)
ss
  second (optional)

The ``--set'` option also sets the system clock; see the next section.

### 10.1.6 Options for `date`

The program accepts the following options. Also see section 2. Common options.

``-d datestr'`
``--date=datestr'`
  Display the time and date specified in datestr instead of the current time and date. datestr can be in almost any common format. It can contain month names, timezones, ``am'` and ``pm'`, ``yesterday'`, ``ago'`, ``next'`, etc. See section 3. Date input formats.
``-f datefile'`
``--file=datefile'`
  Parse each line in datefile as with ``-d'` and display the resulting time and date. If datefile is ``-'`, use standard input. This is useful when you have many dates to process, because the system overhead of starting up the `date` executable many times can be considerable.
``-I[timespec]'`
``--iso-8601[=timespec]'`
  Display the date using the ISO 8601 format, ``%Y-%m-%d'`. The optional argument timespec specifies the number of additional terms of the time to include. It can be one of the following:
  ``auto'`
    The default behavior: print just the date.
  ``hours'`
    Append the hour of the day to the date.
  ``minutes'`
    Append the hours and minutes.
  ``seconds'`
    Append the hours, minutes, and seconds.
  If showing any time terms, then include the time zone using the format ``%z'`. If ``--utc'` is also specified, use ``%Z'` in place of ``%z'`.
``-R'`
``--rfc-822'`
  Display the time and date using the RFC-822-specified format, ``%a, %_d %b %Y %H:%M:%S %z'`. If ``--utc'` is also specified, use ``GMT'` in place of ``%z'`.
``-r file'`
``--reference=file'`
  Display the time and date reference according to the last modification time of file, instead of the current time and date.
``-s datestr'`
``--set=datestr'`
  Set the time and date to datestr, See ``-d'` above.
``-u'`
``--utc'`
``--universal'`
  Print or set the time and date in Universal Coordinated Time instead of in local (wall clock) time.

### 10.1.7 Examples of `date`

Here are a few examples. Also see the documentation for the ``-d'` option in the previous section.

- To print the date of the day before yesterday:

      date --date='2 days ago'

- To print the date of the day three months and one day hence:

      date --date='3 months 1 day'

- To print the day of year of Christmas in the current year:

      date --date='25 Dec' +%j

- To print the current full month name and the day of the month:

      date '+%B %d'

  But this may not be what you want because for the first nine days of the month, the ``%d'` expands to a zero-padded two-digit field, for example ``date -d 1may '+%B %d''` will print ``May 01'`.
- To print a date without the leading zero for one-digit days of the month, you can use the (GNU extension) `-` modifier to suppress the padding altogether.

      date -d=1may '+%B %-d'

- To print the current date and time in the format required by many non-GNU versions of `date` when setting the system clock:

      date +%m%d%H%M%Y.%S

- To set the system clock forward by two minutes:

      date --set='+2 minutes'

- To print the date in the format specified by RFC-822, use ``date --rfc'`. I just did and saw this:

      Mon, 25 Mar 1996 23:34:17 -0600

- To convert a date string to the number of seconds since the epoch (which is 1970-01-01 00:00:00 UTC), use the ``--date'` option with the ``%s'` format. That can be useful in sorting and/or graphing and/or comparing data by date. The following command outputs the number of the seconds since the epoch for the time one second later than the epoch, but in time zone five hours later (Cambridge, Massachusetts), thus a total of five hours and one second after the epoch:

      date --date='1970-01-01 00:00:01 UTC +5 hours' +%s
      18001

  Suppose you had not specified time zone information in the example above. Then, date would have used your computer's idea of the time zone when interpreting the string. Here's what you would get if you were in Greenwich, England:

      # local time zone used
      date --date='1970-01-01 00:00:01' +%s
      1

- If you're sorting or graphing dated data, your raw date values may be represented as seconds since the epoch. But few people can look at the date ``946684800'` and casually note "Oh, that's the first second of the year 2000."

      date --date='2000-01-01 UTC' +%s
      946684800

  To convert such an unwieldy number of seconds back to a more readable form, use a command like this:

      date -d '1970-01-01 946684800 sec' +"%Y-%m-%d %T %z"
      2000-01-01 00:00:00 +0000

## 10.2 `uname`: Print system information

`uname` prints information about the machine and operating system it is run on. If no options are given, `uname` acts as if the `-s` option were given. Synopsis:

    uname [option]...

If multiple options or `-a` are given, the selected information is printed in this order:

    sysname nodename release osversion machine

The osversion, at least, may well be multiple words. For example:

    uname -a
    => Linux hayley 1.0.4 #3 Thu May 12 18:06:34 1994 i486

The program accepts the following options. Also see section 2. Common options.

``-a'`
``--all'`
  Print all of the below information.
``-m'`
``--machine'`
  Print the machine (hardware) type.
``-n'`
``--nodename'`
  Print the machine's network node hostname.
``-p'`
``--processor'`
  Print the machine's processor type
``-r'`
``--release'`
  Print the operating system release.
``-s'`
``--sysname'`
  Print the operating system name.
``-v'`
  Print the operating system version.

## 10.3 `hostname`: Print or set system name

With no arguments, `hostname` prints the name of the current host system. With one argument, it sets the current host name to the specified string. You must have appropriate privileges to set the host name. Synopsis:

    hostname [name]

The only options are ``--help'` and ``--version'`. See section 2. Common options.

Go to the first, previous, next, last section, table of contents.