Detailed explanation of linux command date

Linux command summary date command

Command introduction:

date displays the date according to the given format or sets the system datetime. print or set the system date and time

The path where the command is located: /bin/date

Command syntax:

date [OPTION]... [+FORMAT]

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

Command parameters:

parameter

describe

-d

Display the time described by the string

-f

Display the time of each row in the DATEFILE file

-r

Displays the last modification time of a file

-R

Display time in RFC-2822 compatible date format

-rfc-2822

Display time in RFC-2822 compatible date format

-s

set time as string

-u

Display or set to Coordinated Universal Time time format

--help

Display help information for the date command

--version

Display the version information of the date command

Format parameter format

To say that David MacKenzie , the man who wrote this program, is really meticulous. He has adjusted so many format parameters, and I admire it.

parameter

describe

%%

display character %

%a

Abbreviation for the day of the week (Sun..Sat)

%A

Full name of the day of the week (Sunday...Saturday)

%b

Abbreviation of the month (Jan..Dec)

%B

The full name of the month (January..December)

%c

date and time. Entering only the date command will also show the same result

%C

Century (year divided by 100) [00-99]

%d

Date (in 01-31).

%D

Date (including year, month and day).

%e

Day of the month (1..31)

%F

Date, same as %Y-%m-%d

%g

Year (yy)

%G

Year (yyyy)

%h

Same as %b

%H

hours (00..23)

%I

Hours (01..12)

%j

Day of the year (001..366)

%k

hours ( 0..23 )

%l

hours (1..12)

%m

Month (01..12)

%M

minutes (00..59)

%n

newline

%N

Nanoseconds (000000000..999999999)

%p

AM or PM

%P

am or pm

%r

12-hour time (hh:mm:ss [AP]M)

%R

24-hour time (hh:mm)

%s

Seconds since 00:00:00 1970-01-01 UTC

%S

seconds (00..60)

%t

Tabs

%T

24-hour time (hh:mm:ss)

% u

Day of the week (1..7); 1 means Monday

% U

Week of the year, Sunday is the first day of the week (00..53)

%V

Week of the year, Monday is the first day of the week (01..53)

%w

Day of the week (0..6); 0 is Sunday

%W

Week of the year, Monday is the first day of the week (00..53)

%x

Date(mm/dd/yy)

%X

Time (%H:%M:%S)

%and

Year (00..99)

%AND

Year (1970…)

%with

RFC-2822 style number format time zone (-0500)

%WITH

time zone (eg, EDT), empty if time zone cannot be determined

 

 

 

Example of use:

1 : View the help information of the date command

 date --help

 man date

2 : Use the -d parameter

1

2

3

4

5

6

7

8

[root@Gin scripts]# date

Sun Jan 29 10:46:03 CST 2017

[root@Gin scripts]# date -d now

Sun Jan 29 10:46:07 CST 2017

[root@Gin scripts]# date -d 'next monday'

Mon Jan 30 00:00:00 CST 2017

[root@Gin scripts]# date -d yesterday +%Y%m%d

20170128

3:显示文件中的时间

1

2

3

4

5

6

7

8

9

10

11

[root@Gin scripts]# cat >date.txt

2013-11-17 10:54:00

2013-11-22 11:11:11

 

[root@Gin scripts]# more date.txt

2013-11-17 10:54:00

2013-11-22 11:11:11

 

[root@Gin scripts]# date -f date.txt

Sun Nov 17 10:54:00 CST 2013

Fri Nov 22 11:11:11 CST 2013

4:显示文件最后修改的时间

1

2

[root@Gin scripts]# date -r date.txt

Sun Jan 29 10:49:11 CST 2017

5:按各种格式显示当前日期时间

这个命令的格式参数实在是太多了,在此没有必要每一个参数都尝试一遍,大家可以对照Format参数表,自己敲一敲命令实践一下。

1

2

3

4

5

6

7

8

9

10

11

12

[root@Gin scripts]# date +%Y

2017

[root@Gin scripts]# date +%m

01

[root@Gin scripts]# date +%D

01/29/17

[root@Gin scripts]# date '+%Y-%m-%d'

2017-01-29

[root@Gin scripts]# date +%Y-%m-%d

2017-01-29

[root@Gin scripts]# date +%m/%d/%y

01/29/17

6: 设置系统时间

1

2

3

4

[root@Gin scripts]# date -s "2016-11-11 00:00:00"

Fri Nov 11 00:00:00 CST 2016

[root@Gin scripts]# date

Fri Nov 11 00:00:05 CST 2016

7:请给出如下格式的date命令,如:11-02-26。再给出实现按周输出,如:周六为6,请分别给出命令

1

2

3

4

5

6

[root@Gin scripts]# date +%y-%m-%d

17-01-29

[root@Gin scripts]# date +%F

2017-01-29

[root@Gin scripts]# date "+%Y-%m-%d %H:%M:%S"

2017-01-29 10:57:02

生产场景中常用时间格式来打包数据:

1

2

[root@Andy andy]# tar zcvf etc-$(date +%F).tar.gz /etc

[root@Andy andy]# tar zcvf etc-`date +%F`.tar.gz /etc

解析命令方法:反引号,或$(),如上

 

1

2

3

4

5

6

7

8

9

10

11

12

13

[root@Andy andy]# date +%w  ##显示周,0-6

3

显示过去与未来时间:

[root@Andy andy]# date +%F

2016-11-16

[root@Andy andy]# date +%F -d "-1day"

2016-11-15

[root@Andy andy]# date +%F -d "+2day"

2016-11-18

[root@Andy andy]# date +%F -d "+24hour"

2016-11-17

[root@Andy andy]# date +%F-%H -d "+2hour" 

2016-11-16-14

 

引用:http://www.cnblogs.com/ginvip/p/6539393.html
细节影响成败,感谢 琴酒网络 的总结整理。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325272989&siteId=291194637