6 Practical Examples of Date Command in Linux

HomeLinux6 Practical Examples of Date Command in Linux

6 Practical Examples of Date Command in Linux

date is the default command in Linux to check the current date in your system. Unlike other commands like cal an ncal which each displays the whole calendar in the running month, date displays only the date of the day it is executed. If you type dateon the terminal without any option, you will see the current date followed by the current time.

The use of date is wider than you have probably thought. Especially if you are working with Linux that has no x window system, you will need the command of date so much. date can also be used to figure out the last modification time of a file. If you are a sysadmin and often create a shell script, you can also embed the command of date just like other commands in Linux.

The documentation of date can be read by typing man dateon the terminal. Following are 6 practical examples of how to use the command in Linux.

1. Check the current date of system

The very basic use of date is to check the current date of the system. To do, you don’t any option. If you type dateon the terminal, date will display the current date of the system as well as the current time.

If you want it, you can also set the customized output. For instance, if you want to display date only (without time), you can type the following combination.

date +"%m-%d-%Y"

Or, if you want to display time only, you can type following combination.

date +"%T"

2. Display last modification time of a file

Ever wondered when did the last time a file is modified? You can also use date to the get answer. To check the last modification time of file with date you can use the option of -r followed by the name of the file you want to check its last modification time.

date -r from_pixabay.txt

3. Set system date/time

You can also use the date command to set the system date and time as well. Some distros might require you to login as root to do so. To set the system date, you can use option of -s followed by the string of the date and time. Below is the example.

date -s "2018-03-02 14:53:00"

4. Display date in ISO 8601 format

ISO 8601 is the international standard covering the exchange of date and time-related data. You might will need to use this format if you are writing a shell script involving date. To use the format, you can use the option of –iso-8601 with the syntax as follows:

--iso-8601[=FMT]

You can replace the FMT variables with the valid arguments as follows

  • ‘hours’
  • ‘minutes’
  • ‘date’
  • ‘seconds’
  • ‘ns’

Below is the example.

date --iso-8601=hours

5. Display current time of some other location

Wondering what time is it in London?. You can also use date to get the answer. For instance if you want to check the current time in London, you can type following command on the terminal.

TZ="London" date

To find the value you need to pass to TZ, you can you the tzselect command.

6. Get date corresponding to a day

Sometimes, you check your calendar only to check what date it is on certain day of the week. For instance, the requirement could be to know the date for ‘next Friday’. date is also capable of displaying such information. The option you are to use is -d followed by certain string. A date string may contain items indicating calendar date, time of day, time zone, day of week, relative time, relative date, and numbers. An empty string indicates the beginning of the day.

Following is the example.

date -d "next Friday"

hand-picked weekly content in your inbox

.

related posts

LEAVE A REPLY

Please enter your comment!
Please enter your name here