1. Basic commands under Linux
find (file search)
find /etc-cmin-5 Find files and directories whose attributes have been modified within 5 minutes under /etc
-amin access time access
-cmin file attribute change
-mmin file content modify
which (search for the directory where the command is located and alias information)
栗子:[wang@localhost /]$ which ls
alias ls = 'ls --color = auto'/bin/ls
whereis (the directory where the search command is located and the path to the help documentation)
[wang@localhost /]$ whereis ls
ls: / bin / ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
grep (search the file for lines that match the substring and output)
-i is case insensitive
-v exclude the specified string
for example:
[wang@localhost wangkaijia]$ vim 11.txt
[wang@localhost wangkaijia]$ cat 11.txt
abc
sds
ad
dsee
edf
abc12
abc34dfd
[wang@localhost wangkaijia] $ grep abc 11.txt
abc
abc12
abc34dfd
[wang@localhost wangkaijia]$ grep -v abc 11.txt
sds
ad
dsee
edf
wc (statistics command)
-c count bytes
-l count lines
-m count characters
-w count word count
-L print the length of the longest line
[wang@localhost wangkaijia]$ wc 11.txt
7 7 35 11.txt
[wang@localhost wangkaijia]$ wc -c 11.txt
35 11.txt
[wang@localhost wangkaijia]$ wc -l 11.txt
7 11.txt
[wang@localhost wangkaijia]$ wc -m 11.txt
35 11.txt
[wang@localhost wangkaijia]$ wc -w 11.txt
7 11.txt
[wang@localhost wangkaijia]$ wc -L 11.txt
8 11.txt
cut (cut command, select what we need, and output these selected data to standard output)
At present, the cut command mainly accepts three positioning methods:
First, bytes. -b;
Second, characters. -c;
Third, the field (fields) -f
Chestnuts are as follows:
[wang@localhost wangkaijia]$ grep -i abc 11.txt | cut -b 3
c
c
c
//Explain here, the knowledge of pipelines is used here. grep -i abc 11.txt | cut -b 3 this statement. meaning is
First grep the statement with abc, and then cut it in bytes. Cut the content of the third character and output.
Use -d and -f combination in cut
-d Custom delimiter, default is tab.
-f used with -d to specify which region to display
For example: a new 22.txt document is created here. Shows as follows
[wang@localhost wangkaijia] $ cat 22.txt
abc abc abc abcabc123 abc abc 23
abcsdad a adsd ds
sff fdf
//The cut command is used directly here. Customize the output with a space as the delimiter and select the column number as 1.
[wang@localhost wangkaijia]$ cut -d ' ' -f 1 22.txt
abcabc123
abcsdad
please
//Can be used with grep command.
[wang@localhost wangkaijia]$ grep -i abc 22.txt | cut -d ' ' -f 1
abc
abc123
abcsdad
sort (can be sorted in line units for the content of the text file)
sort compares each line of the file as a unit. ———"AscII code
-b ignore whitespace characters at the beginning of each line
-n sort by numerical value
-r sort in reverse order
-t<separation character> Specifies the column separation character used when sorting
-k choose which interval to sort by
-u global deduplication
Chestnut:
[wang@localhost wangkaijia]$ cat 44.txt
apple
banna
pear
orage
pear
[wang@localhost wangkaijia]$ sort -ur 44.txt
pearorage
banna
apple
Use of sort separator
sort -n -k 2 -t ':' Meaning of 55.txt: Output the contents of the 55.txt document in reverse order, according to the second column, with: as the delimiter
[wang@localhost wangkaijia]$ sort -n -k 2 -t ':' 55.txt
apple:10:2.5
orange:20:3.4
banana:30:5.5
pear:90:2.3
[wang@localhost wangkaijia]$ cat 55.txt
banana:30:5.5
apple:10:2.5
pear:90:2.3
orange:20:3.4
uniq (remove duplicate lines in a file)
Note that before using this command, use the sort command to make all duplicate lines adjacent.
-c Prepend output lines with the number of times each line appears in the input file.
-d only show duplicate lines
-u only show unique lines
for example:
[wang@localhost wangkaijia]$ cat 44.txt
apple
banna
pear
orage
pear
[wang@localhost wangkaijia]$ sort 44.txt | uniq
apple
banna
orage
pear
[wang@localhost wangkaijia]$ sort 44.txt | uniq -c
1 apple
1 banna
1 orage
2 pear
[wang@localhost wangkaijia]$ sort 44.txt | uniq -d
pear
[wang@localhost wangkaijia]$ sort 44.txt | uniq -u
apple
banna
storm
tar -zcvf 压缩 tar -zcvf japan.tar.gz japan
tar -zxvf 解压 tar -zxvf jspsn.tsr.gz
redirect
>: redirect output to a file or device overwrite the original file
>>: append
<: input redirection
2> redirect a standard error output to a file or device overwrite the original file
2>> redirect a standard error output to a file or device append to the original file
2&>1 redirect a standard error output to standard Output note: 1 may represent standard output
&> redirects a standard error output to a file or device overwriting the original file
[wang@localhost wangkaijia]$ cat 55.txt
total 16
-rw-rw-r--. 1 wang wang 45 Mar 13 16:14 11.txt
-rw-rw-r--. 1 wang wang 62 Mar 13 16:33 22.txt
-rw-rw-r--. 1 wang wang 62 Mar 13 17:10 33.txt
-rw-rw-r--. 1 wang wang 28 Mar 13 17:11 44.txt
-rw-rw-r--. 1 wang wang 0 Mar 13 19:04 55.txt
[wang@localhost wangkaijia]$ ls
11.txt 22.txt 33.txt 44.txt 55.txt
[wang@localhost wangkaijia]$ cat 11.txt 12.txt 2> 55.txt
abc
sds
ad
dsee ads
edf
abc12 asd
abc34dfd
[wang@localhost wangkaijia]$ cat 55.txt
cat: 12.txt: No such file or directory
Two, wildcard
(Wildcards are used by system commands)
*: matches any number of characters
? : matches any character
[...]: matches any character that appears in parentheses
[!...]: does not match any character that appears in parentheses
Third, regular expressions
*: 0 or more common characters before the * character
. : matches any character
^: matches the beginning of the line, or the non-existence of the following characters
$: matches end of line
[]: set of matching characters
\: escape character, mask the special meaning of a metacharacter
\<\>: exact match symbol
\{n\}: Matches the preceding character n times
\{n,\}: Match the preceding character at least n times
\{n,m\}: Match the preceding character n~m times
[[:digit:]] : digit
[[:lower:]] : lowercase letters
[[:upper:]] : uppercase letters
[[:alpha:]] : upper and lower case letters
[[:alnum:]] : numbers and uppercase and lowercase letters
[[:space:]] : whitespace character (space bar)
Specific examples:
Use . to match a single character
ls -l | grep ... x..x..x
This will filter out the detailed information displayed by ls according to the style of ...x..x..x.
Use ^ at the beginning of a line to match a string or sequence of characters
ls -l | grep ^ d
This will display the line starting with d.
Match a string or character with $ at the end of the line
ls | grep sp $
This will output the sp-terminated string.
Match any number: [0-9]
Match any lowercase letter: [az]
Match any letter: [A-Za-z]
Note that the use of ^ should be used directly in the first parenthesis, which means negation or does not match the content in the parentheses.
For example [^a-zA-Z], which means that it does not belong to any letter, not a letter. That is, matches any non-alphabetic character
[^0-9]: matches any non-numeric character
Use \{\} to match the number of occurrences of the pattern result
Use * to match all matching results any number of times, but if you only need to specify the number of times, you should use \{\}, this pattern has three forms
pattern\{n\} matches the pattern n times.
pattern\{n,\} matches the pattern at least n times.
pattern\{n,m} matches the pattern between n and m times. n,m is any integer from 0-255
for example:
The first 4 characters are numbers, the next is xx, and the last 4 are also numbers.
[0-9]\{4\}xx[0-9]\{4\}
The meaning of the representation is: the matching number appears 4 times. It is followed by xx, and finally the number appears 4 times.
Those special symbols in regular
? : Match 0 or 1 common character before it
+: matches one or more ordinary characters that precede it
(): Represents a set of characters or is used in expr
|: means or, matches an optional set of characters
The Bash shell itself does not support regular expressions. The shell commands and tools that use regular expressions, such as grep, sed, and awk.