Linux中的特殊符號與正則表達(dá)式
第1章 linux的特殊符號
1.1 通配符 * {}
1.1.1 含義
方便查找文件 通配符是用來找文件名字的。
1.1.2 *
通過find 命令找以 .sh 結(jié)尾的文件,使用*替代文件名字。
find /oldboy -type f -name "*.sh" -mtime +7 -size +100k -size -10M
查找文件名中,包含有oldboy字節(jié)的文件。
[root@znix 20170118]# find -type f -name "*oldboy*" [root@znix 20170118]# ls -l *oldboy*
1.1.3 {}
{} 用來生成序列
[root@znix 20170118]# echo oldboy{1..3}.txt oldboy1.txt oldboy2.txt oldboy3.txt [root@znix 20170118]# echo {a,c,d,f}
a c d f
echo {a..z} {A..Z} 中間需要有空格,表示兩個(gè)無關(guān)的序列
[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
生成按規(guī)則序列{開始..結(jié)束..間隔}
[root@znix ~]# echo {1..10..3}
1 4 7 10
備份一個(gè)文件的時(shí)候使用
[root@znix ~]# cp oldboy.txt{,.bak} [root@znix ~]# 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 特殊符號
> 標(biāo)準(zhǔn)輸出重定向,先把內(nèi)容清空,再向文件里放其他東西
>> 標(biāo)準(zhǔn)追加重定向 向文件內(nèi)加內(nèi)容
< 標(biāo)準(zhǔn)輸入 xargs
<< 追加輸入 cat>/oldboy.txt<<EOF 追加多行
. 當(dāng)前目錄/linux下面的隱藏文件
.. 當(dāng)前用戶的上一級目錄
~ 當(dāng)前用戶的家目錄
/ 根 路徑的分割符號
\ 臨時(shí)取消別名
| 管道
!
1) vim中強(qiáng)制
2) 取反 find awk
3) 表示使用你用過的命令 使用歷史命令
!可以看歷史命令 history 命令
!ls ===== history |grep ls
[root@znix ~]# history
# 注釋
$ 取出變量里的內(nèi)容
&& 并且 前一個(gè)命令運(yùn)行成功,然后再運(yùn)行后面的命令
ifdown eth0 && ifup eth0
; 分開多條命令 在同一行里面放入多個(gè)命令。
ls; pwd; hostname
1.2.2 單引號、雙引號、不加引號
' '
吃啥吐啥
[root@znix ~]# echo '$LANG $(pwd) `hostname` {1..3}' $LANG $(pwd) `hostname` {1..3}
" "
把雙引號里面的特殊符號進(jìn)行解析
[root@znix ~]# echo "$LANG $(pwd) `hostname` {1..3}" en_US.UTF-8 /root znix {1..3}
不加引號
[root@znix ~]# echo $LANG $(pwd) `hostname` {1..3} en_US.UTF-8 /root znix 1 2 3
` `
反引號 先運(yùn)行,把結(jié)果留下 與$()作用相同
[root@znix ~]# du -sh `find -type d`
764K .
第2章 正則表達(dá)式
2.1 什么是正則
特殊符號表示文字 文本
^ 開頭
[0-9] 數(shù)字
2.2 作用
提高效率 省事
2.3 分類
2.3.1 基礎(chǔ)正則表達(dá)式
^ $ ^$ . * .* [0-9] [^0-9]
2.3.2 擴(kuò)展正則表達(dá)式
| () + {} ?
2.4 正則表達(dá)式與通配符的區(qū)別
1、通配符是用來找文件的。
2、正則表達(dá)式用來的文件中找內(nèi)容、文本。
2.5 基礎(chǔ)正則表達(dá)式
2.5.1 環(huán)境準(zhǔn)備
cat -A 在每一行最后加上一個(gè)$符號。
[root@znix ~]# 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開頭,^表示以什么開頭。
[root@znix ~]# 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結(jié)尾的行結(jié)尾的行 $
m$ 表示以m結(jié)尾。
[root@znix ~]# grep "m$" oldboy.txt my blog is http://oldboy.blog.51cto.com
2.5.4 顯示空行,并且加上行號
-n 顯示行號
^$ 表示開頭和結(jié)尾中間沒有東西,即空行
[root@znix ~]# grep -n "^$" oldboy.txt
3:
6:
8:
10:
2.5.5 表示任意一個(gè)字符 . (點(diǎn))
點(diǎn)表示任意一個(gè)字符,oldb.y 表示點(diǎn)的位置是什么都可以 。
[root@znix ~]# 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執(zhí)行的過程(每一次找到的東西)。
[root@znix ~]# grep -o "." oldboy.txt [root@znix ~]# grep -o "oldb.y" oldboy.txt oldboy oldboy oldbey
2.5.6 找到以點(diǎn)結(jié)尾的行
\ 轉(zhuǎn)意符號,把特殊含義的的去掉特殊含義。
\.$ 表示以點(diǎn)結(jié)尾。
[root@znix ~]# grep '\.$' oldboy.txt I teach linux. my qq num is 49000448. not 4900000448.
2.5.7 * 前一個(gè)文本連續(xù)出現(xiàn)了0次或1次以上
連續(xù)出現(xiàn)了0次就是沒出現(xiàn)
-o 顯示grep找到的過程
[root@znix ~]# 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! [root@znix ~]# grep -o "0*" oldboy.txt 000 00000
2.5.8 正則表達(dá)式的貪婪性
有多少要多少,盡可能多的匹配。
2.5.9 .* 表示所有
顯示所有的內(nèi)容,一次找到。
[root@znix ~]# 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!
表示所有.* 連續(xù)出現(xiàn)的時(shí)候會(huì)表現(xiàn)貪婪性。
[root@znix ~]# 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] 中括號表示一個(gè)整體
相當(dāng)于一個(gè)符號,表示a或者b或者c。
[root@znix ~]# grep "[0-9]" oldboy.txt [root@znix ~]# grep "[A-Z]" oldboy.txt [root@znix ~]# grep "[a-z]" oldboy.txt
找到文本中的大寫和小寫字母。
[root@znix ~]# grep "[a-zA-Z]" oldboy.txt
2.5.11 找以 m或n或o開頭的 并且以 m或g 結(jié)尾的行
.* 表是什么都可以
^[mno] m 或n或o開頭的
[mg]$ m 或g 結(jié)尾
[root@znix ~]# 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之外的其他字符 [root@znix ~]# 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章 昨日回顧(刪除文件、開機(jī)自啟動(dòng))
3.1 linux如何讓一個(gè)服務(wù)/腳本開機(jī)自啟動(dòng)?
1)chkconfig
2)/etc/rc.local
3.1.1 被chkconfig管理 需要什么條件
1)必須放在/etc/init.d/
2)這個(gè)腳本要有執(zhí)行權(quán)限
3)加上chkconfig要求的內(nèi)容
# chkconfig: 2345 99 99
4)chkconfig --add 把腳本添加到開機(jī)自啟動(dòng)
5)檢查
3.2 /etc/rc.local
[root@znix ~]# 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滿了 文件沒有被徹底刪除 硬鏈接數(shù)為0,進(jìn)程調(diào)用數(shù)不為零
檢查:lsof|grep delete
3.4 文件的刪除原理(條件)
1、硬鏈接數(shù)為0
2、進(jìn)程調(diào)用數(shù)為0
日志
/var/log/messages /var/log/secure rsyslog
3、inode滿了
創(chuàng)建一個(gè)文件要占用一個(gè)inode和至少一個(gè)block
大量的小文件
總結(jié)
以上所述是小編給大家介紹的Linux中的特殊符號與正則表達(dá)式,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
shell腳本中執(zhí)行python腳本并接收其返回值的例子
這篇文章主要介紹了shell腳本中執(zhí)行python腳本并接收其返回值的例子,本文重點(diǎn)在于如何接收python腳本的返回值,需要的朋友可以參考下2014-08-08Linux shell下30個(gè)有趣的命令和提示(推薦)
這些是我收集了多年的Linux shell的30個(gè)有趣的命令和提示。特此分享到腳本之家平臺,供大家參考2017-10-10Linux shell實(shí)現(xiàn)HTTP服務(wù)示例代碼
這篇文章介紹的是如何實(shí)現(xiàn)一個(gè)可以調(diào)用shell腳本的HTTP服務(wù),本文給出了詳細(xì)的示例代碼,有需要的可以參考借鑒。2016-08-08