When using Linux distributions we have at our disposal hundreds of commands that help us obtain accurate information about services, processes , system components and much more, all of this focused on proper administrative and support management..
One of these commands is the date which is used to obtain the current date and time of the selected system:
But more than this, this command gives us the possibility to see or calculate a date in a format that we specify or the root user can use it to configure the operating system clock if necessary.
TechnoWikis will make a study on this command and its forms of use in Linux..
1. Basic use date command in Linux
We can see in the image above that when only the date command is executed without any additional parameters, it will display the current date and time of the system, including the day of the week, month, time, time zone and year.
If we wish to display the results on a specific date, it will be necessary to use the parameter -de entering the respective date:
date -d "2018-01-15"
This date command has multiple options at the display format level. For example, it will be possible to indicate the date with the format string by preceding it with a plus sign as follows:
date + "Week number:% V Year:% y"
This will result in the current week number of the current year since% V is the format option to display the current week number, and% and represents the last two digits of the year.
2. Format options in date Linux
Some of the most common formatting options when using date are:
Refers to the abbreviated name of the week
Indicate the full name of the week
Abbreviated Name of the Month
Display local date and time
Display the day of the month
It shows us the day of the current year
Indicate the elapsed minutes
It allows us to see the nanoseconds elapsed
Display time in 24-hour format
Show the number of the week taking Monday as the first day of the week
Show the number of the week taking Sunday as the first day of the week
To see the various options we can execute the following command:
date –help
3. Edit the current time zone with date Linux command
By default, the date command makes use of the time zone defined in the / etc / localtime directory. The environment variable TZ (TimeZone) can be used to cancel this behavior, we can check the TZ using the following command:
TZ = GMT date
All valid time zones can be found in the / usr / share / zoneinfo / directory:
Some examples of how we can use the date command defining certain times are:
4. Use of seconds with date Linux command
Using the date command allows us to perform another type of analysis, for example, we can take the date command to convert a specific date or time to the time of the Unix era (seconds from 00:00:00, January 1, 1970 ) and vice versa, in this case, we will execute the following with which we will see the epoch seconds at the current time:
date +% s
The result will be as follows:
Using this same format we can obtain the seconds of a certain date, for example:
date -d "2017-12-31" + "% s"
5. How to convert epoch to a Linux date
Recall that epoch is the UNIX time format.
With date it is possible to convert epoch to a specific date using one of the following commands:
date -d "UTC 2000-01-15 123456897 secs" date -d @ 123456897
We also have the possibility of knowing the exact day of some date with the following line:
date -d "2000-01-15" + "% A"
Set a date manually and use the date command in Scripts and commands When it is necessary to set a date or time different from the current one, just run the following syntax:
date --set = "YYYYMMDD HH: SS"
It is possible to assign the result of the date command to a shell variable and then use it later in various scripts, for example, we can execute the following:
STARTTIME = `date` echo $ STARTTIME sleep 7 echo $ STARTTIME
We can see how the date command allows us to perform actions far beyond simply seeing the current date and time.