[Linux study notes 27] Detailed explanation of basic shell commands commonly used (diff, patch, cut, sort, uniq, tr, &&, ||, test)

1. diff compare text files


Command format:diff [options] files或directorys

1.1 Interpretation of diff output information


输出信息 [num1,num2][a|c|d][num3,num4]
num1,num2	第一个文件中的行
a 添加
c 更改
d 删除
< 第一个文件中的内容
> 第二个文件中的内容
num3,num4	第二个文件中的行

1.2 Detailed explanation of diff parameters


diff parameters
-b
Ignore spaces
-B
Ignore blank lines
-i
Ignore case
-c
Show all the contents of the file and mark the difference
-r
Comparison catalog
-u
Merge output
-q
Only show whether there is a difference, do not show detailed information

1.3 Examples of diff command usage


  1. Compare files

Insert picture description here

  1. Compare files, ignoring spaces -b

Insert picture description here

  1. Compare files, ignore blank lines -B

Insert picture description here

  1. Compare files, ignore case -i

Insert picture description here

  1. Show all the contents of the file and mark the difference

Insert picture description here



2. patch


Install the patch:dnf install patch -y

Command format:patch 原文件 补丁文件

patch parameter
-b
Backup original file

patch patch example

1. Combine and output files with different contents and import them into a .path file

Insert picture description here
2. Install the patch program, patch the original file

Insert picture description here
Insert picture description here



3. Cut displays the specified content of the file


3.1 cut parameters


cut parameter
-d
Specify the separator (the default field separator is "TAB")
-f
Specify the displayed columns
-c
Specify the character to be intercepted
#-f -c用法相同
5==第5列	
3,5==第3和5列	
3-5==3到5列	
5-==第5列到最后	
-5==开始到第5列)

3.2 Examples of cut usage


Experimental material

head /etc/passwd > /mnt/passwd

Insert picture description here

1. Display the specified column

Insert picture description here

2. Display the specified characters

Insert picture description here


3. Filter users who cannot log in, and display user names

grep bash -v /etc/passwd | cut -d : -f 1

Insert picture description here



4. sort


Sort takes each line of the file/text as a unit and compares each other. The comparison principle is to compare the ASCII code values ​​from the first character to the back, and finally output them in ascending order.


4.1 sort parameter


sort parameter
-n
Pure numeric sort
-r
Reverse order
-u
Remove duplication
-O
Output to specified file
-t
Designated segregated house
-k
Specify the sorting column

4.2 sort example usage


Experimental material

Insert picture description here


Sorting example 1

Insert picture description here
Insert picture description here

Sorting example 2

Insert picture description here



5. uniq ignore duplicate lines


Used to report or ignore duplicate lines in the file, generally used in conjunction with the sort command


sort parameter
-c
Combine duplicates and count the number of duplicates
-d
Show duplicate rows
-u
Show only row

Experimental material

Insert picture description here

Combine duplicates and count the number of duplicates

Insert picture description here

Show duplicate rows

Insert picture description here

Show only row

Insert picture description here

Show the number of the most repeated files

sort -n sortfile | uniq -c | sort -k 1 -n | cut -d " " -f 8 | tail -n 1

Insert picture description here


6. tr replace, compress and delete characters


The tr command can replace, compress, and delete characters from standard input. It can turn a group of characters into another group of characters, and is often used to write beautiful single-line commands, which is very powerful.


tr ‘a-z’ ‘A-Z’ : Lowercase to uppercase

tr ‘A-Z’ ‘a-z’ : Uppercase to lowercase



7. && 与 ||


&& Eligible actions

|| Unqualified actions

Insert picture description here



8 test


The test command is a practical tool for testing conditional expressions in the shell environment


test== [ ]: [] Equivalent to test command

test \$a = \$b  相当于 ['\$a' = '\$b' ]

8.1 Test number comparison


=
Strings are equal
!=
Strings are not equal
-eq
equal
-born
not equal to
-the
Less than or equal to
-lt
Less than
-give
greater or equal to
-gt
more than the

Insert picture description here
Insert picture description here

8.2 Test condition relationship


-a
and
-O
or

Insert picture description here

8.3 Test to determine empty


-n
Determine the content is not empty nozero
-with
The judgment content is empty zero

Insert picture description here

8.4 test judgment on files


-e
exist
-ef
Whether the file node numbers are consistent
-nt
Is file 1 newer than file 2
-ot
Is file 1 older than file 2
-d
Is it a directory
-S
Socket
-L
Soft link
-f
Normal file
-b
Block device
-c
Character device

Determine whether the file exists -e

Insert picture description here

Determine whether the file node numbers are consistent -ef

Insert picture description here

Determine the new and old files -nt and -ot

Insert picture description here

Whether it is a directory -d (same as judging whether it is another file type)

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46069582/article/details/111100991