how to get current date and time inside a shell script

When writing shell scripts, many times you would want to get the current time and date, that is the date and time when the script is getting executed. This value can be saved to a variable and used through out the script for various different purposes. You can use them to generate informative file names or even other variables. You can use them to compare it with an another time or date etc.

In this post, we will develop a simple shell script to backup an existing file as an example. We will use the current date and/or time to generate backup file names for the file that is backed up. Just as with the command shell, we will use the date command to get the current date.

using date command

The date command when executed without any arguments will print out the current machine time. The date is a versatile command can also used to set the date and time on your machine, but we will just use it here to get the current value.

$ date

Now to store the date there are two ways you execute a command inside the script. You can use the single back quotes (`) or the standard syntax with dollar signs. Both the syntax are shown in the example below.

#!/bin/bash 
now=$(date)
anow=`date`

Both now and anow variables in the script will now have same value for date. We will just use the first format for the rest of the post. You can print out the value of the variable with the echo command, just as any other variable.

#!/bin/bash 
now=$(date)
echo "current date: $now"

Let’s now create a file name from this variable that we can use to save as the back up. We will also create some simple code that will take a file name as input and then create a backup file. First we will create a file name by just concatenating the provided file name with the current date, separated by a dash (-).

#!/bin/bash 
now=$(date)
filename=$1
backupfilename=$1-$now

echo "current date: $now"
echo "saving to file: $backupfilename"

While this a good enough solution for the problem, the file name that is generated is not very friendly. We will now see how we can format the date string so that we can create file names that are much more desirable and informative. You can use the following syntax to format the date, where F stands for the custom format.

now=$(date +%F)

To create a date in the format of days-month-year, you can do something similar to the following. The dashes (-) in the format is not needed, meaning you can use any separator or no separator at all in the format string.

now=$(date +%d-%m-%Y)

As you can guess, the d stands for days, m for months and Y for years. You can use M for minutes and S for seconds. That means if you want to create a file name which is accurate to the seconds, then you can use a format similar to this:

now=$(date +%d-%m-%Y-%M-%S)

So, a complete bash script would look something like this.

#!/bin/bash 
now=$(date +%d%m%Y-%M:%S)
filename=$1
backupfilename=$1-$now

cp -f $filename $backupfilename
echo "saved $filename to file: $backupfilename"

The above example is very simple one that generates a file name based on the date and time when the shell script is executed and then copies or backup another file to the generated file. You should be able to easily extend this method to create your own shell scripts.