Date
Linux clock is divided into system clock and real time clock (RTC. The system clock is the current Linux kernel clock, and the hardware clock is the battery-powered clock on the motherboard. This hardware clock can be set in the BIOS. When Linux is started, the hardware clock reads the system clock settings, and then the system clock runs independently of the hardware.
All commands (including functions) in Linux use the system clock settings. In Linux, the commands used for clock viewing and setting mainly include date and hwclock.
If it does not start with a plus sign, the time is set, and the time format is mmddhhmm [[CC] YY] [. SS], where mm is the month, DD is the day, HH is the hour, mm is the minute, CC is the first two digits of the Year, YY is the second digit of the Year, SS is the second digit
In Linux Shell programming, addition and subtraction of dates are often used.
Previously, it was very troublesome to use expr function computing.
In fact, the date command itself provides the addition and subtraction operation of the date
Very convenient. For example, get the time of yesterday
Date + % Y % m % d -- date = "-1 day"
Date usage: Date [Option]... [+ format]
Date [-u | -- UTC | -- Universal] [mmddhhmm [[CC] YY] [. SS]
Date can be used to display or set the date and time of the system.
1. In terms of display, you can set the format to be displayed. The format is set to a plus sign followed by several tags. The list of available tags is as follows:
%: Print %
% N: Next row
% T: Skip
% H: hour (00 .. 23)
% I: hour (01 .. 12)
% K: hour (0 .. 23)
% L: hour (1 .. 12)
% M: minute (00 .. 59)
% P: displays local am or PM
% R: direct display time (in 12-hour format: hh: mm: ss [AP] m)
% S: the number of seconds since January 1, 1970 00:00:00 UTC till now
% S: seconds (00 .. 61)
% T: direct display time (in 24-hour format)
% X: equivalent to % H: % m: % s
% Z: Display Time Zone % A: day of the week (Sun .. SAT)
% A: The day of the week (Sunday .. Saturday)
% B: Month (Jan .. dec)
% B: Month (January... December)
% C: Display date and time directly
% D: Day (01 .. 31)
% D: Display date directly (mm/DD/yy)
% H: Same as % B
% J: The day of the year (001 .. 366)
% M: Month (01 .. 12)
% U: Week (00 .. 53) of the year (the first day of the week on Sunday)
% W: The day of the week (0 .. 6)
% W: Week (00 .. 53) of the Year (Monday is the first day of the week)
% X: Display date directly (mm/DD/yy)
% Y: last two digits of the Year (00.99)
% Y: full year (0000 .. 9999)
2. Set the time
Date-S // set the current time. Only the root permission can be set. Others can only be viewed.
Date-s 20080523 // set to 20080523, so that the specific time is set to null 00:00:00
Date-s 01:01:01 // set the specific time and do not change the date
Date-s "01:01:01" // you can set all the time
Date-s "01:01:01 20080523" // you can set all the time
Date-s "01:01:01" // you can set all the time
Date-s "20080523 01:01:01" // you can set all the time
3. addition and subtraction
Date + % Y % m % d // display current day year month day
Date + % Y % m % d -- date = "+ 1 day" // display the date of the next day
Date + % Y % m % d -- date = "-1 day" // display the date of the previous day
Date + % Y % m % d -- date = "-1 month" // display the date of the previous month
Date + % Y % m % d -- date = "+ 1 month" // display the date of the next month
Date + % Y % m % d -- date = "-1 year" // display the date of the previous year
Date + % Y % m % d -- date = "+ 1 year" // display the date of the next year
Or, for a simpler vertex, date = 'date-D-$ {t} Day' + % Y % m % d' // It is a few days before t.
[[Email protected] root] # date-D next-day + % Y % m % d
20060328
[[Email protected] root] # date-d last-Day + % Y % m % d
20060326
[[Email protected] root] # date-d Yesterday + % Y % m % d
20060326
[[Email protected] root] # date-d tomorrow + % Y % m % d
20060328
[[Email protected] root] # date-d last-month + % Y % m
200602
[[Email protected] root] # date-D next-month + % Y % m
200604
[[Email protected] root] # date-D next-year + % Y
2007
Date-S: Time modified in string Mode
Modify only the date. Enter date-S
Modify the time only. Enter date-s 14:15:00.
Modify the date and time at the same time. Add double quotation marks. There is a space between the date and time. Enter:
# Date-s "14:15:00"
Note: If you do not want meaningless 0 values (for example,), you can insert the-symbol in the tag, for example, date '+ %-H: %-M: %-s will remove the meaningless 0 in the hour, minute, and second, as if the original 08:09:04 will change. In addition, you can set the system time only when you have the permission (such as root.
After you change the system time as root, remember to write the system time to CMOS using clock-w, in this way, the system time will continue to hold the latest correct value upon the next reboot.
Expr
The expr command is generally used as an integer, but can also be used as a string. The general format is:
Expr argument operator argument
Expr is also a manual command line counter.
[[Email protected] script] # expr 10 + 10
20
[[Email protected] script] # expr 10-8
2
[[Email protected] script] # expr 2 \ * 3 backlash shielding its specific meaning
6
[[Email protected] script] # expr 30/3/2
5
Seq
Usage: seq [Option]... ending number
Or: seq [Option]... ending number and ending number
Or: seq [Option]... increment ending number
To print the number from the beginning to the end of a specified increment.
-F, -- format = format: Use the printf floating point format
-S, -- separator = string: Use the specified string to separate numbers (\ n by default)
-W, -- equal-width: Add 0 before the column so that the width is the same
-- Help: displays the help information and exits.
-- Version: displays the version information and exits.
If the first number or increment is omitted, the default value is 1, even if the ending number is still less than the first number.
The first, increment, and tail numbers are interpreted as floating-point numbers. When the first number is smaller than the ending number, the increase is generally positive,
On the contrary, when the first number is greater than the ending number, the increase is generally negative.
The specified format must be applicable to parameters of the double type.
The default value of precision in fixed-point decimal is "%. Precision F". Otherwise, the default value is "% G ".
Two methods from 1 loop to 100 (Bash other shells have not been tried)
For X in 'seq 1 100 '; do echo $ X; done
For X in {1 .. 100}; do echo $ X; done
1-output, excluding number 7, and cannot be divisible by 7
SEQ 100 | grep-V "7" | awk '$ 0% 7! = 0 {print }'
In addition, you can do this without seq:
[[Email protected] # For I in {1 .. 10}; do echo $ I; done
Between 1 and 10 are two halfwidth points.
-F is most commonly used. For example, it is useful to create ten dir001, dir002... dir010 targets at a time. We can
In this case, the next command is ready.
SEQ-f'dir % 03g '1 10 | xargs mkdir
Or
Mkdir $ (SEQ-f'dir % 03g '1 10)
It uses the printf format. % 03g represents three floating points. This method is used, for example, the printf of bash3.
It can also be used as a waiting command
Printf 'dir % 03d \ n' {1 .. 10} | xargs mkdir or mkdir 'printf' dir % 03d '{1 .. 10 }'
Awk certainly does.
Awk 'in in {While (Num <10) printf "dir % 03d \ n", ++ num; exit} '| xargs mkdir
This is faster than creating a notebook, so you don't have to convert it
For dir in 001 002 003 004 005 006 007 008 009 010
Do
Mkdir dir $ {dir}
Done
I also use the sequence of several digital JPEG values under seq, as long as the format has a sequence of numbers, especially some XXX sites ;)
For I in 'seq-F' % 02g'1 20'
Do
If! Wget-p $ home/tmp-C [img] http://www.xxxsite.com/photo/polici.jpg#/img]; then
Wget-p $ home/tmp-C $ _
Fi
Done
The-s option mainly changes the shard output. The Delimiter is \ n, Which is newline.
-S can be used for change, as shown in figure
SEQ-s ''1 10
1 2 3 4 5 6 7 8 9 10, with space as the shard,
Shell external auxiliary commands