How to Display the Date and Time in the Linux Terminal (and Use It In Bash Scripts)

Bash shell on the Unity desktop conceptFatmawati Achmad Zaenuri / Shutterstock.com

The date command is in the Bash shell, which is the default shell for most Linux distributions and even MacOS. This tutorial explains how to control the date on the command line and how to use it in shell scripts to do more than just print the time.

Run the date command to see this information. It prints the current date and time of your time zone:

date

Exit date order

The default formatting seems a little awkward. Why is the printed year after the month and day not shown instead of marked at the end, behind the time zone? Do not worry: if you have control over the output format you want, Date provides it straightforwardly. There are more than 40 options that you can transmit to date to ask him to accurately format his output, as you wish.

To use one of the options, type date, a space, a plus sign, and the option including the sign of the primary percentage. The% c option (data and time in locale format) causes the date and time to be printed in the standard format associated with your locale. Your locale is defined by the geographic and cultural information that you provided during the installation of your operating system. The locale controls items such as currency symbol, paper sizes, time zone, and other cultural norms.

date +% c

Exit date command with option c

The year now appears in a more natural position in the exit.

You can pass several options at once. A sequence of options is called a format string. To see the name of the day (% A), the day of the month (% d) and the name of the month (% B), use this command:

date +% A% d% B

Output of the date command with options A d B

It worked, but it's ugly. No problem, we can include spaces as long as we wrap the entire format string in quotation marks. Note that the + comes out of the quotation marks.

date + "% A% d% B"

Output date command with option A d B with spaces

You can add text to the format string, like this:

date + "Today is:% A% d% B"

Output of the data command with the text added by the user

Scroll up and down through the date manual page looking for the option you want is fast becoming tiring. We have divided the options into groups to help you find your way more easily.

Options to display the date and time

% c: Prints the date and time in the format of your country, including the time zone.

Exit date order

Options to display the date

%RE: Prints the date in mm / dd / yy format.
F%: Prints the date in yyyy-mm-dd format.
X%: Prints the date in the format of your area.

Exit the date command with options D F x

Options to display the day

%a: Displays the name of the day, abbreviated as Monday, Tuesday, Wednesday, etc.
%A: Print the full name of the day, Monday Tuesday, Wednesday, etc.
% u: Print the number of the day of the week, where Monday = 1, Tuesday = 2, Wednesday = 3, etc.
% w: Print the number of the day of the week, where Sunday = 0, Monday = 1, Tuesday = 2, etc.
%re: Prints the day of the month, with a zero at the beginning (01, 02 … 09) if necessary.
% e: Prints the day of the month, with a space ("1", "2" … "9") if necessary. Note that single quotes do not print.
% j: Prints the day of the year, with up to two zeros, if necessary.

Output of the date command with the options A u w d e j

Options to view the week

% U: Prints the week number of the year by considering Sunday as the first day of the week. For example, the third week of the year, the twentieth week of the year, and so on.
% V: Prints the ISO week number of the year, Monday being the first day of the week.
% W: Week number of the year, considering Monday as the first day of the week.

Output of the date command with U V W options

Options to view the month

% b or % h: Displays the name of the abbreviated month in January, February, March, etc.
% B: displays the full name of the month, January, February, March, etc.
% m: Print the number of the month with a zero if necessary 01, 02, 03 … 12.

Output of the date command with options b h B m

Options to view the year

% C: Print the century without the year. By 2019, he would print 20.
% y: Prints the year as two digits. in 2019, he will print 19.
% Y: Prints the year as four digits.

Exit the date command with C y Y options

Options to display the time

% T: Prints the time in the form HH: MM: SS.
% R: Prints the hour and minutes as HH: MM without seconds, using the 24-hour clock.
% r: Prints the time according to your locale, using the 12-hour clock and an am or pm indicator.
X%: Prints the time according to your locale, using the 24-hour clock. Allegedly. Note that during the test, this option behaves exactly like% r, as shown below. On a Linux machine configured for the UK locale and set to GMT, it printed the time, using the 24-hour clock without AM or PM indicator, as expected.

Output of the date command with the options T R and X

Options to display the time

% H: Prints the time 00, 01, 02 … 23.
%I: Prints the time using the 12-hour clock, 00, 01, 02 … 12, with a leading zero if necessary.

Output of the date command with options H I

Options to display minutes

% M: prints the minutes, 01, 02, 03 … 59, with a left zero if necessary.

Output of the date command with options M

Options to display seconds

% s: Prints the number of seconds since 1970-01-01 00:00:00, the beginning of the Unix era.
% S: Prints the seconds, 01, 02, 03 … 59, with a left zero if necessary.
% NOT: Prints the nanoseconds.

Output of the date command with the options S S N

Options to view time zone information

% z: Prints the time difference between your time zone and your UTC time.
%: z: Prints the time difference between your time zone and your UTC time, with a: between hours and minutes. Note the: between the sign% and z.
% :: z: Prints the time difference between your time zone and your UTC time, with a: between hours, minutes, and seconds. Note the :: between the sign% and z.
% Z: Prints the name of the alphabetic time zone.

Output of the date command with time zone options

Options related to formatting

% p: Prints the AM or PM indicator in upper case.
% P: Prints the am or pm indicator in lowercase. Note the whim with these two options. A lowercase p gives an uppercase output, an uppercase p gives a lowercase output.
% t: Prints a tab.
% not: Prints a new line.

Date control output with AM PM indicator and formatting options

Options to change other options

These modifiers can be inserted between the% and the option letter of other options to change their display. For example,% -S removes the initial zero for single digit values.

: A single hyphen prevents zero-valued single-digit values ​​from filling.
_: A single underscore adds leading spaces for single-digit values.
0: Provides leading zeros for single-digit values.
^: Use capital letters, if possible (all options do not respect this modifier).
#: If possible, use the opposite of the default case of the option (all options do not respect this modifier).

Output of the date command with formatting options

Two other tips

To get the last time to edit a file, use the -r (reference) option. Note that this uses a – (hyphen) instead of a% sign and does not require a + sign. Try this command in your personal folder:

date -r .bashrc

Output of the date command with the file modification time option

The TZ setting allows you to change your time zone for the duration of a single command.

TZ = date GMT +% c

Output of the date command for a different time zone

Using the date in scripts

Enabling a Bash shell script to print the time and date is simple. Create a text file with the following content and save it as gd.sh.

#! / bin / bash

TODAY & # 39; HUI = $ (date + "Today is% A,% d% B")
TIMENOW = $ (date + "local time is% r")
TIME_UK = $ (TZ = date BST + "UK time is% r")

echo $ TODAY & # 39; HUI
echo $ TIMENOW
echo $ TIME_UK

Type the following command to set the permissions to run and make the script executable.

chmod + x gd.sh

Run the script with this command:

./gd.sh

Gd.sh script output

We can use the date command to provide a timestamp. The presented script will create a directory with the timestamp as the name. It will then copy all the text files from the current folder into this folder. By running this script regularly, we can take a snapshot of our text files. Over time, we will build a series of folders containing different versions of our text files.

Note that this is not a robust backup system, but only for illustrative purposes.

Create a text file with the following content and save it as snapshot.sh.

#! / bin / bash

# get the date and time
date_stamp = $ (date + "% F-% H-% M-% S")

# create a directory with this name
mkdir "$ date_stamp"

# copy the files from the current folder into this one
cp * .txt "$ date_stamp"

# everything is done, report and exit
echo "Text files copied to the directory:" $ date_stamp

Type the following command to set the permissions to run and make the script executable.

chmod + x snapshot.sh

Run the script with this command:

./snapshot.sh

Effect of running the snapshot.sh script

You will see that a directory has been created. His name is the date and time when the script was run. In this directory are copies of the text files.

With a little thought and creativity, even the modest date control can be used productively.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.