The date command in Linux can be used to show the date or set the system date.
The most common options are:
Coordinated Universal Time, abbreviated UTC (from English Coordinated Universal Time), also known as civil time, is the reference time zone from which all other time zones in the world are calculated.
See the examples:
$ date Sat Oct 19, 2019 1:13:02 PM -03
The “-u” option shows the times in UTC format:
$ date -u Sat Oct 19, 2019 4:13:45 PM UTC
The date command allows you to display a date in several formats:
%D: mm/dd/yy.
%d: Day of the month (01 to 31).
%a: Abbreviated days of the week (Sun, Mon… Sat).
%A: Days of the week in full (Sunday, Monday… Saturday).
%h or %b: Abbreviated month (Jan, Feb… Dec).
%B: Full month (January… December).
%m: Month in numerals (01 to 12).
%y: The last two digits of the year (00 to 99).
%Y: Four-digit year (2019).
%T: Time in the 24-hour format HH:MM:SS.
%H: Time.
%M: Minute.
%S: Second.
%j: Day of the year (122)
%u: day of the week (1… 7) – 7 represents Sunday.
%w: day of the week (0… 6) – 0 represents Sunday.
Examples:
$ date +'Today is %A, %d of %B of %Y, the %j day of the year, %H: %M' Today is Saturday, November 02, 2019, the 292nd day of the year, at 14:14
This example was strange because it shows the date with location in English with a phrase in Portuguese.
To change the location and display everything in Portuguese, we can change the LC_ALL variable:
$ export LC_ALL=en_BR.UTF-8
When you run the date command again, the date will be in Portuguese:
$ date +'Today is %A, %d of %B of %Y, the %j day of the year, %H: %M' Today is Saturday, November 02, 2019, the 292nd day of the year, at 14:15
Changing the date format can be useful to be used in a variable to be used in a script:
#!/bin/bash
echo "Data backup"
export DATE=$(date +'%F-%H%M%S')
tar cvzf /home/uira/backup-$DATE.tar.gz /etc
The script above writes the date in the format YYYY-MM-DD-HHMMSS in the DATE variable. It then uses the variable as part of the name of a tar file that backs up the /etc. directory. The result of the execution will be a backup-2019-10-19-142339.tar.gz file.
Note that the result of a command can be used in a variable if it is contained in $ (command).
The date command can also be used to change the date of the system clock. In this example, I changed the system date to my birthday:
# date +%y%m%d -s “2019-05-11" 20190511
$ date Sat May 11 00:00:01 -03 2019
The TZ variable changes the date and time shown by the date command to the time zone.
Share
The sudo command on Linux executes a given command as if it were another user.…
The ss command on Linux is extremely useful for investigating sockets, providing various information about…
Free Linux command shows the amount of total memory in use and available, as well…
The shell has structures for testing conditions and executing certain program sequences several times (loop),…
The /etc/fstab file stores the configuration of which devices should be mounted and what is…
The Netcat Command in Linux or nc is a utility used to do “almost anything”…
This website uses cookies.