Monday, 23 March 2020

Linux date command usage

Date command usages in Linux

The date command displays or sets the system date and time. It is most commonly used to print the date and time in different formats and calculate future and past dates.

$ man date
$ info date

$ date
Mon Mar 23 08:08:21 EDT 2020

$ date +"Year: %Y, Month: %m, Day: %d"
Year: 2020, Month: 03, Day: 23

$ date "+DATE: %D%nTIME: %T"
DATE: 03/23/20
TIME: 08:08:41

$ date -d "2010-02-07 12:10:53" #date string option
Sun Feb  7 12:10:53 EST 2010

$ date -d '16 Dec 1974' +'%A, %d %B %Y'
Monday, 16 December 1974

$ date -d "last week"             #Display past date
Mon Mar 16 08:10:54 EDT 2020

$ date -d 'TZ="Australia/Sydney" 06:30 next Monday'
Sun Mar 29 15:30:00 EDT 2020

$ TZ=GMT date
Mon Mar 23 12:15:31 GMT 2020

$ TZ='Australia/Melbourne' date #current time of some other location
Mon Mar 23 23:11:29 AEDT 2020 (ls /usr/share/zoneinfo)

$ date -u               #Display universal time
Mon Mar 23 16:57:45 UTC 2020

$ date --iso-8601=seconds #ISO 8601 format
2020-03-23T13:06:30-0400

$ date --rfc-3339=seconds #RFC 3339 format
2020-03-23 13:08:54-04:00

$ date +%s                #Epoch converter (seconds since 00:00:00, Jan 1, 1970)
1584965527

$ date -d @1234567890         #Convert epoch to a date
Fri Feb 13 18:31:30 EST 2009

$ date -d "2020-01-01" +"%s" #calculate the seconds from epoch to provided date/time
1577854800

$ date_now=$(date "+%F-%H-%M-%S") #use with shell scripts
$ echo $date_now
2020-03-23-08-13-50

$ date -r /etc/hosts                 #Last Modification Time of a File
Wed Jan 23 17:30:26 EST 2019

$ date --set="20200801 12:30" #Set the System Time and Date

$ date +"Week number: %V Year: %y"
Week number: 13 Year: 20

$ touch ~/Desktop/`date +%F`.txt #create a file with the date and time

$ tar cfz /backup-`date +%F`.tar.gz /home/ #create archives with the date and time

$ mysqldump  db_name > db_name-$(date +%Y%m%d).sql #mysqldump file with date


No comments:

Post a Comment