In this post we will learn about how to set date and time as well as how to get date and time information with date command.
Note: If you want to set date and time of system you must be login with superuser or root.
Date command is one of the useful command in linux. At the end I will show you some examples which are generally used by Server Admins.
Date command can also be used as timezone converter , “use date command as a timezone converter“
How to see system date and time
To see the system date and time,use the given below command i.e date .
linux@tuxworld:~$ date Tue Jul 2 19:50:40 IST 2013 linux@tuxworld:~$
How to set Date and Time
To set the date and time of system use the format with date command i.e date MonthDateHourMinute
In this example I set the date and time , July 02 08:01 p.m
root@tuxworld:~# date Tue Jul 2 19:56:32 IST 2013 root@tuxworld:~# root@tuxworld:~# date 07022001 Tue Jul 2 20:01:00 IST 2013 root@tuxworld:~#
There is another method, You can also set the date and time by using strings. Below are the examples. date --set="Write-your-strings-as-per-given-below-examples"
(In this example if you do not set time,it will start with 00:00 means 12'O clock midnight) root@tuxworld:~# date --set="July 02 2013" Tue Jul 2 00:00:00 IST 2013 root@tuxworld:~# date --set="July 02 2013 20:10:01" Tue Jul 2 20:10:01 IST 2013 OR root@tuxworld:~# root@tuxworld:~# date --set="07/02/2013 20:10:01" Tue Jul 2 20:10:01 IST 2013 OR root@tuxworld:~# date --set="02-July-2013 20:10:15" Tue Jul 2 20:10:15 IST 2013 root@tuxworld:~# OR root@tuxworld:~# date --set="20130702 20:15:01" Tue Jul 2 20:15:01 IST 2013 root@tuxworld:~#
How To Set Only Time
You can also set only time by using –set flag
root@tuxworld:~# date --set="20:15:01" Tue Jul 2 20:15:01 IST 2013 root@tuxworld:~#
Using flags to get specific output from date command
There are many flags which you can use with date command to get specific information.
I will recommend you once read the man page of date man date
. We are sharing a few examples which will help you a lot.
Syntax
date +%format-flag
Date Command Examples
Example 1
In this example, I want to see only date but having hyphen(-) sign in between
root@tuxworld:~# date +%F 2013-07-02 root@tuxworld:~#
Example 2
See the date and having separated by slash (/)
root@tuxworld:~# date +%D 07/02/13 root@tuxworld:~#
Example 3
Now I want to see only weekday in Full words or abbreviation
root@tuxworld:~# date +%b Jul root@tuxworld:~# date +%B July root@tuxworld:~#
Example 4
To see the time
root@tuxworld:~# date +%T 20:31:22 root@tuxworld:~# root@tuxworld:~# date +%R 20:31 root@tuxworld:~#
Example 5
Now check this %n is for new line here and %t for new tab
root@tuxworld:~# date '+DATE: %m/%d/%y%nTIME:%H:%M:%S' DATE: 07/02/13 TIME:20:32:48 root@tuxworld:~# root@tuxworld:~# date '+DATE: %m/%d/%y%tTIME:%H:%M:%S' DATE: 07/02/13 TIME:20:33:53 root@tuxworld:~#
Example 6
To see only timezone with date command
root@tuxworld:~# date +%Z IST root@tuxworld:~#
Date Command While Taking Backup Of Files
For example we want to take backup of file by using cp
command means copy . Here we are using backticks ( ` ).
You can play with date command for many purposes it depends upon your task.
Example 1
In this example, copy the file and appending date as suffix in file name. It is like taking backup of file. It helps to identify on which date the file was backup.
root@tuxworld:/tmp# ls -l data_list -rw-r--r-- 1 root root 0 Jul 2 20:38 data_list root@tuxworld:/tmp# root@tuxworld:/tmp# cp -p data_list data_list.`date +%F` root@tuxworld:/tmp# ls -l data_list* -rw-r--r-- 1 root root 0 Jul 2 20:38 data_list -rw-r--r-- 1 root root 0 Jul 2 20:38 data_list.2013-07-02 root@tuxworld:/tmp#
Example 2
In this example, we are using Date command in bash script. It will create the mysql dump(backup) and also append date with the mysql dump file name.
#!/bin/bash
mysqldump -u username -p password database-name > database-name-backup.`date +%F`.sql
Check Date Command Version
To check the version of date command apply the following command.
root@tuxworld:~# date --version date (GNU coreutils) 8.13 Copyright (C) 2011 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later . This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by David MacKenzie. root@tuxworld:~#
Do you want to know how we can use the Date command for converting the TimeZone. Read our this article “Use Date As A TimeZone Converter“