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
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – 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.
- Yesterday’s date with day of the week and YYYY-MM-DD format:
$ date -d "yesterday" '+%A, %Y-%m-%d' Sunday, 2022-04-03
- Alternatively you can use
1 day agoin your syntax instead of
$ date -d "1 day ago" '+%A, %Y-%m-%d' Sunday, 2022-04-03
- 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
- 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
In this tutorial, you saw how to get yesterday’s date via the Bash command line on a Linux system. As seen here, the
datecommand 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 dateto see even more formatting options.