date [man page] - print or set the system date and time
date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
-u, --utc, --universal
print or set Coordinated Universal Time
+%F full date; same as %Y-%m-%d
date command is used to set and display time. By typing date command without any options and arguments the date command will display a current time:
linuxconfig.org:~$ date Wed May 12 12:30:55 EST 2010
date command can also be used to set time. However, this requires superuser permissions. To set time to 20th March 2010 23:45:28 a following date command can be used:
linuxconfig.org ~# date 032023452010.28 Sat Mar 20 23:45:28 EST 2010
Linux date command can be very flexible as it offers more ways on how to set a time on your Linux systems: the next example we set date and time to "05 JUL 2021 01:55:56".
linuxconfig.org ~# date -s "05 JUL 2021 01:55:56" Mon Jul 5 01:55:56 EST 2021
In case that we want to set just time but date a following syntax to date command can be applied:
linuxconfig.org ~# date +%T -s "18:30:01" 18:30:01 linuxconfig.org ~# date Mon Jul 5 18:30:05 EST 2021
Date command is often used with connection to bash scripting. In the next example we create a simple bash script to create a backup of a user's home directory and we will name the output backup file accordingly to a current date:
linuxconfig.org:~$ cat backup_date_example.sh #!/bin/bash OF=myhome_directory_$(date +%Y%m%d).tar.gz tar -czf $OF /home/linuxconfig linuxconfig.org:~$ chmod +x backup_date_example.sh linuxconfig.org:~$ date Wed May 12 12:47:39 EST 2010 linuxconfig.org:~$ ./backup_date_example.sh tar: Removing leading `/' from member names linuxconfig.org:~$ ls backup_date_example.sh myhome_directory_20100512.tar.gz linuxconfig.org:~$
Linux date command is also capable to do some simple arithmetics as shown in the following example:
linuxconfig.org:~$ echo "Today is `date +%A` `date +%e`th of `date +%B` \
`date +%G` which is `date +%j`th day since 1th of January `date +%G`" Today is Wednesday 12th of May 2010 which is 132th day since 1th of January 2010