The Linux ‘date’ command

via Simple Help by Sukrit Dhandhania on 12/18/08

Linux

A command line tool that I find use for very frequently on Linux is the “date” command. It’s a pretty simple command that returns you the date and time when you call it without any options. But when you begin to use some of the options it provides you with this tool can make your life much easier and fun. Let’s take a closer look at the command itself and some of its options and the cool tricks that we can make it do.

The first thing to do is to get the current date and time:

# date
Sun Dec 14 11:33:55 IST 2008

This is the simplest use of this command. Now suppose you wanted to just get the date and nothing more:

# date +”%d”
14

If you want the date, complete with date, month, and year:

# date +”%d%m%y”
141208

To get the day of the week along with the rest of the date:

# date +”%a%d%m%y”
Sun141208

These are a few of the many possibilities that the “date” command offers you. Check out “date –help for options”. Some interesting ones are:

%D   date (mm/dd/yy)
%d   day of month (01..31)
%m   month (01..12)
%y   last two digits of year (00..99)
%a   locale’s abbreviated weekday name (Sun..Sat)
%A   locale’s full weekday name, variable length (Sunday..Saturday)
%b   locale’s abbreviated month name (Jan..Dec)
%B   locale’s full month name, variable length (January..December)
%H   hour (00..23)
%I   hour (01..12)
%Y   year (1970…)

You can also do some fancy formatting. If you want to add a hyphen or a back-slash in between the different parts of the date:

# date +”%d-%m-%Y”
14-12-2008

# date +”%d/%m/%Y”
14/12/2008

You can also use spaces and commas. Here’s a pretty fancy example:

# date +”%A,%B %d %Y”
Sunday,December 14 2008

Say you’re writing a shell script to back up the logs form you server. You want the backup script to get the logs for the day before and back them up. Here’s how you can get the previous day’s date:

# date –date=”yesterday”
Sat Dec 13 12:04:03 IST 2008

Similarly, you can also get tomorrow’s date:

# date –date=”tomorrow”
Mon Dec 15 12:04:39 IST 2008

You can get last or next month’s date:

# date –date=”last month”
Fri Nov 14 12:06:23 IST 2008

# date –date=”next month”
Wed Jan 14 12:06:25 IST 2009

Pretty cool stuff, eh! You’re just getting started. You can customize the date you want more precisely than by day and week, and get the date five days ago or seven days from now:

# date –date=”5 days ago”
Tue Dec  9 12:08:26 IST 2008

# date –date=”7 days”
Fri Dec 21 12:09:23 IST 2008

You can even ask it more difficult questions such as:

# date –date=’next Saturday’
Sat Dec 20 00:00:00 IST 2008

Or something totally in the future:

# date –date=’2 years 3 months 4 day’
Fri Mar 18 12:12:16 IST 2011

It’s almost as if “date” speaks English. I keep discovering new tricks. So play around with it and let me know if you find some new cool possibilities that I might not have discovered yet.

---
Related Articles at Simple Help:


The Linux ‘date’ command - Simple Help

by Ben Pike