The date command
Summary
The date command sets and shows the system date and time.
Basic command structure
For all descriptions below, the dollar sign indicates that BASH command prompt.
$ date [options] [+format]
Note: If no option or format is given then the ‘date’ command will print the current locale’s date and time.
Possible Flags
-d
The -d flag displays the given date/time string. The date displayed can be given as a word: today, yesterday, etc. or as a phrase: “Jan 12 2012”
-r
The -r flag displays the time when a specified file was last modified. Use the filename or path/to/filename.
-s
The -s flag sets the system date and time. That being so, it also requires superuser privileges. Use “YYYY-MM-dd HH:MM:SS”
-u
The -u flag shows the date/time in UTC instead of the local timezone.
Output options
The date command has many different kinds of outputs as described below:
-
Standard output with no flags or +formats, which displays the local date and time of the system.
-
Output with
-rgives the last modification date of a file within the directory. -
Output with
[+format]adjusts what part of the time or date is displayed. For example+%Youtputs the year (YYYY),+%moutputs the month (MM), and%d(dd) outputs the day.
Examples
- If you wanted to check the last modification date of a file, but in the MM/DD/YY format run $
date -r path/to/filename +%D. - If you want to see the time as the 12-hour format, as opposed to the standard use of 24 hours, run $
date +"%I %p".
Go back to the main list of commands