Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.


Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.


Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.


Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » Commands & Shells, Featured

Playing With Time in Bash Shell

Submitted by on February 24, 2012 – 11:38 pm 5 Comments

The “date” command in Bash shell offers a remarkable array of features that can be very useful in performing many system administration tasks. As you will see below, it is easy to determine date, time, day of week for any interval of time. This can be very useful for system automation tasks with “cron” and “at”.

Current date in a common database-style format (YYYY-MM-DD HH:MM:SS):

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


$ date +'%Y-%m-%d %H:%M:%S'
2012-02-24 19:19:22


Dates of yesterday and tomorrow:

date --date="yesterday" +'%Y-%m-%d'
date --date="tomorrow" +'%Y-%m-%d'


$ date --date="yesterday" +'%Y-%m-%d'

$ date --date="tomorrow" +'%Y-%m-%d'


Date of next Saturday and last Monday:

date --date="next saturday" +'%Y-%m-%d'
date --date="last monday" +'%Y-%m-%d'


$ date --date="next saturday" +'%Y-%m-%d'

$ date --date="last monday" +'%Y-%m-%d'


Date of four day ago and two weeks from now:

date --date="4 days ago" +'%Y-%m-%d'
date --date="2 weeks" +'%Y-%m-%d'


$ date --date="4 days ago" +'%Y-%m-%d'

$ date --date="2 weeks" +'%Y-%m-%d'


Convert current date/time to epoch time:

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


$ date -d "`date +'%Y-%m-%d %H:%M:%S'`" "+%s"


Convert epoch time to date/time:

date -d @${epoch_time} "+%Y-%m-%d %H:%M:%S"


$ epoch_time=$(date -d "`date +'%Y-%m-%d %H:%M:%S'`" "+%s") ; echo ${epoch_time} ; date -d @${epoch_time} "+%Y-%m-%d %H:%M:%S"
2012-02-24 19:16:54


Determine if one date is older or newer than the other

Let’s say you have a bill due on February 21, 2012. Today is February 24. You need to write a script that will tell you if a) the bill is past due; and b) if so, then by how many days

today_epoch=$(date -d "`date +'%Y-%m-%d'`" "+%s")
due_date_epoch=$(date -d "${due_date}" "+%s")

if [ ${today_epoch} -gt ${due_date_epoch} ]
past_due_days=$(echo "scale=0;(${today_epoch}-${due_date_epoch})/60/60/24"|bc -l)
echo "The bill was due ${past_due_days} days ago."


$ if [ ${today_epoch} -gt ${due_date_epoch} ]
> then
> past_due_days=$(echo "scale=0;(${today_epoch}-${due_date_epoch})/60/60/24"|bc -l)
> echo "The bill was due ${past_due_days} days ago."
> fi
The bill was due 3 days ago.
Print Friendly, PDF & Email


  • Wooooody says:

    I want to build a linux distro which is entirely terminal based. I’m considering LFS and I want to include only terminal based applications. But I want it to be as simple as possible. No long commands to remember or anything like that. Anyone want to form a team?

  • Big Banger says:

    Hey I’m at the VERY last level called “Turn the tables on Plankton”. I need help because what do I do once all of the tables are turned? And how do I defeat King Neptune? I’ve been trying to use the Spongebowl move but it’s not working…Help?
    I tried to turn all the tables and stand behind Mr. Krabs but King N. still shoots me and I die so the “reflect” thing doesn’t work. Tips?

  • Mr SoLo DoLo says:

    How did it get that name? Is it American?
    I had a brown derby in mind.

    (this isn’t an advert for the Wimpy bar, by the way, I don’t own them) Any more info?

  • PolishPokeyPimp says:

    Please, anyone please help me solve this problem with some examples.
    I am dead if I don’t finish my assignment.

  • The Beatles says:

    Hi, I have a few questions about learning Linux. Some friends have told me to switch to Linux because it is more stable and more reliable. I’ve played with several automated DVDs like UBUNTU Knoppix and others but so far I don’t see a big difference between them and the usual Windows or Unix based Mac OS. For those familiar with Linux, could you please give me good advice or reasons why I should switch to LINUX? I’m not a programmer or developer but I’d be interesting to know what the opinions of people are on this. Thank you all.

Leave a Reply

%d bloggers like this: