linux command to print yesterday's date

Linux Commands: How to Display Yesterday’s Date (and more)

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.

Linux doesn’t have to be intimidating. With my e-book, Master Linux Commands, you’ll uncover the secrets of the terminal in a fun, step-by-step journey. From basics to scripts, get ready to level up your Linux skills. Oh, and did I mention the handy cheat sheet you get as a bonus?

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 --date=yesterday
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"

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

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.

Join Our Community!

Connect, learn, and grow with other Raspberry Pi enthusiasts. Support RaspberryTips and enjoy an ad-free reading experience. Get exclusive monthly video tutorials and many other benefits.

Learn more

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.

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

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"
  • Time-based:
    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 ;-).

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

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 YYYY
For 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:
date +"<FORMAT>"

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.

Examples

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:
    date +"%Y-%m"
  • 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):
    date +"%d/%m/%Y"

Remember to check the manual for more details and format options not listed here:
man date

And if you want to expand your skills with Linux commands, you should also check my other articles on the topic:

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

🛠 This tutorial doesn't work anymore? Report the issue here, so that I can update it!

If you are looking for exclusive tutorials, I post a new course each month, available for premium members only. Join the community to get access to all of them right now!

Additional Resources

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.

VIP Community
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.

Similar Posts