Date Command in CentOS-display and set the system date and time

Source: Internet
Author: User
The time on the operating system may only be used as a clock. Especially in the console, we usually do not think that time is important. However, this is wrong for administrators. Do you know that the wrong date and time will make you unable to compile the program? Because date and time are important, this may be the reason for developing the Network Time Protocol (NTP: NetworkTimeProtocol. Let's take a look at how the date command works. To display the system date, as long

The time on the operating system may only be used as a clock. Especially in the console, we usually do not think that time is important. However, this is wrong for administrators. Do you know that the wrong date and time will make you unable to compile the program?

Because date and Time are important, this may be the reason for developing the Network Time Protocol (NTP: Network Time Protocol. Let's take a look at how the date command works.

Show system date

To display the system date, just enter:

$ dateThu Dec 5 22:55:41 WIB 2013
Format Display date

There are many formats for dates. If you do not like the default format, you can change the format. You may wonder, "Why do I need to change the format? The default output is enough for me. "

Yes, you are right, but when you are programming, the default output may not meet your needs, so you need some custom output.

Date and time output format of RFC 2822
$ date -RThu, 05 Dec 2013 23:40:53 +0700

RFC 2822The format is as follows:Week, day-month-year, hour: minute: Second time zone

Time zone + 0700 is equivalent to GMT + 7.

By defaultDateIs defined in/Etc/localtime. Valid time zone data is defined in/Usr/share/timezones.

Display or set Coordinated Universal Time

In Wikipedia, UTC means

The world's major clock and time standards. This is one of several very similar replacements for the Green location standard time.

Display the date and time in UTC format, using the-u parameter

$ date -uThu Dec 5 16:45:58:UTC 2013
Use formatting options

To customize your date format,Use the plus sign (+)

$ date +”Day : %d Month : %m Year : %Y”Day: 05 Month: 12 Year: 2013
 
$ date +%D12/05/13

% DThe format isYear/Month/Day format.

If you want to, you can output the date name. The following are some examples:

$ date +”%a %b %d %y”Fri 06 Dec 2013$ date +”%A %B %d %Y”Friday December 06 2013$ date +”%A %B %d %Y %T”Friday December 06 2013 00:30:37$ date +”%A %B-%d-%Y %c”Friday December-06-2013 12:30:37 AM WIB

There are also many date formats. Just enter:

$ date –help

Or

$ man date

To display the syntax and parameters of the date command.

Basically, the date command translates all the formats starting with percent (%) and the content output in quotation marks.

Set system date and time

Generally, you want your system date and time to be set automatically. If you want to manually modify it for some reason, you can use this command.

# date –set=”20140125 09:17:00”

This willSetYour current system date and timeJanuary 25, 2014 and 09:17:00 AM. Note:, YouRequiredWith the root privilege. Otherwise, you will get such an error.

date: cannot set date: Operation not permittedSat Jan 25 09:17:00 WIB 2014
Reset your time

If you want to reset your system date and time to the original value, you can use this technique.

# hwclockFri 06 Dec 2013 03:44:10 AM WIB -0.314082 seconds

This time sets the output of your system date and time to the hwclock command.

Use the date command in the script

Do you still remember why you need to change the date output? One answer is that you may need programming. Let's take a look at an example in the bash script.

$ vi display.date#! /bin/bashDATETIME=$(date +”DATE: %a %b-%d-%Y TIME: %T WEEK NUMBER: %W”)echo $DATETIME

Save and run it:

$ ./display.dateDATE : Fri Dec-06-2013 TIME: 03:08:19 WEEK Number :40

If you find the permission rejection error message, enter:

$ chmod 755 display.date
Use date in the backup process

Another example is to use date in the backup process.

$ date +%F2013-12-06$ tar zcfv /daily_backup/backup-`date +%F`.tar.gz /home/pungki/Documents

It compresses the folder./Home/pungki/DocumentsTo/Daily_backup folderFileBackup-2013-12-06.tar.gz. Other commands are embedded in the command line through the 'command '. this character is not a single quotation mark, but a tilde ~ The symbol of the same key position .)

Summary

Date may be considered unimportant in some aspects. However, date plays an important role. To learn more about the date command, enter man date in your console to access the man page.

Via: http://linoxide.com/linux-command/date-command-linux/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.