RSS Subscription
Linux Howtos & Tutorials

Enter your email:

Delivered by


NOTE:New tutorials are from LinuxCareer.com

Poll

Do you own or wish to have iPhone?
 


Linux eBooks FREE Download
A guide to programming Linux kernel modules
Introduction to Linux - A Hands on Guide
A Newbie's Getting Started Guide to Linux

Linux from Scratch - Create Your Own Linux System - Free eBook

Linux: The Hacking Solution (v.3.0)

SQLite 3 with PHP Essential Training – Free Video Training Tutorials

This guide will introduce you to the world of GNU/Linux

The GNU/Linux Advanced Administration

A Complete Beginner's Manual for Ubuntu 10.04 (Lucid Lynx)

Advanced Bash-Scripting Guide

Set up, maintain, and secure a small office email server

Partner Linux Sites:
How-To.LinuxCareer.com
Jobs.LinuxCareer.com
TuxMachines
Monsterb
LinuxBloggers
AdamsInfo
LinuxScrew
All For Linux

date

Article Index
1. Name
2. Synopsis
3. Frequently used options
4. Examples

1. Name

date [man page] - print or set the system date and time

2. Synopsis

date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

3. Frequently used options

-u, --utc, --universal
print or set Coordinated Universal Time
+%F full date; same as %Y-%m-%d

4. Examples

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

Share this linux post:

Submit date in Delicious Submit date in Digg Submit date in FaceBook Submit date in Google Bookmarks Submit date in Stumbleupon Submit date in Technorati Submit date in Twitter
 
Comments for this page are closed !!!
Please visit our new Linux Forum for additional help or discussion.


Linux eBooks FREE Download