How to Use the date Command in Linux: Step-by-Step Tutorial

It’s time to Learn Linux! The Linux Crash Course series on Learn Linux TV takes you through a valuable Linux-related concept, one video at a time. In this episode, Jay covers the date command.

YouTube player

Basic usage:

date

Without any options, the date command will show the date, set to the time zone of the operating system. Note the time zone is printed within the output in case you’re curious.

Last modification time of a file:

date -r /etc/hosts

Already, that gives us the ability to do a few things. The information that it provides can be queried, or even used within scripts.

For example, consider this example:

echo "$(date): Script has finished."

Within the echo statement, we have a subprocess, running the date command. The output of the date command from within the subshell here will be shown along with the rest of the text, creating the sentence you see.

We can add an instruction like this one to the end of a script, so we can see the date and time the script finished running. Sure, it’s not extremely life-changing, but it’s something.

One thing to know about the date command is that you don’t have to accept the default formatting, you can change it. Consider this example.

date +%m

With this example, we see just the month number. Let’s see another.

date +%H

This time, we see just hour number, im 24-hour format.

The reason this works is because of the fact that the date command accepts formatting options. If you’re only interested in a certain field, or if you prefer the format to be different, you can customize it.

With the previous two examples, we typed +%, and then a field. There’s an entire list of fields you can use to format the output, which I’ll leave on the screen for a few seconds right now.

With that in mind, let’s see an example of customizing the output a bit more:

echo "$(date +%Y-%m-%d): Script has finished."

This time, we improved the previous example, but this time we added some formatting. The date command is used within a subshell just like last time, but we add formatting options to only show us the year, month and day.

But I don’t know about you, but I’d prefer to see the time within the output as well. Let’s see what that looks like:

echo "$(date "+%Y-%m-%d %H:%M:%S") - Script has finished."

It’s getting a bit complicated here, but this is as bad as it gets during this article, and I’m about to break it down anyway.

So overall, we have an echo command. The echo command prints information to standard output.

We can use echo to echo a sentence, like this:

echo "Linux is great!"

But when we include a subshell, we can have the output of the command within the parenthesis executed and its output shown within the rest of the output here.

There’s no difference between the date command within the parenthesis, or without. For example, watch what happens when I extract the date command from the parenthesis in our earlier example, and just run that:

date "+%Y-%m-%d %H:%M:%S"

Another thing you can do is query a specific date. Why might that be useful? One example might be trying to remember what day of the week a specific date falls on.

And for a fun example of this, I’ll show you how you can query which day of the week you were born on, based on your birth date:

date +%A -d "1982-08-16"

I was born on a Monday, as you can see.

I know these examples so far might not seem useful, but keep in mind, there’s going to come a point where you need to take the date into equation, and use that information as part of a command or script.

Another thing you can do with the date command is to set the actual date of your host system. I’m not recommending anyone actually do this, because normally you should rely on network time synchronization to do that for you.

That said, if you’re still curious what the command looks like in case you end up in a situation where time sync isn’t working, here’s how the command would look to actually set the date and time:

date --set="20190601 17:30"

Formatting values:

  • %a – Locale’s abbreviated short weekday name (e.g., Mon)
  • %A – Locale’s abbreviated full weekday name (e.g., Monday)
  • %b – Locale’s abbreviated short month name (e.g., Jan)
  • %B – Locale’s abbreviated long month name (e.g., January)
  • %d – Day of month (e.g., 01)
  • %H – Hour (00..23)
  • %I – Hour (01..12)
  • %j – Day of year (001..366)
  • %m – Month (01..12)
  • %M – Minute (00..59)
  • %S – Second (00..60)
  • %u – Day of week (1..7)
  • %Y – Full year (e.g., 2019)