Linux data command guide

  
                

The Linux date command is mainly used to display and set the system time. For programmers, the date command is very important. Date and time errors may cause the program to fail to be compiled. The following small series will introduce you to date. The usage of the command.

because the date and time is important, which is perhaps the development of the Network Time Protocol (NTP: Network Time Protocol) reasons. Let us know how the date command works.

Displaying the system date

To display the system date, simply type:

$ date

Thu Dec 5 22:55:41 WIB 2013

Formatting Display Dates

Dates come in many formats. If you don't like the default format, you can change the format. You might think "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 for RFC 2822

$ date -R

Thu, 05 Dec 2013 23:40:53 +0700

RFC 2822 The format is like this: Week, Day-Month-Year, Hour: Minute: Second Time Zone

Time Zone +0700 is equivalent to GMT +7.

By default, date uses the time zone defined in /etc/localtime. Valid time zone data is defined in /usr/share/timezones.

Display or set Coordinated Universal Time

On Wikipedia, UTC means

the world's major clock and time standard. This is one of the very similar alternatives to Green's standard time.

Display date and time in UTC form, use -u parameter

$ date -u

Thu Dec 5 16:45:58:UTC 2013

Using formatting options

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

$ date +”Day : %d Month : %m Year : %Y”< Br>

Day: 05 Month: 12 Year: 2013

$ date +%D

12/05/13

%D format is year/month/The format of the day.

If you want, you can output the name of the date. Here 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 +&rdquo ;%A %B-%d-%Y %c”

Friday December-06-2013 12:30:37 AM WIB

There are also many date formats. Just type:

$ date –help

or

$ man date

to display the syntax and parameters of the date command.

Basically, the date command translates all the formatting starting with the percent sign (%) and outputting all the content in quotes (“”).

Setting the system date and time

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

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

This will set your current system date and time to January 25, 2014 and 09:17:00 AM . Please note that you must have root privileges to do this. Otherwise you will get such a mistake.

date: cannot set date: Operation not permitted Sat Jan 25 09:17:00 WIB 2014

Reset your time

If you wish to reset your system You can use this technique with the date and time to the original value.

# hwclock

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

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

Using the date command in a script

Remember that I said before that you need to change the output of date? One answer is that you may need to program. Let's take a look at an example of a bash script.

$ vi display.date

#! /bin/bash

DATETIME=$(date +”DATE: %a %b-%d-%Y TIME: %T WEEK NUMBER: %W”)

echo $DATETIME

Save and run it:

$ . /display.date

DATE : Fri Dec-06-2013 TIME: 03:08:19 WEEK Number :40

If you find permission to reject the error message, type:

$ chmod 755 display.date

Using date

in the backup process Another example is the use of date in your backup process.

$ date +%F

2013-12-06

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

It compresses the folder /home/pungki/Documents into a file backup-2013-12-06.tar.gz located in the /daily_backup folder. (Annotation: through the “ `command'” to embed other commands on the command line, this character is not a single quote, but the same symbol with the wave number ~ the same key.)

Summary

date may be considered unimportant in some respects. But date plays an important role. To find out more about the date command, type man date in your console to access the man page.

The above is the usage of the Linux date command, so when your computer's time and date are wrong, you have to change it quickly to avoid inconvenience.

Copyright © Windows knowledge All Rights Reserved