The “date” command on Linux is not a hard one to remember, but mastering its use with the various options and formats available can be a challenge. I’ll show you how to easily print yesterday’s date and give you a few tips to get exactly what you need.
The easiest way to print yesterday’s date using a Linux command is to use the “-d” or “–date” option, specifying the string “yesterday”:
date -d "yesterday"
Let’s get into it right away with a more detailed answer and a few examples you need to know.
Getting yesterday’s date using Linux commands
On Linux, the “date” command has an option to specify the date to display in a human format. For example, we can use “yesterday” or “now” to get a different result.
The option to print a specific date is “–date”, which can be shortened to “-d”. It looks like this for yesterday’s date:
date -d yesterday
If you try it on your system, you’ll see that you get the date from yesterday at the same time, not the current date. You can then combine it with the other options of the date command, for example, to get the date in a specific format:
date -d yesterday +"%Y-%m-%d"
You now have your short answer about how to quickly get yesterday’s date on Linux. But I’ll now explain these two parameters in more detail so you can also use them in other situations.
Linux date command: the -d option explained
The “-d” or “–date” option with the Linux “date” command displays the time described in the parameter, not the current date and time.
Let’s see how to use this option.
How to get tomorrow’s date in the Linux terminal?
To get tomorrow’s date in the Linux terminal, you can also use -d in the same way:
date -d tomorrow
date -d tomorrow +"%Y-%m-%d"
The “-d” option is a smart option that will recognize what you specify in the parameter, even if it’s written in human format.
If you’re new to the Linux command line, this article will give you the most important Linux commands to know, plus a free downloadable cheat sheet to keep handy.
How to get any specific date in the Linux terminal
The –date parameter of the date command on Linux (or -d for short) accepts a wide range of string formats to represent dates and times.
Here’s a list of some possible options:
- Specific date:
date -d yesterday
date -d now
date -d tomorrow
- Relative dates:
date -d "2 days ago"
date -d "3 weeks ago"
date -d "last year"
date -d "next thursday"
date -d "8 days later"
date -d "13:00 tomorrow"
date -d "8am yesterday"
As you can see, you can easily get any date using this option, by just describing it in plain English. Remember to add quotes around your query when it’s longer than one word though. It will avoid interpretation issues. The -d parameter needs to be one string, not more.
I have worked with various programming languages and I can tell you that most don’t have an option as easy to use as this one. Enjoy, especially if you write scripts using it ;-).
Reminder: Remember that all the members of my community get access to this website without ads, exclusive courses and much more. You can become part of this community for as little as $5 per month & get all the benefits immediately.
Formatting dates in the Linux terminal
Now that you know how to get a specific date in a Linux terminal, you might need some additional guidance to format it properly. The default format is pretty long, and largely useless in most cases, especially if you aim to use this date in scripts.
Date formatting essentials
By default, the “date” command prints the date with all the details (in ctime format), including day, month, time, timezone and year.
The default syntax looks like this:
Day Mon DD HH:MM:SS Timezone YYYYFor example:
Wed Aug 24 14:30:00 UTC 2023
But you can change the format to anything you want by specifying it after the “+” symbol at the end of your command line:
The format is described using sequences like:
- %Y: Year (2023)
- %m: Month (01-12)
- %d: Day (01-31)
- %A: Full weekday (Sunday)
%a: Short weekday (Sun)
- %B: Full month name (August)
%b: Short month name (Aug)
All the other characters in your format string will not be interpreted, so you can use “-“, “,” or “/” to separate values.
The easiest way to understand how this format parameter works is to give you some examples:
- As mentioned earlier, the “date” command, without any format specified, prints a long string with all the details (first line).
- If you add this to the command: +”%Y-%m”, you’ll get only the current year and month, separated with a dash:
- In the next example, we add the weekday and remove the month by using a different format:
date +"%A %d, %Y"You probably won’t use this format, but it’s just to understand how it works.
- The last command prints the date in a typical European format (day/month/year):
Remember to check the manual for more details and format options not listed here:
And if you want to expand your skills with Linux commands, you should also check my other articles on the topic:
- 50 Most Useful Linux Commands You Need To Know (Cheat Sheet)
- How to Increase Font Size in the Linux Terminal (+ Bonus tips)
- 3 Best Ways to Access a Website in a Linux Terminal
Overwhelmed with Linux commands?
My e-book, “Master Linux Commands”, is your essential guide to mastering the terminal. Get practical tips, real-world examples, and a bonus cheat sheet to keep by your side.
Grab your copy now.
If you just want to hang out with me and other Linux fans, you can also join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
More details here.
Need help building something with Python?
Python is a great language to get started with programming on any Linux computer.
Learn the essentials, step-by-step, without losing time understanding useless concepts.
Get the e-book now.