📅 June 11, 2024
Who needs a fancy GUI calendar when you can get the date and time from the command line like a true tech guru?
There are times when you will need to get the date and possibly the time using the command line or from within a shell script. Maybe you want to display the time in binary?
How is this achieved? Bash makes it easy…okay, easy from the command line. What a great way to impress your friends and freak out those afraid of a command prompt!
What Time is It?
The next time somebody asks you this question, boot your Linux computer (you might need to travel home for a desktop system), open a terminal, and at the Bash command prompt, enter this:
date
Output:
Tue Jun 11 15:42:06 BST 2024
Presto! You now have the date and time. By default, date returns the local system time. Let’s analyze the components.
UTC
Maybe you wish to get the Coordinated Universal Time (UTC)? This, too, is easy with the -u or –utc or –universal option.
date -u date --utc date --universal
Output:
Tue Jun 11 03:32:13 PM UTC 2024
Customizing the Date Output
“That looks too geeky. Can I customize the output?”
Yes, and date provides so many possibilities that it is best to consult man date for details. We can use format specifiers (similar to those found in the C programming language) or interpreted sequences (according to date’s man page) to specify what should be displayed in the output.
date +%Y date '+%Y' date "+%Y"
Output:
2024
All three of the above will display the current four-digit year. Quotes are optional for this simple example, but the plus character (+)Â must begin the sequence.
date +%c
There are many ways to format the output depending upon which sequence is used and in what order, so experiment for yourself with different combinations. However, quoting becomes necessary when combining sequences with spacing and commas.
date +%A %B %d, %YÂ # This will not work!
date '+%A %B %d, %Y' # Quotes are required
Output:
Tuesday June 11, 2024
See what happened? We can add spacing and commas to help format the date we want to see. We can even add words.
date '+Month: %Y Day: %d and the year: %Y'
Output:
Month: 2024 Day: 11 and the year: 2024
Maybe you need to separate the date and time components for a CSV file?
date '+%Y,%m,%d,%H,%M,%S'
Output:
2024,06,11,15,42,06
(Year,month,day,hour,minute,second)
With a system like this, date formatting is flexible.
date +%D 06/11/24Â (mm/dd/yy) date +%F 2024-06-11 (yyyy-mm-dd) date +%Z BST (time zone only) date +%u Day of week number. 1 = Monday. 2 = Tuesday date +%TÂ 03:32:13 (Current time in HH:MM:SS format) date +%U 23 (Week number of year. Sunday first.) date +%V 24 (Week number of year. Monday first.) date +%s 1718121418 (UNIX epoch) date '+%% %n' A literal % and newline
The possibilities are endless, but this should be enough to help demonstrate how to use date. To help make it easier to generate a date/time format string, try this online generator.
Generate a Filename with the Current Epoch
As an example of how a custom time can be used, suppose we need to create a file from a script and that file requires the current UNIX epoch in the filename. How would we achieve this?
date +%s returns the UNIX epoch, so we can use this with command substitution.
echo 'My Little Penguin' > "logfile-$(date +%s).log"
This redirects the the example text My Little Penguin into a new text file named logfile-1718122042.log at the time of its creation. The part $(date +%s) will generate an epoch representing the current date and time. It is a way to ensure that we have a unique filename.
The UNIX epoch is the number of seconds elapsed since January 1, 1970. Used like this, we have a one-second granularity. This can be a problem if we need to run this multiple times within the same second. To add more precision, we can concatenate the nanoseconds using %N to add more randomness to the generated filename.
echo 'My Little Penguin' > "logfile-$(date +%s-%N).log"
Output (something like this):
logfile-1718122285-743601556.log
Now, a new file will be created each time this runs instead of overwriting the same file within the same second as before. Of course, if we somehow generate an identical epoch and an identical nanosecond, then the file will be overwritten, but this is unlikely. If you need that level of uniqueness, then there are alternative ways of generating random filenames.
If you need spaces and commas within the date format string, then encapsulate the format string within quotes.
echo 'My Little Penguin' > "logfile-$(date '+%s %N').log"
But it is not good practice to put spaces and commas in filenames anyway (unless you like to type extra escape characters from the command line).
A Binary Clock
date is probably the most direct way to obtain a customized date and time for use in scripts (and to impress others), but what if you want to show off your l33t G33k skillz? binclock is a start.
sudo apt install binclock
This program will display the current time in binary.
binclock
Output:
This is the default binclock output. It might help at first to use some options. (binclock has a man page for more details.)
binclock -n
Still figuring it out? This might help:
binclock -n && binclock -t
Make sure to use run binclock twice using && so we see the same time displayed for both.
binclock only shows time, not date. With binclock -t, we see an HOUR : MINUTE : SECOND format.
We can convert binary to decimal to find the current time. binclock -t might be easier to understand, but how about binclock without options? This output is a little different and does not lend to a binary conversion as with the -t option. Have you figured it out yet?
Here is one more clue.
binclock -l
Watch the 0 and 1 changes that occur each second.
Did you notice the type of change occurring? We read this clock vertically as binary coded decimal (BCD).
The clock is arranged vertically in the HH:MM:SS from left to right, with each two columns representing the current hour, minute, and second. The 1’s place is at the bottom.
A Terminal Binary Clock
Let’s run binclock in its own window by running this command:
gnome-terminal --hide-menubar --title=Time --geometry 16x5+100+100 -- binclock -l
This opens binclock in its own gnome-terminal positioned at 100×100 pixels from the top left of the desktop. Press CTRL+C when the window has focus to quit and exit.
What About the time Command?
There is a modifier called time, but it has nothing to do with time and date. It is used to report how long a command or process took to run.
If you run time by itself, you will see this:
This is because we use time before the command we want to measure.
time sleep 3
sleep is just an example. If you place time before any command, such as rsync or a lengthy backup script, then you can measure how long it took to complete.
Just note that time has nothing to do with the time of day.
Conclusion
Now, when somebody asks you “Excuse me, but do you have any Grey…” —you know what to do in the most terminal-like, command prompt way possible.
Enjoy!