Linux has many utility tools which let quickly find the answer to your question. But, as they are not so familiar people are unaware of it.
In this article, you will learn one such unfamiliar command that has exceptional benefits.
For example,
You want to take a look at the calendar immediately to plan a vacation. What are a few ways in which you open the calendar?
- Google / Outlook Calendar (As people tend to have it open all the time at their work time and are familiar with it, it’s the go-to calendar for everyone)
- System desktop calendar
- The calendar app on mobile phone
- Jira / Confluence calendar (Show you a calendar if you type “//” in an editor)
But, do you know you can get a calendar just by typing 3 letters? Read this article to know the 3 letter command and to know some fun experiences of using date
command with my team.
Query your terminal as you speak
I remember it was one busy working day. I asked one of my colleagues sitting next to me, “Do you know the date of next week Friday? “. Sadly, I realized that he was wearing headphones and sincerely coding by typing fast, I didn’t like to disturb him further and I was about to raise the question to the next colleague sitting right opposite me.
“November 25”, the colleague whom I thought could not have heard replied.
I was amazed and speechless.
“Oh, Man! I thought you were coding sincerely. How did you say the date? “, I asked him with surprise.
“I was querying the terminal to find the answer to the question you asked me”, he replied showing his terminal window.
The terminal window has the following command,
dd "next week Friday"
“I could not understand this. Can you please explain this to me? “, I asked him.
“Well. Linux by default supports the date command to display today’s date and to set the current date and time in our system. It also provides a way to find a date in the future and past as simply as we speak. date is the command to do all stuff related to date and time”, he replied.
I was more confused about looking at dd the command and I raised him right away.
“But, if date
is the command, What’s dd
? Is that part of date
command”.
He answered gently, ” dd
is not associated with the date
command. That was a shortcut command (I’ll be writing about this in my future blogs) created by me. dd
abbreviates to date --date=""
“
So, the actual command is,
date --date="next week Friday"
I ran the same in my terminal and was amazed to see the output.
This was cool and I thanked him for his extraordinary explanation. On further research, I found that this command was able to answer most of my queries. I feel like I was interacting with an AI system.
Posting few of my queries here.
How to get some info about a date?
Till now, we have been finding a day with our query. But, what if you want to find some info about a date?. On my further research, I found that you can find the day of a date using the same command.
The following command displays the day of August 1, 2015.
date --date="2015-08-01"
date --date="08/01/2015"
How to find information about the list of dates?
“I have a list of dates. I’m in need of a script that displays each date with the day. I started but was stuck in the middle with some issues. Can you please help me with that?”, Krish from my team sought my help politely.
He was clear on his problem. But he has gone wrong with the steps he took to achieve it. On further discussion, I found that he searched the following items sequentially in Google.
- How to open a file in read mode?
- How to read line by line?
- How to parse the date in the file?
- Finally, How to find the day of that date?
In the middle of this discussion, I remember this can be achieved quickly with the super powerful date
command. The fact is date
command does all the above actions, our job is to give the path of the file.
The fact that it can read and parse date given in any format is an added advantage. From the above screenshot, you will be able to understand how simple it is to solve Krish’s problem.
Remembering the quote from Unfiltered: No Shame, No Regrets, Just Me by Lily Collins
Asking for help is never a sign of weakness. It’s one of the bravest things you can do. And it can save your life.
Do you know date command works with file operations?
I hope most people who read my old blog post may remember that with stat
command we can get some info about the file such as its size, created date, last modified date, etc.
Do you know you can find the last modified time of a file using the date
command?
Yes. By adding -r
flag with the date
command and the file path, you can find the last modified time of the file.
date -r install_git.sh
There is another place where this date command is more familiar. It is used to create a file with timestamp. This use case is basically used by system administrators who used to see system and server logs often. I heard that most people from stock trading industry also rely on creating files with timestamps.
Here’s a quick example of creating a file with date and time,
vi file-$(date "+%Y-%m-%d_%I:%M:%S").txt
For first time users, there’s a high possibility that you’ll mess up in understanding the above command.
Let’s split this command to understand better,
vi
– Text editor
file-$(date "+%Y-%m-%d_%I:%M:%S").txt
– Name of the file
Let’s bisect the date
command in the file name further,
date ""
– Print the date in the denoted format
%Y
– Year of today
%m
– Month of today
%d
– Today’s date
%I
– Hours
%M
– Minutes
%S
– Seconds
How to find the present date and time of different time zone?
Udhaya from my team raised this interesting question.
It’s possible with the date
command. But, it’s not so user-friendly. You need to remember the full name of the timezone to get the date and time. But, it works out of the box.
I’m in India and we follow Indian Standard Time (IST). I can use the below command to find the time in New York.
TZ='America/New_York' date
The below screenshot shows you the time comparison across different time zones such as Singapore, America, and India.
3 Letter Command
Are you waiting to know the 3 letter command to view the terminal?
It is the cal
command.
Here’s a bonus tip for you.
You can view the calendar of any year by just adding the year next to cal
command.
Show your interest by commenting on any of our social media posts if you wish to learn more about cal
command.
Note
The date
and cal
comes pre-installed in many Linux distributions. If you encounter date: command not found
error while running date
command, please install coreutils
package by running the following command.
sudo apt install coreutils
Conclusion
In this article, I have described my experience of using the date
command in Linux. I hope you enjoyed reading this article.
For more insightful blogs to get delivered to your inbox, subscribe to my newsletter here.