How to display the date and time in Linux Terminal
Surely you are no stranger to the date command in the Bash shell, this is the default shell in most Linux distributions and even macOS. However, in this tutorial, let's take a look at the advanced usage of the date command in the command line and how to use it in the shell script to perform more functions than simply showing the time time.
- Basic Linux commands everyone needs to know
- Basic Shell commands in Linux
- 7 useful commands for Linux network
When running the date command, you will see the current date and time in your country time zone.
date
How to display the date and time in Linux Terminal Picture 1
You can see the default format doesn't look right. Why is the year not printed after the date but left behind after the time zone? Fortunately, there are more than 40 ways to arrange dates so you can adjust the output as desired.
To adjust the date command, type date , space, plus (+) and option (usually letters) with the% symbol. Optional% (date and time in local format) displays the date and time in your localized standardized format. Your location is set according to the geographic information you provided when installing the operating system. It includes currency symbols, page sizes, time zones and some other standards.
date +% c
How to display the date and time in Linux Terminal Picture 2
You can see, five hours have appeared in the position right after the date in the output.
You can change some options at the same time. A string of options is called format string. To see the date name (% A), date of the month (% d) and month name (% B), use the following command:
date +% A% d% B
How to display the date and time in Linux Terminal Picture 3
The command is correct but the format is not very nice, it is difficult to see. To make it easier to read, you can add spaces between options but must be enclosed in quotation marks. Note, the plus sign is outside the quotation.
date + "% A% d% B"
How to display the date and time in Linux Terminal Picture 4
You can add text to a format string like:
date + "Today is:% A% d% B"
How to display the date and time in Linux Terminal Picture 5
Here are some options to help you edit the result format that displays the date and time.
Option to display date and time
- % c : Displays the date and time in the local format, including the time zone.
How to display the date and time in Linux Terminal Picture 6
Optional display date
-
% D : Display the date in month / day / year format.
-
% F : Displays the date according to the year format - month - day.
-
% x : Displays the date according to locale format.
How to display the date and time in Linux Terminal Picture 7
Optional display date
-
% a : Display the abbreviated date name such as Mon, Tue, Wed, etc.
-
% A : Display the full day name like Monday Tuesday, Wednesday, etc.
-
% u : Display numbers instead of days of the week like Monday = 1, Tuesday = 2, Wednesday = 3, etc.
-
% w : Display numbers instead of days of the week starting Sunday like Sunday = 0, Monday = 1, Tuesday = 2, etc.
-
% d : Display the day of the month with the leading zero (01, 02 . 09) if required.
-
% e : Display the day of the month, with the space before each day ('1', '2' . '9') if required. Note, single quotes are not displayed.
-
% j : Display the day of the year with up to two leading zeros if required.
How to display the date and time in Linux Terminal Picture 8
Week display options
-
% U : Display the number of weeks a year, see Sunday as the first day of the week. For example, the third week of the year, the twentieth week of the year, etc.
-
% V : Display the number of ISO weeks a year, see Monday as the first day of a week.
-
% W : Number of weeks per year, considered Monday as the first day of a week.
How to display the date and time in Linux Terminal Picture 9
Month display options
-
% b or % h: Displays the name of the month abbreviated as Jan, Feb, Mar, etc.
-
% B : Displays the full name of a month like January, February, March, etc. .
-
% m : Displays the number of months with the leading zero if required.
How to display the date and time in Linux Terminal Picture 10
Options display year
-
% C : Century display has no year. For example, 2019 is 20 (20th century).
-
% y : Display the year in two-digit form. For example, 2019 is 19.
-
% Y : Displays the year with 4 digits.
How to display the date and time in Linux Terminal Picture 11
Optional display time
-
% T : Display time in hours: Minutes: Seconds.
-
% R : Display hours and minutes in Hour format: Minutes without seconds, use 24-hour clock.
-
% r : Display local time, using 12-hour clock and AM or PM indicator.
-
% X : Display local time, using 24-hour clock.
How to display the date and time in Linux Terminal Picture 12
Option to display hours
-
% H : Display hours in the form of 00, 01, 02 . 23.
-
% I : Display the 12-hour clock usage time in the form of 00, 01, 02 . 12, with 0 standing ahead if required.
How to display the date and time in Linux Terminal Picture 13
Optional display minutes
- % M : Display minutes 01, 02, 03 . 59, with leading 0 if required.
How to display the date and time in Linux Terminal Picture 14
Option to display seconds
-
% s : Displays the number of seconds since 01/01/1970 00:00:00, starting from Unix Epoch.
-
% S : Display the number of seconds 01, 02, 03 . 59, with the number 0 standing ahead if required.
-
% N : Display nano seconds.
How to display the date and time in Linux Terminal Picture 15
Option to display time zone information
-
% z : Displays the time difference between your time zone and UTC.
-
%: z : Displays the time difference between your time zone and UTC with a colon between hours and minutes. Note:: between% and z.
-
% :: z : Displays the time difference between your time zone and UTC with a colon between hours, minutes and seconds. Note, :: between% and z.
-
% Z : Displays the time zone name by letter.
How to display the date and time in Linux Terminal Picture 16
Options related to format
-
% p : Display AM or PM indicator in capital letters.
-
% P : Display indicator am or pm in lowercase letters.
-
% t : Displayed in a tab.
-
% n : Display to a new line.
How to display the date and time in Linux Terminal Picture 17
Other editing options
The following options can be added to% and letters of other options to edit the display format. For example, % -S will remove 0 leading the one-digit seconds value.
- : The hyphen removes the 0-digit leading the one-digit value.
_ : An underscore adds a space before a one-digit number.
0 : Add 0 before the one-digit numbers.
^ : Use capital letters if possible (not all options may apply this way).
# : Use the default format if possible (not all options can apply this way).
How to display the date and time in Linux Terminal Picture 18
To edit the time of a file, you can use the -r option. Here - (hyphen) replaces % and does not require a + sign.
date -r .bashrc
How to display the date and time in Linux Terminal Picture 19
Installing TZ allows you to change the time zone throughout a command.
TZ = GMT date +% c
How to display the date and time in Linux Terminal Picture 20
Use the date command in the script
Activate the Bash shell script to display the date and time as a small matter, create a text file with the following content and save it as gd.sh.
#! / bin / bash
TODAY = $ (date + "Today is% A,% d of% B")
TIMENOW = $ (date + "The local time is% r")
TIME_UK = $ (TZ = BST date + "The time in the UK is% r")
echo $ TODAY
echo $ TIMENOW
echo $ TIME_UK
Type the following command to set the execution permissions for the script.
chmod + x gd.sh
Run the script with this command:
./gd.sh
How to display the date and time in Linux Terminal Picture 21
You can use the date command to create a timestamp. The display script will create a folder with a timestamp as its name. It will then copy the entire text file from the current directory into it. By running this script regularly, you can take a text file image. And then it is possible to build a series of directories with different text file versions.
Note, this is not a strong backup system.
Create a text file with the following content and save it as snapshot.sh.
#! / bin / bash
# obtaining the date and time
date_stamp = $ (date + "% F-% H-% M-% S")
# tạo một thư mục với tên
mkdir "$ date_stamp"
# sao chép tập tin từ thư mục hiện thời vào nó
cp * .txt "$ date_stamp"
# all done, report back and exit
echo "Text file copied to directory:" $ date_stamp
Type the following command to set execution permissions.
chmod + x snapshot.sh
Run the script with the following command:
./snapshot.sh
How to display the date and time in Linux Terminal Picture 22
You will see the newly created folder named after the executable script date. Inside that folder is a copy of the text file.
I wish you all success!
You should read it
- The date command in Windows
- Command at in Windows
- 5 best command line emulation software for Windows 10
- 18 terminal commands on Chromebook you should know
- Forget the GUI, the Command Line is returning
- How to run 2 or more Terminal commands at the same time on Linux
- 14 interesting Linux commands in Terminal
- How to Change the Computer Time and Date Using the Command Prompt
- Notable changes in Windows Terminal ver 0.11
- Common Terminal commands in Raspberry Pi
- How to use the which command in Linux
- How to change date and time manually in macOS
May be interested
How to check the time of shutdown and reboot in Linux
How to run Linux desktop using Windows Subsystem for Linux
How to compare two text files on Linux Terminal
What is the difference between GTK + and Qt?
How to perform multitasking on Linux Terminal with Screen
How to check and manage disk space in Linux