• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
sharadchhetri.com

sharadchhetri.com

Linux,Cloud Computing And DevOps

  • Home
  • Linux
  • CloudComputing
    • Introduction Cloud Computing
    • Amazon AWS
  • Free E-Books
  • About Me

Linux date command examples

July 2, 2013 by Sharad Chhetri Leave a Comment

Linux date command examples

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:~# 
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:~#

Try yourself some more possibilities :)

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. I will show only few examples.

date +%format-flag

examples –

eg 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:~# 

eg. 2 : See the date and having seperated by slash (/)

root@tuxworld:~# date +%D
07/02/13
root@tuxworld:~#

eg 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:~# 

eg 4 To see the time

root@tuxworld:~# date +%T
20:31:22
root@tuxworld:~#
root@tuxworld:~# date +%R
20:31
root@tuxworld:~# 

eg. 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:~# 

eg 6 : To see only timezone with date command

root@tuxworld:~# date +%Z
IST
root@tuxworld:~#

How server admin use the date command in scripting or for backup

for eg. I want to take backup of file by using cp command means copy . Here I am using backticks ( ` ).
You can play with date command for many purposes it depends upon your task.

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# 


#!/bin/bash
mysqldump -u username -p password database-name > database-name-backup.`date +%F`.sql

Ending up this post with Date –version 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:~# 

Read another post “use date command as a timezone converter”

Do'nt be greedy, share the knowledge!

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Pocket (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)

Related

Filed Under: Linux, Linux Commands, Tips And Tricks Tagged With: date

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Help To Needy

If you like our posts and tutorials, please support/donate to ANY “Open Source Project” or do some Charity work. Hope this will make someone happy in this world. Drop me an email if you want to inspire others!

Thank You!

Follow Us

  • Facebook
  • Twitter
  • GitHub
  • YouTube

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Top Posts & Pages

  • How to delete mail queue in Postfix
  • How to start / stop / restart / reload iptables on CentOS 7 / RHEL 7
  • How to find absolute path of command on Linux / Unix : which command
  • How to fix read only USB pen drive in Ubuntu
  • How to set hostname and FQDN on CentOS 7 and RHEL 7
  • Install KVM on Ubuntu 14.04 LTS Desktop
  • How to zip the directory in linux with command line
  • 4 different commands to check the load average in linux
  • How to install XAMPP on CentOS/RHEL 6.5
  • CentOS 7 / RHEL 7 : change OpenSSH port number ( SELINUX enabled )

Recent Posts

  • Install and setup maven in Linux for Jenkins
  • How to setup Jenkins Credentials for Git repo access
  • How to manage Jenkins Credentials
  • Managing Jenkins plugins from command line and GUI
  • How to create Jenkins user by command line and GUI

Footer

Sharad Chhetri is an experienced Linux - Cloud Engineer & freelancer. Working on Open Source Technology since RHEL 4.0 (Red Hat Enterprise Linux). He loves sharing the knowledge which earned from real scenarios. Don't be surprised if you find him in technology seminars and meetup groups. You can contact him on email for freelance projects at admin@sharadchhetri.com. Read More…

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy

Our GitHub Repo

sharadchhetri @ GitHub
  • java-samples
    Java Sample Project For Tutorial
    December 25, 2018 - 2:29 pm UTC
  • misc
    OpenSSH rpm created from tar ball
    November 21, 2018 - 3:12 am UTC
  • scripts
    Cassandra Backup Script
    November 21, 2018 - 3:09 am UTC
  • packer
    Packer : Create server images in automated way
    November 21, 2018 - 3:08 am UTC
  • Nagios
    My Nagios custom plugins
    November 21, 2018 - 3:07 am UTC

Recent Comments

  • Sharad Chhetri on CentOS 7 / RHEL 7 : change OpenSSH port number ( SELINUX enabled )
  • Dave on CentOS 7 / RHEL 7 : change OpenSSH port number ( SELINUX enabled )
  • Prafulla S on Install Shotcut on Ubuntu 16.04 LTS desktop
  • Sujata on Install Telnet Server on CentOS 7 / RHEL 7
  • kishen sharma on How to install redis server on CentOS 7 / RHEL 7
  • Sharad Chhetri on Secondary Logging : save all users history command output as log
  • Anand on Secondary Logging : save all users history command output as log
  • Sharad Chhetri on Install redis 3.0 from source on Ubuntu 14.04 / CentOS 7 / RHEL 7

Copyright © 2009 - 2019 · All Rights Reserved sharadchhetri.com · · Privacy Policy ·
· sitemap.xml · ·The content is copyrighted to sharadchhetri.com and may not be reproduced on other websites without our permission. ·