Linux (vi) Shell operations practical tips

Source: Internet
Author: User

First, the shell operation date Time

The Linux system provides us with a command date that is designed to display or set the date and time of the system.
The syntax format is:
Date [OPTION] ... [+format] or
Date [-u|--utc|--universal] [MMDDHHMM[[CC]YY][.SS]]
Some of the options you can use are:
--HELP Display Auxiliary Information
--version Display the date command version information
-U Shows current GMT
-D Doing date-time-related operations
--date= '-datestr ' to do date-time related operations

1. Display System current DateTime date

Displays datetime date ' +%y-%m-%d%h:%m:%s ' in the specified format

2, set the system date Time date-s "2017-01-01 01:01"

3, sometimes, we operate the date and time, often to get the first few days or days after the time, then the date command also provides us with the option to implement this function '-d ' and-date, see the following thin example

First look at '-d ':
# # Get the time of the next day
Date-d next-day ' +%y-%m-%d%h:%m:%s '
Date-d ' next day ' +%y-%m-%d%h:%m:%s '
Another way to do this:
Date ' +%y-%m-%d%h:%m:%s '-D tomorrow
# # Get the time of the last day
Date-d last-day ' +%y-%m-%d%h:%m:%s '
Another way to do this:
Date ' +%y-%m-%d%h:%m:%s '-D yesterday

# # Get the time of next January
Date-d next-month ' +%y-%m-%d%h:%m:%s '
# # Get last January time date-d last-month ' +%y-%m-%d%h:%m:%s '
# # Get the next year time date-d next-year ' +%y-%m-%d%h:%m:%s
# # Get the time of the previous year date-d last-year ' +%y-%m-%d%h:%m:%s '
# # Get last week datetime: date-d next-week ' +%y-%m-%d%h:%m:%s '
Date-d next-monday ' +%y-%m-%d%h:%m:%s '
Date-d next-thursday ' +%y-%m-%d%h:%m:%s '

So similarly, in fact, Last-year, Last-month, Last-day, Last-week, Last-hour, Last-minute, Last-second all have corresponding implementations. Conversely, last corresponds to next and can be flexibly organized according to the actual situation
Next, let's look at '-date ', which helps me to calculate before and after any time, to see specific examples:
# # Gets the day after the datetime date ' +%y-%m-%d%h:%m:%s '--date= ' 1 days '

Date ' +%y-%m-%d%h:%m:%s '--date= '-1 day ago '

# # Gets the day before the datetime date ' +%y-%m-%d%h:%m:%s '--date= '-1 days '
Date ' +%y-%m-%d%h:%m:%s '--date= ' 1 day ago '
The above example shows the format used, the essence of which is to change the previous string display format, change the data, change the date corresponding field to manipulate, in addition to the day there are other corresponding implementation: year, month, week, days, hour, minute, second, Monday (week, seven days)

4, date can be used to display or set the system's Day and time, in terms of display, the user can set the format to be displayed, the format is set to a plus followed by a number of tokens, where the list of available tags is as follows:
Usage Example: Date ' +%y-%m-%d%h:%m:%s '

Date:
% A: Day of the Week (Sun. Sat)
% A: 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: Show date directly (MM/DD/YY)
%h: Same as%b
%j: Day of the Year (001..366)
%m: Month Copy (01..12)
% U: Week of the Year (00..53) (with Sunday as the first day of the week)
%w: Day of the Week (0..6)
% W: Week of the Year (00..53) (Monday is the first week of Days)
%x: Show date directly (MM/DD/YY)
%y: Last two digits of year (00.99)
% y: Full year (0000..9999)
time facet:
%: Print out
%%n: Next line
%t : Jump grid
% H: Hours (00..23)
%I: Hours (01..12)
%k: Hours (0..23)
% L: Hours (1..12)
% M: minutes (00..59)
%p: Show local AM or PM
%p: Show local AM or PM
%r: Display time directly (12-hour format, Hh:mm:ss [ap]m)
%s: Number of seconds from January 1, 1970 00:00:00 UTC to date%s: seconds (00..61
% T: Direct display time (24-hour)
% X: equivalent to%h:%m:%s%p
%Z: Show time zone
If you do not start with a plus sign, you want to set the time, and the time format is MMDDHHMM[[CC]YY][.SS]
MM is Month,
DD is day,
HH is hour,
mm is minutes,
CC is the first two digits of the year,
YY is two digits after the year,
SS is seconds

5. Useful Tips

# # Get date relative to a date: date-d ' may 14-2 weeks '
# # To remove the useless 0 of time, for example: 01:02:25 will become 1:2:25 date ' +%-h:%-m:%-s '
# # Displays the last time the file was changed date "+%y-%m-%d%h:%m:%s"-R bin/removejdk.sh
# # Two number of days between string dates expr ' (' $ (date +%s-d "2016-08-08")-$ (date +%s-d "2016-09-09") ') '/86400
# # Add and subtract specified interval units in shell
A= ' Date +%y-%m-%d '
b= ' Date +%y-%m-%d-d ' $A +36 hours "'

Second, shell operation string

Working with strings in Linux is also an essential skill. In particular, the interception of strings more frequently, the following for you to introduce 10 ways to intercept strings
1, #截取, delete the left string (including the established delimiter), retain the right string
Define a variable in advance: website= ' http://hadoop/centos/huangbo.html '
Echo ${website#*//} results: hadoop/centos/huangbo.html
2, # #截取, delete the left string (including the specified delimiter), keep the right string, and the top one # different is that it always finds the last, instead of finding one as a # that satisfies the condition of exiting.
Echo ${website##*//} results: huangbo.html
3,% intercept, delete the right string (including the established delimiter), leave the left string
Echo ${website%//*} results: Http://hadoop//centos
4, the percentage of interception, delete the right string (including the specified delimiter), leaving the left string, and the top one is different, it has been found the most front, and not like a% to find a satisfied condition exited.

Echo ${website%%//*} results: http:
5, starting from the first few characters on the left, and the number of characters to intercept echo ${website:2:2} results: TP
6. Starting from the first few characters on the left, until the end of Echo ${website:2} results: tp://hadoop//centos//huangbo.html
7, starting from the right of the first few characters, as well as the number of characters echo ${website:0-4:2} results: HT
8, starting from the first few characters on the right, until the end of Echo ${website:0-4} results: HTML
9. Using awk for string interception echo $WEBSITE | awk ' {print substr ($1,2,6)} ' result: ttp://
10. Using cut for string interception echo $WEBSITE | Cut-b 1-4 Results for HTTP
echo $WEBSITE | Cut-c 1-4 Results for HTTP
echo $WEBSITE | Cut-b 1,4 Results HP
echo $WEBSITE | Cut-c 1,4 Results HP
Third, Linux get string length

Here are five ways to get the length of a string
1. Using the wc-l command
Echo ${website} |wc-l Results 35
2, the use of expr method to calculate
Expr length ${website} result 35
3. Get the string length by awk + length
Echo ${website} | awk ' {print length ($)} ' result 35
4. The number of fields separated by "" is calculated by awk
Echo ${website} |awk-f "" ' {print NF} ' result 35
5. Get the string by # (simplest, most used)
echo ${#WEBSITE} result 35



Linux (vi) Shell operations practical tips

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.