系統基礎之文件管理工具
linux的重要哲學思想之一,一切皆文件.那作為系統管理員,就要求對文件的操作管理特別熟悉.那么下面介紹的一個工具可以幫助到大家,更有效,快捷的完成對文件的處理.下面讓我們來認識以下的工具.
文本工具:
文件內容:
cat: 復制標準輸入到標準輸出
選項:
-n: 加行號(僅在屏幕顯示,不修改文件)
-s: 將所有的連續的多個空行替換為一個空行。
-T: 把文件中的Tab鍵替換成^I,
cat f1|tr '\t' '\r' > f2(不能對同一個文件操作)
-A: 顯示所有文本特殊符號(換行符,空格符)
技巧:
(1)cat > filename 寫簡單腳本,不用打開文件編寫
(2) 合并多個文件
[root@wen-7 ~]# cat -n /etc/issue 1 #\S 2 #Kernel \r on an \m 3 #$hostname 4 #date [root@wen-7 ~]# cat -s abc a a a b d c [root@wen-7 ~]# cat -A /etc/issue #\S$ #Kernel \r on an \m$ #$hostname$ #date [root@wen-7 ~]# cat -T abc ^Ia ^Ia ^I^Ia ^Ib d c [root@wen-7 ~]# cat > file1 echo "Hello World"[root@wen-7 ~]# bash file1 Hello World [root@wen-7 ~]#
tac: cat的倒寫 反向顯示文件
[root@wen-7 ~]# tac /etc/issue #date #$hostname #Kernel \r on an \m #\S [root@wen-7 ~]# cat /etc/issue #\S #Kernel \r on an \m #$hostname #date [root@wen-7 ~]#
rev: 反向顯示文件中每一個行
[root@wen-7 ~]# rev /etc/issue S\# m\ na no r\ lenreK# emantsoh$# etad#
hexdump:查看二進制文件
選項:
-C:顯示ascll編碼
[root@wen-7 ~]# hexdump /bin/cat 0000000 457f 464c 0102 0001 0000 0000 0000 0000 0000010 0002 003e 0001 0000 2644 0040 0000 0000 [root@wen-7 ~]# hexdump -c /bin/cat 0000000 177 E L F 002 001 001 \0 \0 \0 \0 \0 \0 \0 \0 \0 0000010 002 \0 > \0 001 \0 \0 \0 D & @ \0 \0 \0 \0 \0
more: 翻頁查看文件(在顯示器上閱讀文件的過濾器)
只能向后翻頁,到文件底部自動退出
使用:
末行模式,使用!號執行shell命令
[root@wen-7 ~]# more /etc/passwd root:x:0:0:root,123,123,12345:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin m:x:3:4:adm:/var/adm:/sbin/nologin
less:翻頁查看文件,可上可下
比more靈活
可搜索關鍵字,N n切換下一個被模式匹配到的字符
可配合ls管道使用: ls /etc|less
[root@wen-7 ~]# ls /etc/|less [root@wen-7 ~]# less /etc/passwd
head: 顯示指定范圍的行
-n #: 正數顯示文件前#行
-c #: 正數顯示#個字節
[root@wen-7 ~]# head -n3 /etc/passwd root:x:0:0:root,123,123,12345:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin [root@wen-7 ~]# head -c4 /etc/passwd root
tail:顯示文件后#行
-n #:倒數顯示文件第#行
-c #: 倒數顯示文件最后#個字節
-f: 顯示后不自動退出,自動監控文件更新(日志文件),但占用終端頁面
工作需求: 監控日志文件,只顯示并報警最新日志,但不影響終端頁面,完成其他工作
(1)tail -n 0 -f /var/log/messages & (后端執行) Ctrl+c 退出
[root@wen-7 ~]# tail -n3 /etc/passwd www:x:6012:6017::/home/www:/sbin/nologin user1:x:6013:6013::/home/user1:/bin/bash xing:x:6014:6021::/home/xing:/bin/bash [root@wen-7 ~]# tail -c4 /etc/passwd ash [root@wen-7 ~]# tail -f /var/log/messages #被動監控日志,不能退出文件 Aug 5 20:55:02 wen-7 systemd: Started Session 317 of user pcp. Aug 5 20:55:02 wen-7 systemd: Starting Session 317 of user pcp. Aug 5 20:55:02 wen-7 systemd: Removed slice user-988.slice [root@wen-7 ~]# tail -n 0 -f /var/log/messages & #自動監控日志文件,新條目自動顯示 [1] 77572 [root@wen-7 ~]# Aug 5 20:58:47 wen-7 vmsvc[1318]: [ warning] [guestinfo] Failed to get disk info. Aug 5 20:59:17 wen-7 vmsvc[1318]: [ warning] [guestinfo] Failed to get disk info. ^C [root@wen-7 ~]# jobs [1]+ 運行中 tail -n 0 -f /var/log/messages &
文件截取:
cut: [options]..[file]..
-d DELIMIITER:指明分隔符,默認tab
-f FILEDS:
#:指定的單個字段,(第#個字段)
#,#[#]:離散的多個字段.例如1.3.6
#-#:連續的多個字段 例如:1-6
混合使用:1-3,7
例:cut -d:-f1,7 /etc/passwd
cut -d: -f1,3-5,7 /etc/passwd
-c:字符數 例:df -h|cut -c44-46
–output-delimiter= DELIMITER: 指定分隔符替代現有的分隔符,并顯示出來
[root@wen-7 ~]# cut -d: -f1 /etc/passwd root bin daemon adm [root@wen-7 ~]# cut -d: -f1,3,7 /etc/passwd root:0:/bin/bash bin:1:/sbin/nologin daemon:2:/sbin/nologin [root@wen-7 ~]# cut -d: -f1,3-5,7 /etc/passwd root:0:0:root,123,123,12345:/bin/bash bin:1:1:bin:/sbin/nologin daemon:2:2:daemon:/sbin/nologin adm:3:4:adm:/sbin/nologin lp:4:7:lp:/sbin/nologin [root@wen-7 ~]# df -h| cut -c44-46 29 0 0
練習;
1.取出ifconfig中的ip
[root@wen-7 ~]# ifconfig eno16777736| head -2|tail -n1|tr -s " "|cut -d" " -f3 172.18.19.219
2.df -h的磁盤利用率
[root@wen-7 ~]# df -h | tail -n5 |tr -s " "| cut -d" " -f5
100%
擴展:
nmap -sP -v 網段 :掃描本網段主機的狀態
文件合并:
cat file1 file2 …: 合并多個文件到一列
[root@wen-7 ~]# cat /etc/issue file1 #\S #Kernel \r on an \m #$hostname #date echo "Hello World"[root@wen-7 ~]#
paste: 合并多個文件到一行
默認: paste file1 file2….
-d DELIMIITER :分隔符
-s:每個文件橫向輸出
[root@wen-7 ~]# paste -d"##" /etc/issue file1 #\S#echo "Hello World" #Kernel \r on an \m# #$hostname# #date# [root@wen-7 ~]# paste -d"##" /etc/issue file1
[root@wen-7 ~]# paste -s /etc/issue file1 #\S #Kernel \r on an \m #$hostname #date echo "Hello World"
文件統計:
wc:統計文本工具
選項:
wc filename: 全部顯示 (行數,單詞數,字節數,文件名)
-l:lines 行數
-w:words 單詞數
-C:bytes 字節數 (漢字占三個字節)
-m:字符數
默認使用命令,輸出一段字符串 最后寫abc ctrl+d顯示統計信息
[root@wen-7 ~]# wc dsfhidh sdafdsif sdfkdls fdsjfhdjk jdsifjksdld kdlsafkld dfdsofjodfjdso a b c 3 10 78
[root@wen-7 ~]# wc file1 0 3 18 file1 [root@wen-7 ~]# wc -l file1 0 file1 [root@wen-7 ~]# wc -w file1 3 file1 [root@wen-7 ~]# wc -c file1 18 file1 [root@wen-7 ~]# wc -m file1 18 file1 [root@wen-7 ~]# cat /etc/issue | wc -l #配合其他命令,用管道連接 4
文件排序:
sort: 排序工具
默認按字母排列方式
-n:基于數值大小而非字符進行排序 配合-k使用
-t CHAR:指定分隔符;
-k #:用于指定排序比較的字段
-r:逆序排序
-f:忽略字符大小寫
-u:重復的行只保留一份
重復行:連續且相同
例:
(1)[root@wen-7 ~]# cat /etc/passwd| sort -r -n -t: -k3 排序 (2)root@wen-7 ~]# cat abc 刪除重復 a a a b d c [root@wen-7 ~]# cat abc| sort -u a b c d
uniq:移除和報告重復的行
-c:統計重復行的次數
-u:只顯示不重復的行
-d:僅顯示重復的行
例:
[root@wen-7 ~]#uniq -c abc 4 a 1 b 1 d 1 c
先對文件排序,在統計重復的行,與sort配合使用
例:
[root@wen-7 ~]# cat /etc/init.d/functions |tr -cs '[[:alpha:]]' '\n'| sort | uniq -c|sort -n 51 echo 54 file 55 if 67 pid 94 ] 107 [
文件檢查:
diff:逐行比較文件的不同
-u:使用unfied機制,即顯示要修改的上下文默認3行
diff /PATH/OLDFILR /PATH/TO/CONF_FILE > /PATH/TO/CONF_FILE
patch:向文件打補丁
patch [OPTIONS] -i /PATH/TO/PATCH_FILE /PATH/TO/OLDFILE
patch /PATH/TO/OLDFILE >/PATH/TO/PATCH_FILE
正則表達式: (Regual Expression REGEXP)
由一類特殊字符及文本字符所編寫的模式,其中有些字符不表示其字面意義,而是表示控制或通配的功能。
分為兩類:(元字符不同)
基本正則表達式:BRE
擴展正則表達式:
元字符:不能被整體切割,代表著通配或控制的字符. 對兩種表達式的表示不同
grep:
Global search REgular expression and print out the line
作用:文本搜素工具,根據用戶指定的模式(過濾條件),對目標文本逐行進行匹配檢查, 打印匹配得到的行顯示到屏幕
模式:有正則表達式的元字符及文本字符所編寫出的過濾條件
正則表達式引擎: 工具不同,引擎不同
grep分類: 支持選項切換
grep:基本正則表達式 -E -F
egrep:擴展正則表達式 -G -F
fgrep:不支持正則表達式
語法:
grep [options] PATTERN [FILE…]
grep [options] [-e PATTERN | -f FILE] [FILE…]
選項:
–color=auto; 對匹配到的字符高亮顯示 可以alias定義grep
-i:忽略字符大小寫
-o:僅顯示匹配到的字符串本身
-v:顯示不能被匹配到的行、 取反的意思
-E:支持使用擴展正則表達式元字符
-q:靜默模式 不輸出任何信息 只關注執行狀態返回值
-A #:after,顯示被匹配的行的后#行
-B #:before,顯示被匹配的行的前#行
-C #:context,顯示被匹配的行的前后各#行
-n:顯示被匹配到的行號
-c:統計被匹配的行的行數
-e:或, 實現多條表達式共同實行
-w:整行匹配整個單詞;
基本正則表達式元字符(basic):
定義:有一類特殊字符集文本多編寫的模式,其中有些字符(元字符)不表示字符字面意義,而表示控制或通配的功能.
程序支持:grep,vim,
字符匹配:
. :匹配任意單個字符 例:r..t
[]: 匹配制定范圍內的任意單個字符
[^]:匹配制定范圍外的任意單符
[:space:] [:lower:] [:alpha:] [:digit:][:upper:][:punct:][:alnum:]
匹配次數:
用在要指定其出現的次數的字符的后面,用來限制其前面字符出現的次數
默認工作于貪婪模式(能匹配多少就匹配多少),
*:匹配其前面的字符任意次;0,1或多次
.*:匹配任意長度的任意字符
\?:匹配其前面的字符0次或1次,即前面的字符也是可有可無的
\+:匹配其前面的字符1次或多次,即前面的字符出現至少1次
\{m}:匹配前面字符m次
\{m,n\}:匹配其前面的字符至少m次,至多n次。
\{0.n\}:至多n次
\{m,\}:至少m次。
[root@wen-7 ~]# grep "x\?y" abc bxy xxxxxy yaba aby [root@wen-7 ~]# grep "x*y" abc bxy xxxxxy yaba aby [root@wen-7 ~]# grep "x\+y" abc bxy xxxxxy [root@wen-7 ~]# grep "x.*y" abc bxy xxxxxy [root@wen-7 ~]# grep "x\{1\}y" abc bxy
[root@wen-7 ~]# grep "x\{1,2\}y" abc bxy xxxxxy [root@wen-7 ~]# grep "x\{1,3\}y" abc bxy xxxxxy [root@wen-7 ~]# grep "x\{0,2\}y" abc bxy xxxxxy yaba aby [root@wen-7 ~]# grep "x\{0,5\}y" abc bxy xxxxxy yaba aby [root@wen-7 ~]# grep "x\{3\}y" abc xxxxxy
位置錨點:
^:行首錨定 用于模式的最左側
$:行尾錨定 用于模式的最右側
^PATTERN$:用于PATTERN來匹配整行
^$:空白行
^[[:space:]]*$ 空行或包含空白字符的行
\<或\b:詞首錨定
\>或\b:詞尾錨定
\<PATTERN>:匹配完整單詞
[root@wen-7 ~]# grep ^root /etc/passwd #行首 root:x:0:0:root,123,123,12345:/root:/bin/bash rooter:x:0:6011::/home/rooter:/bin/bash rootkit:x:6015:6022::/home/rootkit:/bin/bash [root@wen-7 ~]# grep "root$" /etc/passwd #行尾 user5:x:6016:6023::/home/user5:/bin/chroot [root@wen-7 ~]# grep "\<root" /etc/passwd #單詞首部錨定 root:x:0:0:root,123,123,12345:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin rooter:x:0:6011::/home/rooter:/bin/bash rootkit:x:6015:6022::/home/rootkit:/bin/bash [root@wen-7 ~]# grep "root\>" /etc/passwd #單詞尾部錨定 root:x:0:0:root,123,123,12345:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin user5:x:6016:6023::/home/user5:/bin/chroot [root@wen-7 ~]# grep "\<root\>" /etc/passwd #單詞整個錨定 root:x:0:0:root,123,123,12345:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@wen-7 ~]# grep "^$" /etc/rc.d/init.d/functions #空白行 [root@wen-7 ~]# grep "^[[:space:]]*$" /etc/rc.d/init.d/functions #空行或包含空白字符的行
分組及引用:
分組:\(\);將一個或多個字符捆綁在一起,當作一個整體來處理;
例:\(xy\)*ab
向后引用: 引用前面的分組括號中的模式所匹配的字符。
注意:分組括號中的模式匹配到的內容會被正則表達式引擎自動記錄于內部的變量中,這些變量為:
\1:模式從左側起,第一個左括號以及與之匹配的右括號之間的模式所匹配到的字符;
\2: 第二個
\3: 第三個
……
實例: \(string1\+\(string2\)*\)
\1: string1\+\(string2\)*
\2: string2
例:[root@wen-7 ~]# cat abc He loves his lover He likes his lover She likes her liker She lovers her liker [root@wen-7 ~]# cat abc| grep "\(l..e\).*\1" He loves his lover She likes her liker
[root@wen-7 ~]# grep "^\(r..t\).*\1" /etc/passwd root:x:0:0:root,123,123,12345:/root:/bin/bash rooter:x:0:6011::/home/rooter:/bin/bash rootkit:x:6015:6022::/home/rootkit:/bin/bash
練習1:
1.顯示/etc/passwd/文件中不以/bin/bash結尾的行;
[root@wen-7 ~]# grep -v "bash$" /etc/passwd bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin
2.找出/etc/passwd文件中兩為數或三位數
[root@wen-7 ~]# cat /etc/passwd | grep "\<[[:digit:]]\{2,3\}\>" root:x:0:0:root,123,123,12345:/root:/bin/bash mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin
3.找出/etc/rc.d/init文件中,以至少一個空白字符開頭,且后面非空白自的行
[root@wen-7 ~]# cat /etc/rc.d/init.d/functions |grep "^[[:space:]]\+[^[:space:]]" ( /bin/mountpoint -q /cgroup/systemd || /bin/mountpoint -q /sys/fs/cgroup/systemd ) ; then case "$0" in /etc/init.d/*|/etc/rc.d/init.d/*)
4.找出"netstat -tan"命令中的結果以"LISTEN"后跟0,1或多個空白字符結尾的行
[root@wen-7 ~]# netstat -tan| grep "LISTEN[[:space:]]*$" tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 額n
egrep:
支持擴展的正則表達式實現類似于grep文本過濾功能:等同于grep -E
語法:egrep [options] PATTREN [file…]
選項:
–color=auto; 對匹配到的字符高亮顯示 可以alias定義grep
-i:忽略字符大小寫
-o:僅顯示匹配到的字符串本身
-v:顯示不能被匹配到的行、 取反的意思
-E:支持使用擴展正則表達式元字符
-q:靜默模式 不輸出任何信息 只關注執行狀態返回值
-A #:after,顯示被匹配的行的后#行
-B #:before,顯示被匹配的行的前#行
-C #:context,顯示被匹配的行的前后各#行
-n:顯示被匹配到的行號
-c:統計被匹配的行的行數
-e:或, 實現多條表達式共同實行
-w:整行匹配整個單詞;
-G:支持基本正則表達式
擴展正則表達式的元字符:
字符匹配:
.:任意單個字符
[]:指定范圍內的任意單個字符
[^]:指定范圍外的任意單個字符
次數匹配:(不用反斜線的轉義,可以去掉反斜號)
*:任意次,0,1或多次;
?:0或1次,其前的字符可有可無;
+:其前字符至少1次;
{m}:其前的字符m次
{m,n}:至少m次,至多n次
{0,n};最多n次
{m,};最少m次
位置錨定:(必須反斜號轉義)
^: 行首
$: 行尾
\<,\b 行首錨定
\>,\b 行尾錨定
分組及引用:
分組: ():括號內的模式匹配的字符會被記錄正則表達式引擎的內部變量總
后向引用:\1,\2
或:
a|b:a或b
C|cat :C或cat
(c|C)at:cat或Cat
擴展練習:
1.找出/etc/passwd文件中兩為數或三位數
[[root@wen-7 ~]# grep -E "\<[0-9]{2,3}" /etc/passwd root:x:0:0:root,123,123,12345:/root:/bin/bash mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin
2.找出/etc/rc.d/init文件中,以至少一個空白字符開頭,且后面非空白自的行 (+代替\+)
[root@wen-7 ~]# cat /etc/rc.d/init.d/functions |egrep "^[[:space:]]+[^[:space:]]" ( /bin/mountpoint -q /cgroup/systemd || /bin/mountpoint -q /sys/fs/cgroup/systemd ) ; then case "$0" in /etc/init.d/*|/etc/rc.d/init.d/*)
3 找出/proc/meminfo文件下,所有以大寫或小寫S開頭的行,至少三種方式
[root@wen-7 ~]# egrep "^(s|S)" /proc/meminfo SwapCached: 40 kB SwapTotal: 2097148 kB [root@wen-7 ~]# egrep -i "^s" /proc/meminfo SwapCached: 40 kB SwapTotal: 2097148 kB [root@wen-7 ~]# grep "^[sS]" /proc/meminfo SwapCached: 40 kB SwapTotal: 2097148 kB
4.顯示當前系統上root,centos或use1用戶的相關信息
[root@wen-7 ~]# grep -E "^(root|centos|user1)\>" /etc/passwd root:x:0:0:root,123,123,12345:/root:/bin/bash user1:x:6013:6013::/home/user1:/bin/bash
5. 找出/etc/rc.d/init.d/functions文件中某單詞(包括下劃線)后面跟一個小括號的行
[root@wen-7 ~]# cat /etc/rc.d/init.d/functions | egrep "[[:alnum:]_]+\(\)"
6.使用echo命令輸出一絕對路徑,使用egrep取出基名
[root@wen-7 ~]# echo "/etc/rc.d/init/" | egrep -o "[^/]+/?$" init/
7.取出ifconfig中1-255的數值
[root@wen-7 ~]# ifconfig | egrep -o "[1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]" 167 77 73 6
8.找出/etc/passwd 文件中用戶名同shell名的行
[root@wen-7 ~]# cat /etc/passwd | egrep -o "^([^:]+\>).*\1$" sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 位于行首 除:外的單詞(詞尾錨定) 9.找出/etc/passwd文件中的最后一個單詞 [root@wen-7 ~]# cat /etc/passwd | egrep -o "(\<[^/]+$)" bash nologin nologin 除 / 外位于行尾的單詞
fgrep:不支持正則表達式元字符
當無需用到元字符去編寫模式時,使用fgrep性能更好;
原創文章,作者:wencx,如若轉載,請注明出處:http://www.www58058.com/29794