SoFunction
Updated on 2025-03-10

Special symbols and regular expressions in Linux

Chapter 1 Special symbols of linux

1.1 Wildcard * {}

1.1.1 Meaning

Easy to find files Wildcards are used to find file names.

1.1.2  *

Use the find command to find files ending with .sh and use * instead of file names.

find /oldboy -type f -name "*.sh" -mtime +7 -size +100k -size -10M

Find files containing oldboy bytes in the file name.

[root@znix 20170118]# find -type f -name "*oldboy*"
[root@znix 20170118]# ls -l *oldboy*

1.1.3  {}

{} is used to generate sequences

[root@znix 20170118]# echo oldboy{1..3}.txt
  
[root@znix 20170118]# echo {a,c,d,f}

a c d f

echo {a..z} {A..Z} There needs to be spaces in the middle to represent two unrelated sequences

[root@znix 20170118]# echo {a..z} {A..Z}

a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Generate sequence by rule {start...end...interval}

[root@znix ~]# echo {1..10..3}

1 4 7 10

Use when backing up a file

[root@znix ~]# cp {,.bak}
[root@znix ~]# ll oldboy*
-rw-r--r-- 3 root root 241 Aug 30 11:40 
-rw-r--r-- 1 root root 241 Aug 31 09:38 

1.2 Special symbols

1.2.1 Special symbols

>  Standard output redirection, clear the content first, and then put other things into the file

>> Standard append redirection Add content to the file

<  Standard input     xargs

<< Append input    cat>/<<EOF Append multiple lines

.  The hidden files under the current directory/linux

.. The previous directory of the current user

~  Current user's home directory

/  Root The splitting symbol of the path

\  Temporarily cancel the alias

| Pipeline

!

1) Vim compulsory

2) Inverse find awk

3) Indicates using the command you have used Use history commands

! You can see the history command

       !ls   ===== history |grep ls

[root@znix ~]# history

#   Comments

$   Take out the contents of the variable

&& and the previous command is run successfully, and then the following command is run

  ifdown eth0 && ifup eth0

;    Multiple commands are separated and multiple commands are placed in the same line.

ls; pwd; hostname

1.2.2 Single quotes, double quotes, and no quotes

' '

What to eat and what to vomit

[root@znix ~]# echo '$LANG $(pwd) `hostname` {1..3}'
$LANG $(pwd) `hostname` {1..3}

" "

Analyze the special symbols in double quotes

[root@znix ~]# echo "$LANG $(pwd) `hostname` {1..3}"
en_US.UTF-8 /root znix {1..3}

Without quotes

[root@znix ~]# echo $LANG $(pwd) `hostname` {1..3}
en_US.UTF-8 /root znix 1 2 3

`  `

Backticks Run first, leave the result the same as $()

[root@znix ~]# du -sh  `find -type d`

764K    .

Chapter 2 Regular Expressions

2.1 What is a regular

Special symbols represent text Text

^

[0-9] Number

2.2 Function

Improve efficiency and save trouble

2.3 Category

2.3.1 Basic regular expressions

^ $  ^$ . * .* [0-9] [^0-9]

2.3.2 Extended regular expressions

|  ()  +   {}  ? 

2.4 The difference between regular expressions and wildcard characters

1. Wildcards are used to find files.

2. Find content and text in the file used by regular expressions.

2.5 Basic regular expressions

2.5.1 Environmental preparation

cat -A adds a $ symbol at the end of each line.

[root@znix ~]# 
I am oldboy teacher!$
I teach linux.$
$
I like badminton ball ,billiard ball and chinese chess!$
my blog is .$
$
our site is $
$
my qq num is 49000448.$
$
not 4900000448.$
my god ,i am not oldbey,but OLDBOY!$

2.5.2 Find the line starting with m ^

^m means starting with m, ^ means starting with what.

[root@znix ~]# grep "^m" 
my blog is .
my qq num is 49000448.
my god ,i am not oldbey,but OLDBOY!

2.5.3 Lines ending in m $

m$ means ending in m.

[root@znix ~]# grep "m$" 
my blog is .

2.5.4 Display blank lines and add line number

-n Show line number

^$ means there is nothing between the beginning and end, that is, empty lines

[root@znix ~]# grep -n "^$" 

3:

6:

8:

10:

2.5.5 represents any character. (dot)

A dot represents any character, and it can be used to indicate the position of the dot.

[root@znix ~]# grep "" 
I am oldboy teacher!
my blog is .
my god ,i am not oldbey,but OLDBOY!

grep -o Shows the process of grep/egrep execution (every time you find something).

[root@znix ~]# grep -o "." 
[root@znix ~]# grep -o "" 
oldboy
oldboy
oldbey

2.5.6 Find a line ending with a point

\ Change the meaning symbol, remove the special meaning of special meaning.

\.$ means ending with a dot.

[root@znix ~]# grep '\.$' 
I teach linux.
my qq num is 49000448.
not 4900000448.

2.5.7 * The previous text appears 0 or more times in succession

It appears 0 times in a row but it doesn't appear

-o Show grep finding process

[root@znix ~]# grep "0*" 
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is .
our site is 
my qq num is 49000448.
not 4900000448.
my god ,i am not oldbey,but OLDBOY!
[root@znix ~]# grep -o "0*" 
000
00000

2.5.8 Greediness of regular expressions

As many as you want, match as many as you can.

2.5.9.* means all

Show all contents, find them once.

[root@znix ~]# grep -o ".*"  
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is .
our site is 
my qq num is 49000448.
not 4900000448.
my god ,i am not oldbey,but OLDBOY!

Indicates all.*  It will show greed when it appears continuously.

[root@znix ~]# grep "^.*m" 
I am oldboy teacher!
I like badminton ball ,billiard ball and chinese chess!
my blog is .
my qq num is 49000448.
my god ,i am not oldbey,but OLDBOY!

2.5.10 [abc] brackets represent a whole

It is equivalent to a symbol, indicating a or b or c.

[root@znix ~]# grep "[0-9]" 
[root@znix ~]# grep "[A-Z]" 
[root@znix ~]# grep "[a-z]" 

Find uppercase and lowercase letters in the text.

[root@znix ~]# grep "[a-zA-Z]" 

2.5.11 Find lines starting with m or n or o and ending with m or g

.*

^[mno] m or n or o

[mg]$ m  or ending with g

[root@znix ~]# grep "^[mno].*[mg]$" 
my blog is .
our site is 

2.5.12 [^abc] Exclude a or exclude b or exclude c

[^abc]  Indicates finding exclusionaOr excludebOr excludecOther characters other than
[root@znix ~]# grep "[^abc]" 
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is .
our site is 
my qq num is 49000448.
not 4900000448.
my god ,i am not oldbey,but OLDBOY!
2.5.13 grep -v Exclude and[^abc]
grep -v Exclude row
[^abc]  Characters or text

Chapter 3 Yesterday’s review (delete files, start up by booting)

3.1 How to make a service/script boot on and start automatically?

1)chkconfig

2)/etc/

3.1.1 What are the requirements to be managed by chkconfig

1) Must be placed in /etc//

2) This script must have execution permissions

3) Add the content required by chkconfig

# chkconfig: 2345 99 99

4) chkconfig --add add script to boot

5) Check

3.2 /etc/

[root@znix ~]# ls -l /etc// |grep 
lrwxrwxrwx. 1 root root 11 Aug 10 18:36 S99local -> ../

3.3 Insufficient disk space no space left on device

1) Block full of 500G 3*200G video

 df -h
 du -sh /*
 du -sh /* |grep G

2) The block is full, the file has not been completely deleted, the number of hard links is 0, and the number of process calls is not zero

Check:lsof|grep delete

3.4 File deletion principle (condition)

1. The number of hard links is 0

2. The number of process calls is 0

log

/var/log/messages
/var/log/secure
rsyslog

3. Inode is full

Create a file to occupy an inode and at least one block

A large number of small files

Summarize

The above is the special symbols and regular expressions in Linux introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!