Bash get yesterday date

The purpose of this tutorial is to show how to get yesterday’s date via the Bash command line on a Linux system. The date command gives us a few ways to accomplish this. Check out the examples below to see how to retrieve yesterday’s date in Bash.

In this tutorial you will learn:

  • How to get yesterday’s date via Bash command line
Examples of retrieving yesterday's date in Bash
Examples of retrieving yesterday’s date in Bash
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software date
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Bash get yesterday date

Open up a command line terminal and try out some of the commands below. Alternatively, incorporate these examples into your Bash script.

The only real difference in these commands is how the date itself is formatted. Feel free to use your preference from the list below, or change the date formatting in single quotes to match your own needs.

  1. Yesterday’s date with day of the week and YYYY-MM-DD format:
    $ date -d "yesterday" '+%A, %Y-%m-%d'
    Sunday, 2022-04-03
  2. Alternatively you can use 1 day ago in your syntax instead of yesterday.
    $ date -d "1 day ago" '+%A, %Y-%m-%d'
    Sunday, 2022-04-03
  3. Day of the week with year, month, day, hour, minutes, and seconds. This will give ultimate precision of what time it was yesterday.
    $ date -d "yesterday" '+%A, %Y-%m-%d-%H-%M-%S'
    Sunday, 2022-04-03-00-26-13
  4. Or you can keep it simple with the default formatting, which outputs the date as day of the week (abbreviation), day of month, month abbreviation, year, hour, minutes, seconds, clock period, and time zone.
    $ date -d "yesterday"
    Sun 03 Apr 2022 12:27:09 AM EDT

Closing Thoughts

In this tutorial, you saw how to get yesterday’s date via the Bash command line on a Linux system. As seen here, the date command can be used along with a large number of different formats, depending on your preference. Adapt the formatting as needed to fit your own needs, and execute man date to see even more formatting options.

Comments and Discussions
Linux Forum