Linux get current time in terminal

HowTo: Get Current Time In Shell Script

H ow do I get the current server time in shell script on Linux or Unix-like operating systems? How do I store the current time in the shell variable and use in my scripts?

You can use the date command to display or set the current date and time. You need to use the date FORMAT syntax to controls the output of the date command. The %T format sequence interpreted by the date command to

Tutorial details
Difficulty level Easy
Root privileges No
Requirements None
Est. reading time Less than a minute

display the current time. The syntax is:

date +%FORMAT
date +»%FORMAT»
var=$(date +»%FORMAT»)

Example: Show current time

Open a terminal and type the following command:

To store time to a shell variable called now, enter:

Example: 12 hour clock time

Pass the %r format to the date command:

To remove AM or PM from the output use, type:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Sample shell script

Run it as follows:

Animated gif 01: demo.sh script in action. Please note time is updating as screen refresh due to the clear command.

References:
  • See date command man page for more information.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

ow=$(date +”%T”)
should be
now=$(date +”%T”)

Thanks for the heads up!

i need a shell script to implement backround process that will print current time (**not in command prompt)

if i want to run my shell script for only five minutes but that should be in loop.how can i do this??

Could you please give some examples ASAP?

put your shell script under crontab configure it as per your requirement

Hi,
I need shell script which need to check one folder every day and list the files and send a mail to mail box..

ex: I have below folder
#cd /image/abc/20140921/
#/image/abc/20140921/ ls -ltr *gif* | wc

but here last date folder name will change as each day date..so script has to check every days files and send mail..

Источник

Linux: Bash Get Time

[donotprint]

Tutorial details
Difficulty level Easy
Root privileges No
Requirements None
Est. reading time 1m

[/donotprint]

Syntax

To display current time, enter:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

You can only display time, enter:
$ date +»%T»
Sample outputs:

The following command use your local standards to display date and time:
$ date +»%c»
Sample outputs:

To display the date and time in a specified format, enter:
date +»%r %a %d %h %y (Julian Date: %j)»
Sample outputs:

A list of date command field descriptors

Finally, you can use TZ variable as follows. For example, show the time on the west coast of the US:
$ TZ=’America/Los_Angeles’ date
Sat Sep 19 12:26:57 PDT 2015

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

And to set the time trought BASH, use this:

where
MM = Month (2 digits)
DD = Day (2 digits)
HH= Hours (24 hours format)
mm= Minutes (2 digits)
YYYY= Year (4 digits)

Example:
date 102712232010
Set the time to Wed Oct 27 12:23:00 CDT 2010

It’s not
$ date +”%T”

@soubhik — Your comment is inaccurate. Read more on the man page for /bin/sh or any shell.

Those commands that you posted, in this case, are equivalent. Read the section on quoting.

Setting a multi time zone clock on a corner of my desktop. Using NerdTool shell scripting. I have tried making use of the different time zone example on this page. Problem is Unless it is immediate close to local I get a default of several hours – not at all what I expected to get. Can anyone please tell me what I am doing wrong? Thanks.

TZ=’America/Hawaii’ date “+%H:%M %p”
TZ=’America/Los_Angeles’ date “+%H:%M %p”
TZ=’America/Denver’ date “+%H:%M %p”
TZ=’America/Dallas’ date “+%H:%M %p”
TZ=’America/Miami’ date “+%H:%M %p”

PS – I am not an expert in this, just trying examples out and doing my best to modify as the index reveals options. The example uses the dollar sign to precede the code but doing that kills the script in NerdTool.

Источник

Display Date And Time In Linux

H ow do I display date and time in Linux using the command line and GUI options?

To display date and time under Linux operating system using command prompt use the date command. It can also display the current time / date in the given FORMAT. We can set the system date and time as root user too.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux with the terminal application
Est. reading time 3 minutes

Syntax

Open a terminal and type the following command:
date

You can format the date as follows in dd-mm-yy format:
date +»%d-%m-%y»

Simply display the current time:
date «+%T»

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

To print the date of the day before yesterday, run:
date —date=’2 days ago’
Want to see the day of year of Christmas in the current year? Try:
date —date=’25 Dec’ +%j
Display the current full month name and the day of the month:
date ‘+%B %d’

Display date and time in Linux using the timedatectl

Most modern Linux distro with systemd use the timedatectl command. It is used to query and change the system clock and its settings, and enable or disable time synchronization services. All you have to do is type the following command:
$ timedatectl

TZ environment variable

The TZ environment tells Linux what time zone you are in. Many times when you want to change your time zone temporarily. For example, you might want to print the current date and time in “America/Los_Angeles” timezone even though you are in “Europe/London”. So we can set TZ, give a command like as follows when using TCSH/CSH:
setenv TZ timezone
For BASH/KSH/SH (see export command:
TZ=timezone; export TZ
Another option:
TZ=»America/Los_Angeles» date
TZ=»Asia/Tokyo» date
Use the following command to print a list of all timezones:
timedatectl list-timezones
timedatectl list-timezones | more
## filter out data using the grep command/egrep command ##
timedatectl list-timezones | grep -i Hong_kong
timedatectl list-timezones | grep -i paris
timedatectl list-timezones | grep -E -i ‘paris|london|kolkata’

The TZ Environment Variable on Linux

GUI Tool: Time Administration

The Time Administration Tool allows you to set the time, date and timezone of your system, as well as setting any time server to synchronize your local time server. Type the following command to start time admin tool:
sudo time-admin
## OR ##
time-admin

Fig.01: Linux Date and Time Administration Tool

How to change date and time settings with Gnome based Linux desktop

First, you need to Settings in Activities and then click Details in the sidebar. Make sure you click Date & Time in the sidebar to open the panel:

Conclusion

In this quick tutorial, you learned about the date command that you can use to see or change the date/time under Linux operating systems. We further explained how to use the GUI tools too. The date command has many more options. See man page by typing the following man command:
$ man date

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

Читайте также:  Что лучше debian или windows
Оцените статью