[[email protected] ~]# ls
111 234 anaconda-ks.cfg Dir3 test5 Yum.log
123 2.txt a.txt dir4 Testb.txt Learning Schedule
1_hard.txt 2.txt.bak BB split_dir testc.txt
1.txt 3.txt dir2 test4 Thinking_in_java (Chinese Version _ Fourth edition). pdf
[[email protected] ~]# ls test*
Test4 test5 Testb.txt Testc.txt
[email protected] ~]# ls test?
Test4 TEST5
[[email protected] ~]# ls test\* de-ideographic character, * no special meaning
LS: Unable to access test*: No file or directory
[Email protected] ~]#
[Email protected] tmp]# abc=123 #aaa
[Email protected] tmp]# echo $ABC
123
Command Cat
The Cut command is used to intercept a field whose format is cut-d ' delimited character ' [-CF] n,n to represent a number.
-D: followed by delimited characters, separated by single quotation marks
-C: followed by the first few characters
-F: The next block is followed by the first chunk
[[email protected] ~]# cat/etc/passwd |cut-d ': '-F 1 |head-5 take/etc/passwd file first column in header 5 row
Root
Bin
Daemon
Adm
Lp
The-D option is followed by a colon as the separator character, and-F 1 means that the first paragraph is intercepted, and the space between-F and 1 is optional.
[Email protected] ~]# head-n2/etc/passwd |CUT-C2
O
I
[Email protected] ~]# head-n2/etc/passwd |CUT-C1
R
B
[Email protected] ~]# head-n2/etc/passwd |cut-c1-10
root:x:0:0
Bin:x:1:1:
[Email protected] ~]#
[Email protected] ~]# head-n2/etc/passwd |cut-c5-10
: x:0:0
X:1:1:
The-c option can be followed by 1 number n, or an interval n1-n2, or multiple numbers n1,n2 and N3.
[Email protected] ~]# head-n2/etc/passwd |cut-c1,3,10
Ro0
Bn
The sort command is used for sorting, in the form of sort [-t delimiter] [-KN1,N2] [-nru], where N1 and N2 refer to numbers
-T: followed by delimited characters, as with the-D option of cut
-N: means sorting using pure numbers
-r: Indicates reverse ordering
-U: means to repeat
-KN1,N2: Indicates that the N1 interval is sorted to the N2 interval, and the N1 field can be sorted by writing only-kn1.
[[email protected] ~]# sort/etc/passwd sort sorting, followed by the first character by the ASCII value of the comparison, and finally output them in ascending order
Adm:x:3:4:adm:/var/adm:/sbin/nologin
Avahi-autoipd:x:170:170:avahi Ipv4ll Stack:/var/lib/avahi-autoipd:/sbin/nologin
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Dbus:x:81:81:system message Bus:/:/sbin/nologin
Ftp:x:14:50:ftp User:/var/ftp:/sbin/nologin
Games:x:12:100:games:/usr/games:/sbin/nologin
Halt:x:7:0:halt:/sbin:/sbin/halt
Lizhipeng:x:1000:1000::/home/lizhipeng:/bin/bash
Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[Email protected] ~]# head-n5/etc/passwd |sort
Adm:x:3:4:adm:/var/adm:/sbin/nologin
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Root:x:0:0:root:/root:/bin/bash
The-t option is followed by a delimiter, and the-K option is followed by a single number to sort the string of the first range, with the-N option indicating the use of a purely numeric sort
[Email protected] ~]# head-n5/etc/passwd |sort-t:-k3-n
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Adm:x:3:4:adm:/var/adm:/sbin/nologin
Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
The-K option is followed by numbers N1 and N2 for ordering strings within the N1 and N2 regions, and-R for reverse ordering
[Email protected] ~]# head-n5/etc/passwd |sort-t:-k3,5-r
Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Adm:x:3:4:adm:/var/adm:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Bin:x:1:1:bin:/bin:/sbin/nologin
Root:x:0:0:root:/root:/bin/bash
Command WC
The WC command is used to count the number of lines, characters, or words in a document. Common options for this command are-l (count rows),-m (count characters), and-W (statistics)
[Email protected] ~]# WC/ETC/PASSWD
1211/etc/passwd
[Email protected] ~]# wc-l/etc/passwd
26/etc/passwd
[Email protected] ~]# wc-m/etc/passwd
1211/etc/passwd
[Email protected] ~]# wc-w/etc/passwd
44/etc/passwd
WC does not follow any options, directly with the document, it will be the number of rows, words and characters sequentially output.
Command Uniq
The Uniq command is used to remove duplicate rows, which are used only by the-C option, which represents the number of rows counted and writes the number of rows in front
[Email protected] ~]# vim Testb.txt
111
222
111
333
You must sort the files before using Uniq, otherwise it won't work.
[Email protected] ~]# Uniq testb.txt
111
222
111
333
[Email protected] ~]# sort Testb.txt |uniq
111
222
333
[email protected] ~]# cat Testb.txt
111
222
111
333
[Email protected] ~]# sort Testb.txt |uniq-c
2 111
1 222
1 333
Command Tee
The tee command is followed by a file name, which acts like a redirect, but it is more of a function to write a file to the file that follows it, and also to display it on the screen.
This command is commonly used in pipe | after
[Email protected] ~]# echo "KKKKKKKK" |tee testb.txt
Kkkkkkkk
[email protected] ~]# cat Testb.txt
Kkkkkkkk
Command TR
The TR command is used to replace characters and is commonly used to handle special characters appearing in documents
-D: Deletes a character followed by the character to be deleted
-S: Indicates deletion of duplicate characters
[[email protected] ~]# head-n2/etc/passwd |tr ' [A-z] ' [A-z] '
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
[[email protected] ~]# grep ' root '/etc/passwd |tr ' r ' R '
Root:x:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root:/sbin/nologin
Command split
The split command is used for cutting documents, and the common options are-B and-L.
-B: Indicates that the document is divided by size in bytes.
[Email protected] ~]# mkdir Split_dir
mkdir: Unable to create directory "Split_dir": File already exists
[Email protected] ~]# CD Split_dir
[[email protected] split_dir]# ls
passwd Xaa Xab
[Email protected] split_dir]# cp/etc/passwd.
CP: Do you want to overwrite "./passwd"? Y
[Email protected] split_dir]# split-b passwd
[[email protected] split_dir]# ls
passwd XAA Xab Xac
If split does not specify a target file name, it will be Xaa, Xab ... Such a filename to access the cut file. You can also specify a file name
[Email protected] split_dir]# rm-f xa*
[Email protected] split_dir]# Split-b passwd 123
[[email protected] split_dir]# ls
123AA 123ab 123ac passwd
-L: Represents the number of rows to split the document
[Email protected] split_dir]# rm-f 123a*
[Email protected] split_dir]# split-l passwd
[Email protected] split_dir]# Wc-l *
passwd
Ten XAA
Ten Xab
6 Xac
52 Total Dosage
Special Symbol $
The symbol $ can be used as the identifier in front of the variable, and can be used together with!
[Email protected] split_dir]# CD.
[[email protected] ~]# ls testb.txt
Testb.txt
[[email protected] ~]# ls!$
LS Testb.txt
Testb.txt
! $ represents the last variable in the previous command.
Special symbols;
Running two or more than two commands in a row, you need to add a symbol between the commands
Special Symbols ~
The symbol ~ Represents the user's home directory, the root user's home directory is/root, and the normal user is/home/username.
[Email protected] ~]# CD ~
[Email protected] ~]# pwd
/root
[Email protected] ~]# Su-lizhipeng
Last login: 41 months 05:19:32 CST 2018pts/0
[Email protected] ~]$ CD ~
[Email protected] ~]$ pwd
/home/lizhipeng
Special Symbols &
If you want to put a command in the background, you need to add the symbol &, which is usually used for a long time, for example, can be used after sleep.
[Email protected] ~]$ sleep &
[1] 2737
[[email protected] ~]$ jobs
[1]+ Running in Sleep &
redirect Symbols >, >>, 2>, and 2>>
redirect Symbols > and >>, which represent the meaning of substitution and append, respectively. When a command error is run, the error message is output to the current screen. If you want to redirect to a text, you need to use the redirect symbol 2> or 2>>, which represent error redirection and error append redirection, respectively.
[[email protected] ~]# ls AAAA
LS: Unable to access AAAA: No file or directory
[email protected] ~]# ls AAAA 2>/tmp/error
[Email protected] ~]# Cat/tmp/error
LS: Unable to access AAAA: No file or directory
[email protected] ~]# ls AAAA 2>>/tmp/error
[Email protected] ~]# Cat/tmp/error
LS: Unable to access AAAA: No file or directory
LS: Unable to access AAAA: No file or directory
Brackets []
A combination of characters within the brackets, representing any of the character combinations
Special symbols && and | |
Command1;commond2
Commond1 && Commond2
Commond1 | | Commond2
When used, the COMMOND2 is executed regardless of whether or not the COMMOND1 is performing successfully.
With &&, only commond1 execution succeeds, Commond2 executes, otherwise commond2 does not perform
Use | | , commond1 execution succeeds, Commond2 does not execute, otherwise execution commond2, that is, commond1 and Commond2 always have a command to execute
[Email protected] ~]# rm-f test*
[email protected] ~]# Touch test1 test3
[[email protected] ~]# ls test2 && touch test2
LS: Unable to access test2: No file or directory
[[email protected] ~]# ls test2
LS: Unable to access test2: No file or directory
Touch Test2 is performed only if LS test2 is executed successfully. Because the test2 does not exist, LS Test2 did not execute successfully, so the touch test2 behind && was not executed.
[[email protected] ~]# ls test2 | | Touch Test2
LS: Unable to access test2: No file or directory
[[email protected] ~]# ls test*
Test1 test2 Test3
If the LS test2 execution is unsuccessful, the touch test2 will be executed. Because the test2 does not exist, the LS test2 does not execute successfully, in turn executes | | Behind the touch test2, so added test2 this file.
8.10 Shell Special Symbol cut command 8.11 sort_wc_uniq command 8.12 tee_tr_split command 8.13 Shell special symbol