The date command has two uses. The most common one is to get the current date and time, optionally in a special format. To get this in the default format, just enter
The system will come back with something like:
Sat Jun 5 16:11:46 CDT 2004
Setting the date
We'll cover setting the date, which is the other main use date has, before going into all of the options the format can have, because setting the date is fairly simple (if not exactly easy to remember). (Run the command man date to see this info on your own system.) The format of the date string is:
# date MMDDhhmm[[CC]YY][.ss]
That's not as nasty as it looks. The stuff inside the square brackets is optional - you can either use it or not. Each pair of letters stand for one part of the date and time:
- MM is month
- DD is day (of the month)
- hh is hour (24 hour clock)
- mm is minute
- YY (if you decide to use it) is the two-digit year.
- CC (if you decide to use it in addition to the year) is the century.
- .ss is seconds (Be sure to include the dot!)
If that's all you specify, then the system assumes you want the current year. So if we entered
# date 06051621
the system would set the date to Jun 5, 2004 (it happens to be 2004 when this was written). Let's say we don't want the current year, we want 2002. Then the command would be
# date 0605162102
How does it know the century? Unix time is kept as the number of seconds from January 1st, 1970, Greenwich Mean Time. (Also known as Zulu time, UCT, UTC and probably a half dozen I've never heard of.) As it has been used in the past, this is a 4-byte number, which means sometime in 2037, it's going to wrap around. Since it runs from 1970 to 2037, there's never any ambiguity about what century a year is in. If it's from 00 to 37, it has to be in the 2000s. If it's 70-99, it has to be in the 1900s. As a matter of fact, the GNU date command (as of Jun 2004) won't let you set it to any date outside that range.
You have to be root to change the date, otherwise you an error message saying:
date: cannot set date: Operation not permitted
Run "man date" to see the full list of possible formats.
The basic date command is fine if you want to know what the date and time are. Where the various formats become useful is scripts. Sometimes you need to squeeze the date into a smaller space; sometimes you only care about the time, not the date; sometimes you want to compare the current date-and-time to something that comes from somewhere else, and the comparison will be a LOT easier if you can get the current date in the same format as the incoming data.
Here's a few examples:
The current date in standard American format:
$ date +%m/%d/%y 06/05/04
The plus sign starts the "format string". Each of the "%<something>" parameters tells it to print that particular thing (month, hour, minute, whatever) in a particular format.
One thing to watch out for: like most Unix commands, date is case sensitive. Watch what happens when we use upper case letters:
$ date +%M/%D/%Y 12/06/05/04/2004
That's minute 12, a slash, the date in American format 06/05/04, another slash, and the 4-digit version of the year, 2004.
Something else to notice: I put slashes in between each parameter, and they showed up in the output as well. You can put anything you like in between the % parameters, and date will reproduce it exactly. If you use a space (or anything else that the shell might get confused about, like a backslash), you'll need to put the parameter string in quotes, like this:
$ date "+%m %d %y" 06 05 04
A good use for the date command in scripts is to create a suffix for a file name, so that the file name will automatically wrap around and be reused after enough time has gone by. For example, let's say I'm making a backup of my system, and I'm not getting real fancy about it. I'm going to back the whole thing up with a simple tar. I'm writing the tarball out to a big drive that has a lot of space, but not an infinite amount. My machine is named bugler, so I'll call the backup that. I have the big backup drive mounted as /bkup. Here's my command:
$ tar -zcf /bkup/bugler.backup.$(date +%a).tgz /
This creates a compressed (the -z option) tarball of my entire system (the "/" at the end of the command), and writes it out to file "/bkup/bugler.backup.Sat.tgz". Because of the date in the filename, each week, the old Sat file will get overwritten, and once I've made my backup for each day of the week, my space won't grow very fast -- I'll keep reusing the space I've already got.
(Side note: The $(<command>) construct is a way to make the output of a command look like it was text that was part of the regular string. The older way to do it is with backticks: "...backup.`date +%a`.tgz". The $() format is MUCH easier to nest (commands within commands). )
To compare the date on your computer with a computer target, establish a passwordless login trust relationship between both computers and issue:
To set your local time on a remote computer target, use
ssh root@target "date $(date +%m%d%H%M.%S)"
All of these relate to system-wide information.
- arch - show CPU hardware details.
- nproc - Show the number of cores.
- uname - Show host and kernel information.
- hostname - Set or show the host's name.
- hostid - Show the host's numeric ID.
- uptime - Show system uptime and recent load
- NTP - the internet's time protocol
- Date input formats for the GNU Coreutils commands.