When writing a Linux shell script, date is a frequently used command, this article makes a summary, in case you use to look everywhere
1, the most basic, display the current specific period: directly into the date can, as below,
[Email protected]:~/scripts$ Date
Saturday, January 03, 2015 21:46:49 CST
2. Display the last modified time of a file: Date-r
[Email protected]:~/scripts$ date-r save.sh
Friday, January 02, 2015 23:29:24 CST
3. Displays the time represented by a string: date-d string
[Email protected]:~/scripts$ date-d 20:32
Saturday, January 03, 2015 20:32:00 CST
4. Show Time Only
[Email protected]:~/scripts$ Date +%r
10:00 P.M. 35 sec
[Email protected]:~/scripts$ Date +%t
22:01:01
(Above 12-hour system, below 24-hour system)
[Email protected]:~/scripts$ Date +%x
22:02 28 sec
(Localization notation)
5, Display Date:
[Email protected]:~/scripts$ Date +%d
01/03/15
[Email protected]:~/scripts$ Date +%f
2015-01-03
[Email protected]:~/scripts$ Date +%x
January 03, 2015
(localized representation)
6. Show only hours and minutes:
[Email protected]:~/scripts$ Date +%r
21:52
7, only show the year
[Email protected]:~/scripts$ Date +%y
15
[Email protected]:~/scripts$ Date +%y
2015
8. Show only the Month
[Email protected]:~/scripts$ Date +%m
01
[Email protected]:~/scripts$ Date +%b
January
[Email protected]:~/scripts$ Date +%b
January
(last one for localization)
9. Show week
[Email protected]:~/scripts$ Date +%a
Six
[Email protected]:~/scripts$ Date +%a
Saturday
10. Display the exact date
[Email protected]:~/scripts$ Date +%d
03
[Email protected]:~/scripts$ Date +%e
3
Note that the second method removes 0, which in some cases is useful
11, display hours (24-hour system)
[Email protected]:~/scripts$ date +%h-d 6:45
06
[Email protected]:~/scripts$ date +%k-d 6:45
6
The second method removes 0
12, display hours (12-hour system)
[Email protected]:~/scripts$ date-d 16:45 +%i
04
[Email protected]:~/scripts$ date-d 16:45 +%l
4
Note that the upper is upper case I, below is the lowercase l
13. Display minutes
[Email protected]:~/scripts$ date-d 16:05 +%m
05
14, display seconds
[Email protected]:~/scripts$ Date +%s
01
15. Remove the 0 in front of the number
Either notation can be used to "add a _ before the parameter" method to remove the number in front of the 0, which is necessary to participate in the mathematical operation. For example, for minutes and seconds, to remove the previous 0, this can only be:
[Email protected]:~/scripts$ date-d 16:05 +%_m
5
[Email protected]:~/scripts$ date-d 16:05:09 +%_s
9
The same is true for other parameters, but for hours it is clear that you have independent parameters.
16, show this is the first week of the year (Monday is the beginning)
[Email protected]:~/scripts$ Date +%v
01
[Email protected]:~/scripts$ Date +%u
00
I don't know what the next one is.
17. Show the day of the year
[Email protected]:~/scripts$ Date +%j
003
18, show this is the day of the week
[Email protected]:~/scripts$ Date +%w
6
19, localized display all information
[Email protected]:~/scripts$ Date +%c
January 03, 2015 Saturday 22:33 43 seconds
(It feels like a waste.) )
Summary of the date command