1. 程式人生 > >Day15 - linux的特殊符號與正則表達式

Day15 - linux的特殊符號與正則表達式

find 括號 window type yellow 啟動 字符 clip 標準輸出

第1章 linux的特殊符號

1.1 通配符 * {}

1.1.1 含義

方便查找文件 通配符是用來找文件名字的。

1.1.2 *

通過find 命令找以 .sh 結尾的文件,使用*替代文件名字。

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

查找文件名中,包含有oldboy字節的文件。

[[email protected] 20170118]# find -type f -name "*oldboy*"

[[email protected] 20170118]# ls -l *oldboy*

1.1.3 {}

{} 用來生成序列

[[email protected] 20170118]# echo oldboy{1..3}.txt

oldboy1.txt oldboy2.txt oldboy3.txt

[[email protected] 20170118]# echo {a,c,d,f}

a c d f

echo {a..z} {A..Z} 中間需要有空格,表示兩個無關的序列

[[email protected] 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

生成按規則序列{開始..結束..間隔}

[[email protected] ~]# echo {1..10..3}

1 4 7 10

備份一個文件的時候使用

[[email protected] ~]# cp oldboy.txt{,.bak}

[[email protected] ~]# ll oldboy*

-rw-r--r-- 3 root root 241 Aug 30 11:40 oldboy.txt

-rw-r--r-- 1 root root 241 Aug 31 09:38 oldboy.txt.bak

1.2 特殊符號

1.2.1 特殊符號

> 標準輸出重定向,先把內容清空,再向文件裏放其他東西

>> 標準追加重定向 向文件內加內容

< 標準輸入 xargs

<< 追加輸入 cat>/oldboy.txt<<EOF 追加多行

. 當前目錄/linux下面的隱藏文件

.. 當前用戶的上一級目錄

~ 當前用戶的家目錄

/ 路徑的分割符號

\ 臨時取消別名

| 管道

!

1 vim中強制

2 取反 find awk

3 表示使用你用過的命令 使用歷史命令

!可以看歷史命令 history 命令

!ls ===== history |grep ls

[[email protected] ~]# history

# 註釋

$ 取出變量裏的內容

&& 並且 前一個命令運行成功,然後再運行後面的命令

ifdown eth0 && ifup eth0

; 分開多條命令 在同一行裏面放入多個命令。

ls; pwd; hostname

1.2.2 單引號、雙引號、不加引號

‘ ‘

吃啥吐啥

[[email protected] ~]# echo ‘$LANG $(pwd) `hostname` {1..3}‘

$LANG $(pwd) `hostname` {1..3}

" "

把雙引號裏面的特殊符號進行解析

[[email protected] ~]# echo "$LANG $(pwd) `hostname` {1..3}"

en_US.UTF-8 /root znix {1..3}

不加引號

[[email protected] ~]# echo $LANG $(pwd) `hostname` {1..3}

en_US.UTF-8 /root znix 1 2 3

` `

反引號 先運行,把結果留下 $()作用相同

[[email protected] ~]# du -sh `find -type d`

764K .

第2章 正則表達式

2.1 什麽是正則

特殊符號表示文字 文本

^ 開頭

[0-9] 數字

2.2 作用

提高效率 省事

2.3 分類

2.3.1 基礎正則表達式

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

2.3.2 擴展正則表達式

| () + {} ?

2.4 正則表達式與通配符的區別

1、通配符是用來找文件的。

2、正則表達式用來的文件中找內容、文本。

2.5 基礎正則表達式

2.5.1 環境準備

cat -A 在每一行最後加上一個$符號。

[[email protected] ~]# oldboy.txt

I am oldboy teacher!$

I teach linux.$

$

I like badminton ball ,billiard ball and chinese chess!$

my blog is http://oldboy.blog.51cto.com$

$

our site is http://www.etiantian.org$

$

my qq num is 49000448.$

$

not 4900000448.$

my god ,i am not oldbey,but OLDBOY!$

2.5.2 找以m開頭的行 ^

^m 表示以m開頭,^表示以什麽開頭。

[[email protected] ~]# grep "^m" oldboy.txt

my blog is http://oldboy.blog.51cto.com

my qq num is 49000448.

my god ,i am not oldbey,but OLDBOY!

2.5.3 m結尾的行結尾的行 $

m$ 表示以m結尾。

[[email protected] ~]# grep "m$" oldboy.txt

my blog is http://oldboy.blog.51cto.com

2.5.4 顯示空行,並且加上行號

-n 顯示行號

^$ 表示開頭和結尾中間沒有東西,即空行

[[email protected] ~]# grep -n "^$" oldboy.txt

3:

6:

8:

10:

2.5.5 表示任意一個字符 . (點)

點表示任意一個字符,oldb.y 表示點的位置是什麽都可以

[[email protected] ~]# grep "oldb.y" oldboy.txt

I am oldboy teacher!

my blog is http://oldboy.blog.51cto.com

my god ,i am not oldbey,but OLDBOY!

grep -o 顯示grep/egrep執行的過程(每一次找到的東西)。

[[email protected] ~]# grep -o "." oldboy.txt

[[email protected] ~]# grep -o "oldb.y" oldboy.txt

oldboy

oldboy

oldbey

2.5.6 找到以點結尾的行

\ 轉意符號,把特殊含義的的去掉特殊含義。

\.$ 表示以點結尾。

[[email protected] ~]# grep ‘\.$‘ oldboy.txt

I teach linux.

my qq num is 49000448.

not 4900000448.

2.5.7 * 前一個文本連續出現了0次或1次以上

連續出現了0次就是沒出現

-o 顯示grep找到的過程

[[email protected] ~]# grep "0*" oldboy.txt

I am oldboy teacher!

I teach linux.

I like badminton ball ,billiard ball and chinese chess!

my blog is http://oldboy.blog.51cto.com

our site is http://www.etiantian.org

my qq num is 49000448.

not 4900000448.

my god ,i am not oldbey,but OLDBOY!

[[email protected] ~]# grep -o "0*" oldboy.txt

000

00000

2.5.8 正則表達式的貪婪性

有多少要多少,盡可能多的匹配。

2.5.9 .* 表示所有

顯示所有的內容,一次找到。

[[email protected] ~]# grep -o ".*" oldboy.txt

I am oldboy teacher!

I teach linux.

I like badminton ball ,billiard ball and chinese chess!

my blog is http://oldboy.blog.51cto.com

our site is http://www.etiantian.org

my qq num is 49000448.

not 4900000448.

my god ,i am not oldbey,but OLDBOY!

表示所有.* 連續出現的時候會表現貪婪性

[[email protected] ~]# grep "^.*m" oldboy.txt

I am oldboy teacher!

I like badminton ball ,billiard ball and chinese chess!

my blog is http://oldboy.blog.51cto.com

my qq num is 49000448.

my god ,i am not oldbey,but OLDBOY!

2.5.10 [abc] 中括號表示一個整體

相當於一個符號,表示a或者b或者c

[[email protected] ~]# grep "[0-9]" oldboy.txt

[[email protected] ~]# grep "[A-Z]" oldboy.txt

[[email protected] ~]# grep "[a-z]" oldboy.txt

找到文本中的大寫和小寫字母。

[[email protected] ~]# grep "[a-zA-Z]" oldboy.txt

2.5.11 找以 mno開頭的 並且以 mg 結尾的行

.* 表是什麽都可以

^[mno] m no開頭的

[mg]$ m g 結尾

[[email protected] ~]# grep "^[mno].*[mg]$" oldboy.txt

my blog is http://oldboy.blog.51cto.com

our site is http://www.etiantian.org

2.5.12 [^abc] 排除a或排除b或排除c

[^abc] 表示找排除a或排除b或排除c之外的其他字符

[[email protected] ~]# grep "[^abc]" oldboy.txt

I am oldboy teacher!

I teach linux.

I like badminton ball ,billiard ball and chinese chess!

my blog is http://oldboy.blog.51cto.com

our site is http://www.etiantian.org

my qq num is 49000448.

not 4900000448.

my god ,i am not oldbey,but OLDBOY!

2.5.13 grep -v 排除與[^abc]

grep -v 排除行

[^abc] 字符或文字

第3章 昨日回顧(刪除文件、開機自啟動)

3.1 linux如何讓一個服務/腳本開機自啟動?

1)chkconfig

2)/etc/rc.local

3.1.1 chkconfig管理 需要什麽條件

1)必須放在/etc/init.d/

2)這個腳本要有執行權限

3)加上chkconfig要求的內容

# chkconfig: 2345 99 99

4)chkconfig --add 把腳本添加到開機自啟動

5)檢查

3.2 /etc/rc.local

[[email protected] ~]# ls -l /etc/rc3.d/ |grep rc.local

lrwxrwxrwx. 1 root root 11 Aug 10 18:36 S99local -> ../rc.local

3.3 磁盤空間不足 no space left on device

1)block滿了 500G 3*200G視頻

df -h

du -sh /*

du -sh /* |grep G

2)block滿了 文件沒有被徹底刪除 硬鏈接數為0,進程調用數不為零

檢查:lsof|grep delete

3.4 文件的刪除原理(條件)

1、硬鏈接數為0

2、進程調用數為0

日誌

/var/log/messages

/var/log/secure

rsyslog

3inode滿了

創建一個文件要占用一個inode和至少一個block

大量的小文件

Day15 - linux的特殊符號與正則表達式