Linux+: Linux Shell 15 – Date and Time

J

Jarret W. Buse

Guest
Linux+: Linux Shell 15 – Date and Time

The date and time settings on a system can be important in situations when the file's date and time stamps can make a difference, even in the system logs. The importance is usually not great for most users, but in a larger network environment it can become critical. Some businesses rely on time sensitive materials which end up playing an important role for date and time settings.

To see the current date and time, enter the command date at a shell prompt. An output example is as follows:

Thu Sep 18 11:11:36 EDT 2014

The output shows the day of the week (Thu), the month (Sep), the day (18), the hour:minute and seconds (11:11:36), the time zone (EDT) and the year (2014).

You can also set the date and time by using the 'date' command in a shell with the option '-s'. The format for the date and time input is 'MMDDhhmmYYYY.ss' as descibed by:

  • MM – two digit month code (01-12)
  • DD – two digit day of month (01-31 depending on month)
  • hh – two digit hour (01-12)
  • mm – two digit minute (00-59)
  • YYYY – four digit year (optional)
  • ss – two digit seconds (00-59) (optional)

So, to use the above sample output as an input to set the date and time, the following would be entered:

date -s 091811112014.36

Let's look at the date command in more detail for all of its abilities.

The options are:

  • -d (--date=string) – displays the date/time specified by the string in the same format as described above.

date --date=”Feb 21 1971”

output: Sun Feb 21 00:00:00 EST 1971

date --date=”Feb 21 1971 08:01”

output: Sun Feb 21 08:01:00 EST 1971

  • -f (--file=datefile) – same as -d, but inputs date list from the specified file.

date --file=/home/jarret/date.txt


NOTE: A separate line within the datefile can specify different date and times which will be output one line at a time.

  • -I[timespec] (--iso-8601[=timespec]) – displays date in ISO 8601 format of 'YYYY-MM-DD. 'timespec' can specify one of the following:
    • auto – prints date only (default)
    • hours – appends hour to date
    • minutes – appends hours and minutes to date
    • seconds – appends hours, minutes and seconds to date
    • ns - appends hours, minutes, seconds and nanoseconds to date

date -Iseconds

output: 2014-09-18T14:32:56-0400

  • -r, (--reference=file) – shows the last modification time of specified file.

date --reference=/home/jarret/date.txt

output: Thu Sep 18 12:03:25 EDT 2014

  • -R, (--rfc-2822) – shows date format in rfc-2822 format (DayOfWeek, DD MMM YYYY HH:mm:ss timezone
date -R

output: Thu, 18 Sep 2014 14:41:41 -0400

  • --rfc-3339=timespec – displays date and time in RFC-3339 format and specifying the precision just as in the -I option. Format is YYYY-MM-DD HH:mm:ss timezone.
date --rfc-3339=seconds

output: 2014-09-18 14:48:04-04:00

  • -s, (--set=string) – sets the date/time specified by the string. The format in the string can be nearly any existing format in this article.
date --set=Thu Sep 19 15:03:22 EDT 2014

  • -u, (--utc, --universal) – shows or sets the Coordinated Universal Time.
NOTE: The time standard is similar in nature to Greenwich Mean Time (GMT).

date -u

output: Thu Sep 18 18:56:16 UTC 2014

  • --help – shows help information for the date command.
  • --version – shows version of the date command

You can manually set the date for your system as described above. Another option is to sync your system time with an Internet Time Server which is commonly called a Network Time Protocol (NTP) server. The best way to accomplish this is by running the following command on your system:

sudo ntpdate time.nist.gov

The output should be something similar to the following:

18 Sep 15:15:17 ntpdate[7352]: adjust time server 128.138.141.172 offset 0.026297 sec

For more information on the NTP Internet Protocol, see the article TCP/IP Protocol: Network Time Protocol (NTP).
 

Attachments

  • slide.jpg
    slide.jpg
    49.4 KB · Views: 126,266

Staff online


Top