linux command to print yesterday's date

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

If you click our links and make a purchase, we may earn an affiliate commission. Learn 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.

If you need help with Linux, I’ve got something that can help you right away!
Download my free Linux commands cheat sheet – it’s a quick reference guide with all the essential commands you’ll need to get things done on your system. Click here to get it for free!

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"

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.

Recommended next step
Master Linux Commands

If you’re tired of copy/pasting commands without really understanding them, this book will help you finally feel confident in the Linux terminal.

Improve your Linux skills

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"

Something not working as expected?
You can get answers from real experts in minutes.
Get help with your setup

The “-d” option is a smart option that will recognize what you specify in the parameter, even if it’s written in human format.

Tip: Command lines can be a pain to memorize. I put the essential Linux commands on a printable cheat sheet so you don't have to keep googling them. You can grab the PDF here if you want to save some time.

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


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

Still stuck after following this guide? Drop your question in the RaspberryTips Community — real Pi users answer fast. Post your question here.

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:

Not getting the same result?

Even when you follow every step, small differences in OS version, hardware or config can change the outcome. Instead of wasting time guessing, get help from people who have already fixed the same kind of issue.

Ask your question now →
🔒 No risk. Cancel anytime.
  • Get help on your exact issue
  • Access step-by-step videos for tricky setups
  • Browse the website without ads

Similar Posts

Leave a Reply

Community members only
Comments are reserved for RaspberryTips Community members. Join the community to ask questions, get help, and interact with other Raspberry Pi users.

Join the community  or  log in