Linux date command is used to display the system date and time. We can also print the past date, future date or set the date manually. By default, the date command prints the current date of the Linux system. Here I have listed some of the date command options which one can use to have various outputs.
Linux date command basic usage
Without using any other format specifier, this command prints the current date and time of the system. Let’s have a look on syntax.
date
1. Linux date -u usage
This option for the Linux date command lets you display the time in other time zone either in GMT(Greenwich Mean Time) or UTC (Universal Time Coordinated) time zone.
date -u
2. Linux date –date or -d usage
This option lets you display the date and time in any format. This would not affect the system’s date and time. It will remain the same.
$date --date="STRING"
3. Linux date usage to print past/future dates
This option lets you display the past days as well as future dates.
To print Past Dates
Date and time 2 years ago:
$date --date="2 years ago"
Date and time 5 seconds ago:
$date --date="5 sec ago"
Date and time of the previous day:
$date --date="yesterday"
Date and time of 10 days ago:
$date --date="10 days ago"
To print future dates
Date and time of the upcoming day: It can be done using two commands either by the next day or tomorrow.
$date --date="tomorrow"
$date --date="next day"
Date and time after two days:
$date --date="2 day"
Date and time after one year:
$date --date="1 year"
4. Saving date command output to a file
Using this command, one could save the date in the file.
$date >f.txt
$cat f.txt
5. Linux date with -r usage
This Linux date command option is used to display the last modified timestamp of a file.
$date -r file.txt
6. Format Specifiers
Specifiers mainly defines the type of data to be printed. In the Linux date command, there are many specifiers that can print date, month, year and many more separately.
- %D: Display the date as mm/dd/yy
- %d : Display the day of the month.
- %a : Display the abbreviated name for weekdays
- %A: Displays full days
- %h : Display abbreviated month name
- %b : Displays abbreviated month name
- %B : Displays full month name
- %m : Displays the month of year.
- %y : Display last two digits of the year
- %Y : Display four-digit year
- %T : Display the time in 24 hour format as HH:MM:SS
- %H : Display the hour.
These all the commands that can be used with the date command on the Linux operating system.
Conclusion
We covered the functioning of the date command in Linux in this tutorial. We hope you’ve understood the command better and can now work with the command in a much efficient manner. Feel free to comment if you have any questions. Feel free to use the man command to learn more about the date command.