文件查看命令使用示例
cat file1 #從第一個(gè)字節(jié)開始正向查看文件的內(nèi)容
tac file1 #從最后一行開始反向查看一個(gè)文件的內(nèi)容
more file1 #查看一個(gè)長文件的內(nèi)容
less file1 #類似于 'more' 命令修然,但是它允許在文件中和正向操作一樣的反向操作
head -2 file1 #查看一個(gè)文件的前兩行
tail -2 file1 #查看一個(gè)文件的最后兩行
tail -f /var/log/messages #實(shí)時(shí)查看被添加到一個(gè)文件中的內(nèi)容
grep命令
grep命令是非常重要的命令捕仔,可以對(duì)文本進(jìn)行查找和搜索 ,grep是Global Regular Expression Pattern 的縮寫。我們可以把grep這個(gè)詞分開理解成'g'形葬,'re'切黔,'p',三個(gè)單詞縮寫是偷,分別代表global(全局的)拳氢, regular expresssion(正則表達(dá)式), pattern(模式)晓猛,也就是說以全局的正則表達(dá)式的模式去查找饿幅。
ps -ef | grep svn 查找指定進(jìn)程
ps -ef | grep svn -c 查找指定進(jìn)程個(gè)數(shù)
常用參數(shù)如下:
-a #不要忽略二進(jìn)制數(shù)據(jù)
-A #除了顯示符合范本樣式的那一行之外,并顯示該行之后的內(nèi)容
-b #在顯示符合范本樣式的那一行之外戒职,并顯示該行之前的內(nèi)容
-B #除了顯示符合樣式的那一行之外栗恩,并顯示該行之前的內(nèi)容
-c #計(jì)算符合范本樣式的列數(shù)
-C #除了顯示符合范本樣式的那一列之外,并顯示該列之前后的內(nèi)容
-d #當(dāng)指定要查找的是目錄而非文件時(shí)洪燥,必須使用這項(xiàng)參數(shù)磕秤,否則grep命令將回報(bào)信息并停止動(dòng)作
-e #指定字符串作為查找文件內(nèi)容的范本樣式
-E #將范本樣式為延伸的普通表示法來使用,意味著使用能使用擴(kuò)展正則表達(dá)式
-f #指定范本文件捧韵,其內(nèi)容有一個(gè)或多個(gè)范本樣式市咆,讓grep查找符合范本條件的文件內(nèi)容,格式為每一列的范本樣式
-F #將范本樣式視為固定字符串的列表
-G #將范本樣式視為普通的表示法來使用
-h #在顯示符合范本樣式的那一列之前再来,不標(biāo)示該列所屬的文件名稱
-H #在顯示符合范本樣式的那一列之前蒙兰,標(biāo)示該列的文件名稱
-i #忽略字符大小寫的差別
-l #列出文件內(nèi)容符合指定的范本樣式的文件名稱
-L #列出文件內(nèi)容不符合指定的范本樣式的文件名稱
-n #在顯示符合范本樣式的那一列之前,標(biāo)示出該列的編號(hào)
-q #不顯示任何信息
-R/-r #此參數(shù)的效果和指定“-d recurse”參數(shù)相同
-s #不顯示錯(cuò)誤信息
-v #反轉(zhuǎn)查找
-V #顯示版本信息
-w #只顯示全字符合的列
-x #只顯示全列符合的列
-y #此參數(shù)效果跟“-i”相同
-o #只輸出文件中匹配到的部分
正則表達(dá)式
^ #匹配以XX開頭的行
$ #匹配以XX結(jié)尾的行
常用示例:
# 在多個(gè)文件中查找:
grep "file" file_1 file_2 file_3
# 輸出除之外的所有行 -v 選項(xiàng):
grep -v "file" file_name
# 標(biāo)記匹配顏色 --color=auto 選項(xiàng):
grep "file" file_name --color=auto
# 使用正則表達(dá)式 -E 選項(xiàng):
grep -E "[1-9]+"
egrep "[1-9]+"
# 只輸出文件中匹配到的部分 -o 選項(xiàng):
echo this is a test line. | grep -o -E "[a-z]+\."
line.
echo this is a test line. | egrep -o "[a-z]+\."
line.
# 統(tǒng)計(jì)文件或者文本中包含匹配字符串的行數(shù)-c 選項(xiàng):
grep -c "text" file_name
2
# 輸出包含匹配字符串的行數(shù) -n 選項(xiàng):
grep "text" -n file_name
或
cat file_name | grep "text" -n
# 多個(gè)文件
grep "text" -n file_1 file_2
# 搜索多個(gè)文件并查找匹配文本在哪些文件中:
grep -l "text" file1 file2 file3...
# grep遞歸搜索文件芒篷,在多級(jí)目錄中對(duì)文本進(jìn)行遞歸搜索:
grep "text" . -r -n
# 忽略匹配樣式中的字符大小寫:
echo "hello world" | grep -i "HELLO"
hello
# 選項(xiàng) -e 指定多個(gè)匹配樣式:
echo this is a text line | grep -e "is" -e "line" -o
is
line
# 也可以使用 **-f** 選項(xiàng)來匹配多個(gè)樣式搜变,在樣式文件中逐行寫出需要匹配的字符。
cat patfile
aaa
bbb
echo aaa bbb ccc ddd eee | grep -f patfile -o
# 在grep搜索結(jié)果中包括或者排除指定文件:只在目錄中所有的.php和.html文件中遞歸搜索字符"main()"
grep "main()" . -r --include *.{php,html}
# 在搜索結(jié)果中排除所有README文件
grep "main()" . -r --exclude "README"
# 在搜索結(jié)果中排除filelist文件列表里的文件
grep "main()" . -r --exclude-from filelist
which命令
which针炉,whereis挠他,locate命令
這三個(gè)命令都用于搜索文件路徑,使用示例
locate *.ps 尋找以 '.ps' 結(jié)尾的文件 - 先運(yùn)行 'updatedb' 命令
whereis halt 顯示一個(gè)二進(jìn)制文件篡帕、源碼或man的位置
which halt 顯示一個(gè)二進(jìn)制文件或可執(zhí)行文件的完整路徑
which命令用于用戶查找命令所有路徑殖侵,這里顯示的時(shí)絕對(duì)路徑贸呢。同時(shí)which命令也可以看到某個(gè)系統(tǒng)命令是否存在。
語法格式
which [options] [--] programname [...]
選項(xiàng)說明
-a #打印每個(gè)匹配文件名的所有匹配路徑名
-V #打印版本信息
退出狀態(tài)說明
0 #找到了所有文件名拢军,所有文件都是可執(zhí)行的
1 #找不到一個(gè)或多個(gè)文件名楞陷,或者文件名不可執(zhí)行
2 #指定的選項(xiàng)無效
應(yīng)用舉例
# 搜索ls
[root@localhost sysconfig]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@centos7 ~]# which which
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
/usr/bin/alias
/usr/bin/which
[root@centos7 ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@centos7 ~]# which pwd
/usr/bin/pwd
[root@centos7 ~]# which rz
/usr/bin/rz
[root@centos7 ~]# which ifconfig
/usr/sbin/ifconfig
whereis命令
whereis命令相對(duì)于which命令,whereis命令搜索的范圍更大朴沿,它可以查找二進(jìn)制程序猜谚、代碼相關(guān)文件路徑。
和find命令命令比赌渣,whereis查找非澄呵Γ快,因?yàn)長inux將系統(tǒng)里所有文件記錄在一個(gè)數(shù)據(jù)庫文件中坚芜,whereis是直接從數(shù)據(jù)庫文件中查找览芳。而find命令是遍歷硬盤來查找,所以whereis會(huì)比find快鸿竖。
語法格式
whereis [ OPTIONS ] file name...
選項(xiàng)說明
-b #只查找二進(jìn)制文件
-B<目錄> #只在指定的目錄下查找二進(jìn)制文件
-f #不顯示文件名前的路徑名稱
-m #只查找說明文件
-M<目錄> #只在指定的目錄下查找說明文件
-s #只查找原始代碼文件
-S<目錄> #只在指定的目錄下查找原始代碼文件
-u #查找不包含指定類型的文件
應(yīng)用舉例
#將相關(guān)的所有文件都查找出來
[root@centos7 ~]# whereis ifconfig
ifconfig: /usr/sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz
[root@centos7 ~]# whereis top
top: /usr/bin/top /usr/share/man/man1/top.1.gz
#只將二進(jìn)制文件查找出來
[root@centos7 ~]# whereis -b ifconfig
ifconfig: /usr/sbin/ifconfig
[root@centos7 ~]# whereis -b top
top: /usr/bin/top
locate命令
locate命令的搜索不特定于命令或者軟件包沧竟,而是能夠找到任何類型的文件。如果沒有該命令的話缚忧,需要通過apt-get install locate或者yum install locate命令安裝悟泵。
file命令
file命令用于獲取文件屬性,查看指定文件的類型闪水。
這里再回顧一下linux的文件類型:
普通文件 #屬性信息表示為 -
目錄文件 #屬性信息表示為 d
鏈接文件 #屬性信息表示為 l
套接字文件 #屬性信息表示為 s
字符設(shè)備文件 #屬性信息表示為 b
塊設(shè)備文件 #屬性信息表示為 c
管道文件 #屬性信息表示為 p
命令格式
file [選項(xiàng)] [文件名或目錄名]
選項(xiàng)說明
-b:#列出結(jié)果糕非,但不顯示文件名稱
-c:#詳細(xì)顯示指令執(zhí)行過程
-L:#顯示鏈接文件的源文件類型
-m<魔法數(shù)字文件>:#指定魔法數(shù)字文件
-v:#打印出版本信息
-z:#查看壓縮文件的內(nèi)容
應(yīng)用示例
[root@localhost sysconfig]# file sshd
sshd: ASCII text
#查看文件類型
[root@centos7 testdir]# file cp
cp: symbolic link to `/usr/bin/cp`
[root@centos7 testdir]# file dir
dir: directory
[root@centos7 testdir]# file test2.txt
test2.txt: empty
[root@centos7 testdir]# file test2.txt~
test2.txt~: empty
#直接顯示結(jié)果,不顯示文件名
[root@centos7 testdir]# file -b dir
directory
#解讀壓縮文件的內(nèi)容
[root@centos7 ~]# file -z httpd-2.4.46.tar.gz
httpd-2.4.46.tar.gz: POSIX tar archive (GNU) (gzip compressed data, was "httpd-2.4.46.tar", from Unix, last modified: Sat Aug 1 10:12:01 2020, max compression)
nl命令
nl命令用于顯示行號(hào)
nl [-bnw] 文件
選項(xiàng)與參數(shù):
-b: 指定行號(hào)指定的方式球榆,主要有兩種:
-b a :表示不論是否為空行朽肥,也同樣列出行號(hào)(類似 cat -n);
-b t :如果有空行持钉,空的那一行不要列出行號(hào)(默認(rèn)值)衡招;
-n :列出行號(hào)表示的方法,主要有三種:
-n ln :行號(hào)在熒幕的最左方顯示每强;
-n rn :行號(hào)在自己欄位的最右方顯示始腾,且不加 0 ;
-n rz :行號(hào)在自己欄位的最右方顯示空执,且加 0 窘茁;
-w :行號(hào)欄位的占用的位數(shù)。
more命令
more命令用于分頁顯示文件內(nèi)容
more file1 查看一個(gè)長文件的內(nèi)容脆烟,支持一頁一頁翻動(dòng)
快捷健
morm命令運(yùn)行的時(shí)候,支持以下幾個(gè)按鍵操作:
空白鍵 (space):代表向下翻一頁房待;
Enter:代表向下翻『一行』邢羔;
/字串:代表在這個(gè)顯示的內(nèi)容當(dāng)中驼抹,向下搜尋『字串』這個(gè)關(guān)鍵字;
:f:立刻顯示出檔名以及目前顯示的行數(shù)拜鹤;
q:代表立刻離開 more 框冀,不再顯示該文件內(nèi)容。
b 或 [ctrl]-b :代表往回翻頁敏簿,不過這動(dòng)作只對(duì)文件有用明也,對(duì)管線無用。
H 鍵 #顯示幫助信息
| #輸入一個(gè)模式惯裕,可以在當(dāng)前文本內(nèi)容查找一個(gè)相匹配的模式
命令格式
more [選項(xiàng)] [文件名]
more [option] [file ...]
選項(xiàng)說明
-<數(shù)字> #指定每頁顯示的行數(shù)
-c #不進(jìn)行滾屏操作
-s #將多個(gè)空行壓縮成一行顯示
-u #去除下劃線
+<數(shù)字> #從指定的行開始顯示
應(yīng)用舉例
#查看文件mingongge的內(nèi)容温数,在顯示之前先清屏并且顯示當(dāng)前頁數(shù)的百分比
more -dc mingongge
#顯示文件mingongge的內(nèi)容,每100行顯示一次蜻势,顯示之前清屏
more -c -100 mingongge
#從第十行開始顯示mingongge.txt文件內(nèi)容
more +10 mingongge.txt
#從包含字符串 "mingonggee" 行開始顯示mingongge.txt文件內(nèi)容
more +/"mingonggee" mingonggee.txt
#用ls打印當(dāng)前目錄列表撑刺,配合more分頁顯示
ls | more
less命令
less file1 類似于 'more' 命令,但是它允許在文件中和正向操作一樣的反向操作
less運(yùn)行時(shí)可以輸入的命令有:
空白鍵:#向下翻動(dòng)一頁握玛;
[pagedown]:#向下翻動(dòng)一頁够傍;
[pageup]:#向上翻動(dòng)一頁;
/字串:#向下搜尋『字串』的功能挠铲;
?字串:#向上搜尋『字串』的功能冕屯;
n:#重復(fù)前一個(gè)搜尋 (與 / 或 ? 有關(guān)!)
N:#反向的重復(fù)前一個(gè)搜尋 (與 / 或 ? 有關(guān)拂苹!)
q:#離開 less 這個(gè)程序安聘;
命令格式
less [選項(xiàng)] [文件名]
less [option] [file ...]
選項(xiàng)說明
e #文件內(nèi)容顯示完成后,自動(dòng)退出
f #強(qiáng)制顯示文件
g #不加亮顯示
l #忽略大小寫
N #行首顯示行號(hào)
s #合并多個(gè)空行只顯示一行
S #不換行顯示內(nèi)容
x<數(shù)字> #將TAB字符顯示為指定個(gè)數(shù)的空格字符
應(yīng)用舉例
#分頁顯示mingongge.log文件內(nèi)容
less /var/log/mingongge.log
#顯示文件mingongge的內(nèi)容醋寝,并在每行行首加上行號(hào)
less -N mingongge.txt
#下/上一頁:
<Space> (down), b (up)
#less 命令轉(zhuǎn)到文件的末尾/開始:
G (end), g (start)
#向前查找字符串(按 n N 跳轉(zhuǎn)到下一個(gè)/上一個(gè))
/mingongge
#向后查找字符串(按 n N 跳轉(zhuǎn)到下一個(gè)/上一個(gè))
?mingongge
#退出 less 命令
q
head命令
head命令用于查看文件的前面幾行搞挣,默認(rèn)只顯示文件的頭10行內(nèi)容。
命令格式
head [選項(xiàng)] [文件名]
head [OPTION] [FILE]
比如:
head [-n number]
head -2 file1 查看一個(gè)文件的前兩行
選項(xiàng)說明
-n<數(shù)字> #按指定行數(shù)顯示(從文件開頭位置計(jì)算)
-c<字符數(shù)> #按指定的字符數(shù)顯示(從文件的第一個(gè)字符開始計(jì)算)
-v #顯示文件名信息
-q #不顯示文件名信息
--help #打印幫助信息后退出
--version #打印版本信息后退出
應(yīng)用舉例
#顯示文件mingongge.txt前150行內(nèi)容(如果沒有其它參數(shù)音羞,可以不需要-n)
head -150 mingongge.txt
#顯示文件mingongge1.txt 和mingongge2.txt的前100行內(nèi)容
head -100 mingongge1.txt mingongge2.txt
[root@centos7 testdir]# head -100 mingongge1.txt mingongge2.txt
==> mingongge1.txt <==
11111111111111111111
22222222222222222222222
33333333333333333333333333
444444444444444444444444
....
==> mingongge2.txt <==
head1
head2
head3
head4
....
#注意:默認(rèn)情況囱桨,一次顯示多個(gè)文件的話,默認(rèn)是顯示文件名信息的嗅绰,可以不加-v參數(shù)舍肠。
[root@centos7 testdir]# head -q -n 3 mingongge1.txt mingongge2.txt
11111111111111111111
22222222222222222222222
33333333333333333333333333
head1
head2
head3
tail命令
tail命令和上面的head命令對(duì)應(yīng),用于查看文件的后面幾行窘面,默認(rèn)是顯示指定文件的末尾10行翠语。
tail 命令還可以查看文件實(shí)時(shí)寫入的數(shù)據(jù),例如财边,我們常用它來查看應(yīng)用運(yùn)行的日志文件肌括,可以動(dòng)態(tài)實(shí)時(shí)顯示應(yīng)用運(yùn)行所產(chǎn)生的日志,便于排錯(cuò)或查看應(yīng)用運(yùn)行是否正常酣难。
命令格式
tail [選項(xiàng)] [鏈接文件名]
tail [OPTION] [LINKNAME]
比如:
tail [-n number]
tail -2 file1 查看一個(gè)文件的最后兩行
tail -f /var/log/messages 實(shí)時(shí)查看被添加到一個(gè)文件中的內(nèi)容
選項(xiàng)說明
-f #顯示文件最新追加的內(nèi)容谍夭,實(shí)時(shí)動(dòng)態(tài)展示一個(gè)文件的寫入數(shù)據(jù)
-n<N> #輸出文件的尾部N(N位數(shù)字)行內(nèi)容黑滴。
--pid=<進(jìn)程號(hào)> #與“-f”選項(xiàng)配合,如果指定進(jìn)程號(hào)的進(jìn)程終止后紧索,自動(dòng)退出
-v #顯示文件名信息
-q #不顯示文件名信息
--help #打印幫助信息
--version #打印版本信息
應(yīng)用舉例
#顯示文件mingongge從第200行至文件末尾的內(nèi)容
tail -n +200 mingongge
#顯示文件mingongge的最后100個(gè)字符
[root@centos7 ~]# tail -c 100 mingongge
#顯示mingongge.log最后的250行
[root@centos7 ~]# tail -250 mingongge.log
#實(shí)時(shí)展示mingongge.log文件的寫入數(shù)據(jù)
[root@centos7 ~]# tail -f mingongge.log
#帶文件名與不帶文件名舉例
[root@centos7 ~]# tail -v -n 10 /etc/passwd /etc/shadow
==> /etc/passwd <==
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
==> /etc/shadow <==
operator:*:17834:0:99999:7:::
games:*:17834:0:99999:7:::
ftp:*:17834:0:99999:7:::
nobody:*:17834:0:99999:7:::
systemd-network:!!:18494::::::
dbus:!!:18494::::::
polkitd:!!:18494::::::
sshd:!!:18494::::::
postfix:!!:18494::::::
chrony:!!:18494::::::
[root@centos7 ~]# tail -q -n 10 /etc/passwd /etc/shadow
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
operator:*:17834:0:99999:7:::
games:*:17834:0:99999:7:::
ftp:*:17834:0:99999:7:::
nobody:*:17834:0:99999:7:::
systemd-network:!!:18494::::::
dbus:!!:18494::::::
polkitd:!!:18494::::::
sshd:!!:18494::::::
postfix:!!:18494::::::
chrony:!!:18494::::::
cut命令
cut將文件中行中內(nèi)容按指定分隔符分割并輸出袁辈。cut命令還可以用于刪除文件中指定行或段,然后打印輸出更改后的內(nèi)容珠漂。還可能用以拼接文件內(nèi)容到一個(gè)新的文件中晚缩,功能和cat類似。
命令格式
cut [選項(xiàng)] [鏈接文件名]
cut [OPTION] [LINKNAME]
選項(xiàng)說明
-b #只顯示行中指定(字節(jié)數(shù))的內(nèi)容
-c #只顯示行中指定(字符數(shù))的內(nèi)容
-d #指定字段的分隔符媳危,默認(rèn)為“TAB”
-f #打印指定字段(列)的內(nèi)容
-n #與“-b”選項(xiàng)連用荞彼,不分割多字節(jié)字符
-s #不打印不包含定界符的行的內(nèi)容
--help #打印幫助信息
--version #打印版本信息
應(yīng)用舉例
#打印指定字節(jié)數(shù)的內(nèi)容
[root@centos7 testdir]# cat mingongge1.txt
1111 11 111111 1 111 1 1 11
22222222222 222 2222 22 2 2 2
33333333333 333333 3333 333 33
444444444444 444 444444444
[root@centos7 testdir]# cut -b 3 mingongge1.txt
1
2
3
4
#截取指定字段內(nèi)容
[root@centos7 testdir]# cat cuttest.txt
1 2 3 4 5 6 8
9 8 7 6 5 4 3
2 1 9 8 7 6 5
#以空格為分隔,打印每一行的第一列
[root@centos7 testdir]# cut -f1 -d" " cuttest.txt
1
9
2
#以空格為分隔济舆,打印每一行的第一列和第三列
[root@centos7 testdir]# cut -f1,3 -d" " cuttest.txt
1 3
9 7
2 9
#以空格為分隔卿泽,打印每一行的第三列到結(jié)尾
[root@centos7 testdir]# cut -f3- -d" " cuttest.txt
3 4 5 6 8
7 6 5 4 3
9 8 7 6 5
#截取每一行第2-5個(gè)字符
[root@centos7 testdir]# cut -c 2-5 cuttest.txt
2 3
8 7
1 9
#截取每一行第一個(gè)到第五個(gè)字符
[root@centos7 testdir]# cut -c -5 cuttest.txt
1 2 3
9 8 7
2 1 9
#截取每一行第五個(gè)到最后一個(gè)字符
[root@centos7 testdir]# cut -c 5- cuttest.txt
3 4 5 6 8
7 6 5 4 3
9 8 7 6 5
md5sum命令
md5sum 用于計(jì)算和校驗(yàn)文件的MD5值。md5sum 常常被用來驗(yàn)證網(wǎng)絡(luò)文件傳輸?shù)耐暾宰叹酰乐刮募蝗舜鄹那┴病T谌粘9ぷ鳟?dāng)中,我們可以用來監(jiān)控系統(tǒng)中的重要文件是否被篡改椎侠。
還可以使用使用 md5sum 生成文件或用戶的密碼第租。
語法格式
md5sum [選項(xiàng)] [文件]
選項(xiàng)說明
-b #使用二進(jìn)制模式對(duì)文件進(jìn)行讀取
-t #把輸入的文件看作是文本文件
-c #從指定文件中讀取MD5校驗(yàn)值,并進(jìn)行校驗(yàn)
--status #校驗(yàn)成功時(shí)不輸出任何信息
-w #當(dāng)校驗(yàn)不正確時(shí)輸出警告信息
應(yīng)用舉例
生成密碼或隨機(jī)數(shù)值
[root@centos7 ~]# date | md5sum
1b1f0ba711e7d4931c23fbbd2b328e40 -
檢查一個(gè)文件的 md5 值
[root@centos7 testdir]# md5sum mingongge1.txt
c5cab5a45a72380ce456a4370bf40348 mingongge1.txt
檢查一個(gè)文件是否被更改
#提取文件原md5值
[root@centos7 testdir]# md5sum mingongge1.txt >./mingongge.txt.md5
[root@centos7 testdir]# cat mingongge.txt.md5
c5cab5a45a72380ce456a4370bf40348 mingongge1.txt
#寫入內(nèi)容
[root@centos7 testdir]# cat >> mingongge1.txt << EOF
> 1
> 2
> 3
> 4
> EOF
[root@centos7 testdir]# md5sum mingongge1.txt -c mingongge.txt.md5
mingongge1.txt: FAILED
md5sum: WARNING: 1 computed checksum did NOT match
sort命令
sort命令用于對(duì)文件的文本內(nèi)容排序我纪。系統(tǒng)默認(rèn)情況下慎宾,排序規(guī)則如下:
1)以數(shù)字開頭的行,將排在以字母開頭的行前面
2)以小寫字母開頭的行浅悉,將排在以大寫字母開頭的行前面
3)按字母表的順序排列以字母開頭的行
語法格式
sort [選項(xiàng)] [文件]
sort [OPTION] [FILE]
選項(xiàng)說明
-b #排除開頭的空白
-d #只考慮空白趟据、字母、數(shù)字
-f #將小寫字母視為大寫字母考慮
-g #根據(jù)數(shù)字排序
-i #排除不可打印字符
-M #按非月份的順序排序
-h #根據(jù)存儲(chǔ)容量排序
-n #根據(jù)數(shù)字排序术健。
-R #隨機(jī)排序
-r #倒序
--sort=WORD #根據(jù)指定的WORD排序
-V #按文本中(版本)數(shù)字的自然排序
-o #將排序結(jié)果寫入一個(gè)文件
--help #顯示幫助信息并退出
--version #顯示版本信息并退出
應(yīng)用舉例
[root@centos7 testdir]# cat cuttest.txt
1 2 3 4 5 6 8
9 8 7 6 5 4 3
2 1 9 8 7 6 5
[root@centos7 testdir]# sort cuttest.txt
1 2 3 4 5 6 8
2 1 9 8 7 6 5
9 8 7 6 5 4 3
#將結(jié)果輸出到文件
[root@centos7 testdir]# sort -o sort.cut.txt cuttest.txt
[root@centos7 testdir]# cat sort.cut.txt
1 2 3 4 5 6 8
2 1 9 8 7 6 5
9 8 7 6 5 4 3
#倒序排列
[root@centos7 testdir]# sort -r cuttest.txt
9 8 7 6 5 4 3
2 1 9 8 7 6 5
1 2 3 4 5 6 8
uniq命令
uniq命令用于去除文件中重復(fù)行汹碱,一般與sort命令結(jié)合使用。
語法格式
uniq [選項(xiàng)] [標(biāo)準(zhǔn)輸入 [輸出]]
uniq [OPTION] [INPUT [OUTPUT]]
輸入文件 #指定要去除的重復(fù)行文件荞估。如果不指定該項(xiàng)咳促,則從標(biāo)準(zhǔn)讀入
輸出文件 #指定要去除重復(fù)行后的內(nèi)容要寫入的輸出文件。如果不指定此項(xiàng)勘伺,則將內(nèi)容顯示到標(biāo)準(zhǔn)輸出設(shè)備(顯示終端)跪腹。
選項(xiàng)說明
-c #在每列旁邊顯示該行重復(fù)出現(xiàn)的次數(shù)
-d #只顯示重復(fù)出現(xiàn)的行與列
-f #忽略比較指定的字段
-s #忽略比較指定的字符
-i #不區(qū)分大小寫的比較
-u #只顯示出現(xiàn)過一次的行與列
-w #指定要比較的字符
-z #用0字節(jié)(NULL)代替換行符
--help #顯示幫助信息并退出
--version #顯示版本信息并退出
應(yīng)用舉例
#刪除重復(fù)行
[root@centos7 ~]# cat test.txt
This is a test line
This is a test line
This is a test line
This is also a test line
This is also a test line
This is also also a test line
[root@centos7 ~]# uniq test.txt
This is a test line
This is also a test line
This is also also a test line
[root@centos7 ~]# sort test.txt | uniq
This is also also a test line
This is also a test line
This is a test line
#只顯示單一行
[root@centos7 ~]# uniq -u test.txt
This is also also a test line
[root@centos7 ~]# sort test.txt |uniq -u
This is also also a test line
#統(tǒng)計(jì)各行在文件中出現(xiàn)的次數(shù)
[root@centos7 ~]# sort test.txt |uniq -c
1 This is also also a test line
2 This is also a test line
3 This is a test line
#在文件中找出重復(fù)的行
[root@centos7 ~]# sort test.txt |uniq -d
This is also a test line
This is a test line
wc命令
wc命令可以認(rèn)為是word count的縮寫,用來統(tǒng)計(jì)文件中的行數(shù)飞醉、單詞數(shù)或字節(jié)數(shù)冲茸,然后將結(jié)果輸出在終端上。我們可以使用 wc 命令來計(jì)算文件的Byte數(shù)、字?jǐn)?shù)或是列數(shù)噪裕。
語法格式
wc [選項(xiàng)] [文件]
wc [OPTION] [FILE]
選項(xiàng)說明
-c #統(tǒng)計(jì)字節(jié)數(shù)
-l #統(tǒng)計(jì)行數(shù)
-m #統(tǒng)計(jì)字符數(shù)
-w #統(tǒng)計(jì)字?jǐn)?shù)
-L #顯示最長行的長度
-help #顯示幫助信息
--version #顯示版本信息
應(yīng)用舉例
1)查看文件的字節(jié)數(shù)蹲盘、字?jǐn)?shù)、行數(shù)
[root@centos7 ~]# wc test.txt
6 34 140 test.txt
[root@centos7 ~]# wc mingongge.file
0 0 0 mingongge.file
[root@centos7 ~]# wc -L test.txt
29 test.txt
[root@centos7 ~]# wc -m test.txt
140 test.txt
[root@centos7 ~]# wc -l test.txt
6 test.txt
[root@centos7 ~]# wc -c test.txt
140 test.txt
統(tǒng)計(jì)當(dāng)前目錄下的所有文件行數(shù)及總計(jì)行數(shù)
[root@centos7 ~]# wc -l *
48 anaconda-ks.cfg
wc: goinception: Is a directory
0 goinception
45222 goInception-linux-amd64-v1.2.3.tar.gz
wc: httpd-2.4.46: Is a directory
0 httpd-2.4.46
36246 httpd-2.4.46.tar.gz
0 mingongge.file
wc: testdir: Is a directory
0 testdir
6 test.txt
81522 total
配合命令輸出結(jié)果來統(tǒng)計(jì)
[root@centos7 ~]# ls -l |wc -l
9
[root@centos7 ~]# ls -l
total 21888
-rw-------. 1 root root 1320 Aug 20 10:39 anaconda-ks.cfg
drwxr-xr-x 3 root root 39 Aug 30 03:48 goinception
-rw-r--r-- 1 root root 13034487 Aug 30 03:42 goInception-linux-amd64-v1.2.3.tar.gz
drwxr-sr-x 11 root 40 4096 Dec 24 22:35 httpd-2.4.46
-rw-r--r-- 1 root root 9363314 Aug 5 07:32 httpd-2.4.46.tar.gz
-rw-r--r-- 1 root root 0 Jan 2 08:43 mingongge.file
drwxr-xr-x 3 root root 210 Jan 16 10:19 testdir
-rw-r--r-- 1 root root 140 Jan 16 10:30 test.txt
diff命令
diff 命令用于查找膳音、分析兩個(gè)文件中不同的行,并打印輸出在屏幕上铃诬。
如果是對(duì)指定的目錄進(jìn)行比較祭陷,就是比較該指定目錄下的同名文件,不會(huì)對(duì)該目錄的子目錄下的文件進(jìn)行比較操作趣席。
語法格式
diff [選項(xiàng)] [文件]
diff [OPTION] [FILE]
選項(xiàng)說明
-<行數(shù)> #指定要顯示的行數(shù)兵志,必須與-c或-u參數(shù)一并使用
-a #逐行比較文件
-b #不對(duì)空格進(jìn)行比較
-B #不對(duì)空行進(jìn)行比較
-c #顯示全部內(nèi)容,并標(biāo)出不同之處
-C<行數(shù)> #與執(zhí)行“-c-<行數(shù)>”指令相同
-d #以小的單位來做比較
-H #加速比較大文件
-n #將比較結(jié)果以RCS的格式顯示
-q #只需顯示有無差異宣肚,不需要顯示其它信息
-r #對(duì)子目錄的文件進(jìn)行比較
-s #如果兩個(gè)文件沒有差異想罕,也給出相關(guān)的信息
-S #比較兩個(gè)目錄時(shí),從指定的文件開始執(zhí)行比較動(dòng)作
-u #以合并的方式來顯示文件內(nèi)容的不同
-v #顯示版本信息
-w #忽略所有空格字符
-y #以并列的方式顯示兩個(gè)文件的差異之處
--help #顯示幫助
應(yīng)用舉例
1)比較兩個(gè)文件的不同之處
[root@centos7 testdir]# diff mingongge1.txt mingongge2.txt
1,8c1,4
< 1111 11 111111 1 111 1 1 11
< 22222222222 222 2222 22 2 2 2
< 33333333333 333333 3333 333 33
< 444444444444 444 444444444
< 1
< 2
< 3
< 4
---
> head1
> head2
> head3
> head4
#a表示添加霉涨,c表示更改按价,d表示刪除
#以<開頭的行是第一個(gè)文件中的行
#以>開頭的行是第二個(gè)文件中的行
[root@centos7 testdir]# diff -c mingongge1.txt mingongge2.txt
*** mingongge1.txt 2021-01-16 09:55:49.489792550 -0500
--- mingongge2.txt 2021-01-16 03:36:24.645284332 -0500
***************
*** 1,8 ****
! 1111 11 111111 1 111 1 1 11
! 22222222222 222 2222 22 2 2 2
! 33333333333 333333 3333 333 33
! 444444444444 444 444444444
! 1
! 2
! 3
! 4
--- 1,4 ----
! head1
! head2
! head3
! head4
2)diff 比對(duì)字符含義
! #表示此行是一個(gè)或多個(gè)需要更改的行的一部分
+ #表示第二個(gè)文件中需要添加到第一個(gè)文件中的一行
- #表示第一個(gè)文件中需要?jiǎng)h除的一行
[root@centos7 testdir]# diff -c cuttest.txt cest.txt
*** cuttest.txt 2021-01-16 12:12:15.844549487 -0500
--- cest.txt 2021-01-16 12:15:08.775042428 -0500
***************
*** 1,6 ****
--- 1,7 ----
aaaaabbbbbbbb
cccccccccccccccccccc
ddddddddddd
+ 1111111111111111111111111111111111111111
fffffffffffffffffff
ggggggggggggggggg
wwwwwwwwwwwwwwww
#合并的方式來進(jìn)行比較
[root@centos7 testdir]# diff -u cuttest.txt cest.txt
--- cuttest.txt 2021-01-16 12:12:15.844549487 -0500
+++ cest.txt 2021-01-16 12:15:08.775042428 -0500
@@ -1,6 +1,7 @@
aaaaabbbbbbbb
cccccccccccccccccccc
ddddddddddd
+1111111111111111111111111111111111111111
fffffffffffffffffff
ggggggggggggggggg
wwwwwwwwwwwwwwww
paste命令
paste 命令用于并排顯示多個(gè)文件的相應(yīng)行,將多個(gè)文件按列合并笙瑟。
語法格式
paste [選項(xiàng)] [文件]
paste [OPTION] [FILE]
選項(xiàng)說明
-d #指定分隔符來取代默認(rèn)分隔符(TAB分隔符)
-s #串列進(jìn)行而非平行處理
--help #顯示幫助信息
--version #顯示版本信息
應(yīng)用舉例
1)并排顯示兩個(gè)文件的內(nèi)容
[root@centos7 testdir]# paste mingongge1.txt mingongge2.txt
1111 11 111111 1 111 1 1 11 head1
22222222222 222 2222 22 2 2 2 head2
33333333333 333333 3333 333 33 head3
444444444444 444 444444444 head4
1
2
3
4
2)paste 合并兩個(gè)文件楼镐,或者添加行
[root@centos7 testdir]# cat mingongge1.txt
1111 11 111111 1 111 1 1 11
22222222222 222 2222 22 2 2 2
33333333333 333333 3333 333 33
444444444444 444 444444444
1
2
3
4
[root@centos7 testdir]# cat mingongge2.txt
head1
head2
head3
head4
[root@centos7 testdir]# paste -d '\n' mingongge1.txt mingongge2.txt
1111 11 111111 1 111 1 1 11
head1
22222222222 222 2222 22 2 2 2
head2
33333333333 333333 3333 333 33
head3
444444444444 444 444444444
head4
1
2
3
4
[root@centos7 testdir]# paste -d '\r' mingongge1.txt mingongge2.txt
head111 111111 1 111 1 1 11
head2222222 222 2222 22 2 2 2
head3333333 333333 3333 333 33
head44444444 444 444444444
1
2
3
4
3)paste 命令其他用法
[root@centos7 testdir]# paste mingongge1.txt
1111 11 111111 1 111 1 1 11
22222222222 222 2222 22 2 2 2
33333333333 333333 3333 333 33
444444444444 444 444444444
1
2
3
4
#連接文件中的所有行
[root@centos7 testdir]# paste -s mingongge1.txt
1111 11 111111 1 111 1 1 11 22222222222 222 2222 22 2 2 2 33333333333 333333 3333 333 33 444444444444 444 444444444 1 2 3 4
#使用指定分隔符連接所有行
[root@centos7 testdir]# paste -d, -s mingongge1.txt
1111 11 111111 1 111 1 1 11,22222222222 222 2222 22 2 2 2,33333333333 333333 3333 333 33,444444444444 444 444444444,1,2,3,4
[root@centos7 testdir]# paste -d[] -s mingongge1.txt
1111 11 111111 1 111 1 1 11[22222222222 222 2222 22 2 2 2]33333333333 333333 3333 333 33[444444444444 444 444444444]1[2]3[4
[root@centos7 testdir]# paste -d/ -s mingongge1.txt
1111 11 111111 1 111 1 1 11/22222222222 222 2222 22 2 2 2/33333333333 333333 3333 333 33/444444444444 444 444444444/1/2/3/4
#查看 paste 命令版本
[root@centos7 testdir]# paste --version
paste (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by David M. Ihnat and David MacKenzie.
lsof命令
lsof 命令用于顯示 Linux 系統(tǒng)當(dāng)前已打開的所有文件列表。查看進(jìn)程或系統(tǒng)打開的文件會(huì)給調(diào)試帶來極大的幫助往枷。下面簡單地介紹 lsof 常使用的功能框产。
lsof (list open files)命令用于查看你進(jìn)程打開的文件,打開文件的進(jìn)程错洁,進(jìn)程打開的端口(TCP秉宿、UDP),還可以用于找回/恢復(fù)被刪除的文件。lsof 命令需要訪問核心內(nèi)存和各種文件屯碴,所以需要具備 root 超級(jí)管理員權(quán)限的用戶才能執(zhí)行此命令描睦。
語法格式
lsof [Options]
選項(xiàng)說明
-a #顯示打開文件的進(jìn)程
-c<進(jìn)程名> #顯示指定進(jìn)程所打開的文件
-g #顯示GID號(hào)進(jìn)程詳情
-d<文件號(hào)> #顯示占用該文件號(hào)的進(jìn)程
+d<目錄> #顯示目錄下被打開的文件
+D<目錄> #遞歸列出目錄下被打開的文件
-n<目錄> #顯示使用NFS的文件
-l #在輸出顯示用戶ID而不是用戶名
-i<條件> #輸出符合條件的進(jìn)程
-p<進(jìn)程號(hào)> #輸出指定進(jìn)程號(hào)所打開的文件
-u #顯示指定UID號(hào)進(jìn)程詳情
-h #顯示幫助信息
-t #僅獲取進(jìn)程ID
-U #獲取UNIX套接口地址
-F #格式化輸出結(jié)果
-v #顯示版本信息
應(yīng)用舉例
1)顯示所有連接
[root@CentOS7-1 ~]# lsof -i
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
chronyd 629 chrony 5u IPv4 16952 0t0 UDP localhost:323
chronyd 629 chrony 6u IPv6 16953 0t0 UDP localhost:323
sshd 866 root 3u IPv4 19638 0t0 TCP *:ssh (LISTEN)
sshd 866 root 4u IPv6 19647 0t0 TCP *:ssh (LISTEN)
master 976 root 13u IPv4 20415 0t0 TCP localhost:smtp (LISTEN)
master 976 root 14u IPv6 20416 0t0 TCP localhost:smtp (LISTEN)
netdata 18325 netdata 4u IPv4 114083 0t0 TCP *:dnp-sec (LISTEN)
netdata 18325 netdata 5u IPv6 114084 0t0 TCP *:dnp-sec (LISTEN)
netdata 18325 netdata 36u IPv6 114297 0t0 UDP localhost:8125
netdata 18325 netdata 37u IPv4 114298 0t0 UDP localhost:8125
netdata 18325 netdata 38u IPv6 114302 0t0 TCP localhost:8125 (LISTEN)
netdata 18325 netdata 39u IPv4 114303 0t0 TCP localhost:8125 (LISTEN)
sshd 18968 root 3u IPv4 118704 0t0 TCP CentOS7-1:ssh->192.168.1.93:62148 (ESTABLISHED)
2)只顯示IPV6的連接信息
[root@CentOS7-1 ~]# lsof -i 6
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
chronyd 629 chrony 6u IPv6 16953 0t0 UDP localhost:323
sshd 866 root 4u IPv6 19647 0t0 TCP *:ssh (LISTEN)
master 976 root 14u IPv6 20416 0t0 TCP localhost:smtp (LISTEN)
netdata 18325 netdata 5u IPv6 114084 0t0 TCP *:dnp-sec (LISTEN)
netdata 18325 netdata 36u IPv6 114297 0t0 UDP localhost:8125
netdata 18325 netdata 38u IPv6 114302 0t0 TCP localhost:8125 (LISTEN)
3)只顯示IPV4的連接信息
[root@CentOS7-1 ~]# lsof -i 4
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
chronyd 629 chrony 5u IPv4 16952 0t0 UDP localhost:323
sshd 866 root 3u IPv4 19638 0t0 TCP *:ssh (LISTEN)
master 976 root 13u IPv4 20415 0t0 TCP localhost:smtp (LISTEN)
netdata 18325 netdata 4u IPv4 114083 0t0 TCP *:dnp-sec (LISTEN)
netdata 18325 netdata 37u IPv4 114298 0t0 UDP localhost:8125
netdata 18325 netdata 39u IPv4 114303 0t0 TCP localhost:8125 (LISTEN)
sshd 18968 root 3u IPv4 118704 0t0 TCP CentOS7-1:ssh->192.168.1.93:62148 (ESTABLISHED)
4)僅顯示TCP連接
[root@CentOS7-1 ~]# lsof -i tcp
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 866 root 3u IPv4 19638 0t0 TCP *:ssh (LISTEN)
sshd 866 root 4u IPv6 19647 0t0 TCP *:ssh (LISTEN)
master 976 root 13u IPv4 20415 0t0 TCP localhost:smtp (LISTEN)
master 976 root 14u IPv6 20416 0t0 TCP localhost:smtp (LISTEN)
netdata 18325 netdata 4u IPv4 114083 0t0 TCP *:dnp-sec (LISTEN)
netdata 18325 netdata 5u IPv6 114084 0t0 TCP *:dnp-sec (LISTEN)
netdata 18325 netdata 38u IPv6 114302 0t0 TCP localhost:8125 (LISTEN)
netdata 18325 netdata 39u IPv4 114303 0t0 TCP localhost:8125 (LISTEN)
sshd 18968 root 3u IPv4 118704 0t0 TCP CentOS7-1:ssh->192.168.1.93:62148 (ESTABLISHED)
5)僅顯示UDP連接
[root@CentOS7-1 ~]# lsof -i udp
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
chronyd 629 chrony 5u IPv4 16952 0t0 UDP localhost:323
chronyd 629 chrony 6u IPv6 16953 0t0 UDP localhost:323
netdata 18325 netdata 36u IPv6 114297 0t0 UDP localhost:8125
netdata 18325 netdata 37u IPv4 114298 0t0 UDP localhost:8125
6)顯示指定端口的連接信息
[root@CentOS7-1 ~]# lsof -i :22
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 866 root 3u IPv4 19638 0t0 TCP *:ssh (LISTEN)
sshd 866 root 4u IPv6 19647 0t0 TCP *:ssh (LISTEN)
sshd 18968 root 3u IPv4 118704 0t0 TCP CentOS7-1:ssh->192.168.1.93:62148 (ESTABLISHED)
[root@CentOS7-1 ~]# lsof -i :80
[root@CentOS7-1 ~]# lsof -i :62148
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 18968 root 3u IPv4 118704 0t0 TCP CentOS7-1:ssh->192.168.1.93:62148 (ESTABLISHED)
7)列由某個(gè)用戶打開的進(jìn)程或文件
[root@CentOS7-1 ~]# lsof -u root | head -5
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd 1 root cwd DIR 253,0 262 64 /
systemd 1 root rtd DIR 253,0 262 64 /
systemd 1 root txt REG 253,0 1628608 16959493 /usr/lib/systemd/systemd
systemd 1 root mem REG 253,0 20064 1679454 /usr/lib64/libuuid.so.1.3.0
#列出除了root以外用戶打開的文件
[root@CentOS7-1 ~]# lsof -u ^root | head
COMMAND PID TID USER FD TYPE DEVICE SIZE/OFF NODE NAME
polkitd 618 polkitd cwd DIR 253,0 262 64 /
polkitd 618 polkitd rtd DIR 253,0 262 64 /
polkitd 618 polkitd txt REG 253,0 120432 17108633 /usr/lib/polkit-1/polkitd
polkitd 618 polkitd mem REG 253,0 61560 18290 /usr/lib64/libnss_files-2.17.so
polkitd 618 polkitd mem REG 253,0 68192 40949 /usr/lib64/libbz2.so.1.0.6
polkitd 618 polkitd mem REG 253,0 99952 324334 /usr/lib64/libelf-0.176.so
polkitd 618 polkitd mem REG 253,0 19896 46144 /usr/lib64/libattr.so.1.1.0
polkitd 618 polkitd mem REG 253,0 20064 1679454 /usr/lib64/libuuid.so.1.3.0
polkitd 618 polkitd mem REG 253,0 265576 20649 /usr/lib64/libblkid.so.1.1.0
8)顯示指定的連接信息
#顯示指定到指定主機(jī)的連接
[root@CentOS7-1 ~]# lsof -i@192.168.1.100
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 18968 root 3u IPv4 118704 0t0 TCP CentOS7-1:ssh->192.168.1.93:62148 (ESTABLISHED)
#顯示指定到指定主機(jī)端口的連接
[root@CentOS7-1 ~]# lsof -i@192.168.1.100:22
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 18968 root 3u IPv4 118704 0t0 TCP CentOS7-1:ssh->192.168.1.93:62148 (ESTABLISHED)
9)顯示某些狀態(tài)的端口信息
#找出處于監(jiān)聽狀態(tài)的端口
[root@CentOS7-1 ~]# lsof -i -sTCP:LISTEN
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 866 root 3u IPv4 19638 0t0 TCP *:ssh (LISTEN)
sshd 866 root 4u IPv6 19647 0t0 TCP *:ssh (LISTEN)
master 976 root 13u IPv4 20415 0t0 TCP localhost:smtp (LISTEN)
master 976 root 14u IPv6 20416 0t0 TCP localhost:smtp (LISTEN)
netdata 18325 netdata 4u IPv4 114083 0t0 TCP *:dnp-sec (LISTEN)
netdata 18325 netdata 5u IPv6 114084 0t0 TCP *:dnp-sec (LISTEN)
netdata 18325 netdata 38u IPv6 114302 0t0 TCP localhost:8125 (LISTEN)
netdata 18325 netdata 39u IPv4 114303 0t0 TCP localhost:8125 (LISTEN)
##找出處于已連接狀態(tài)的端口
[root@CentOS7-1 ~]# lsof -i -sTCP:ESTABLISHED
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 18968 root 3u IPv4 118704 0t0 TCP CentOS7-1:ssh->192.168.1.93:62148 (ESTABLISHED)
10)終止用戶行為
[root@CentOS7-1 ~]# lsof -u mingongge
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
bash 20369 mingongge cwd DIR 253,0 82 462615 /home/mingongge
bash 20369 mingongge rtd DIR 253,0 262 64 /
bash 20369 mingongge txt REG 253,0 964536 50333070 /usr/bin/bash
bash 20369 mingongge mem REG 253,0 106172832 50333042 /usr/lib/locale/locale-archive
bash 20369 mingongge mem REG 253,0 61560 18290 /usr/lib64/libnss_files-2.17.so
bash 20369 mingongge mem REG 253,0 2156240 18266 /usr/lib64/libc-2.17.so
bash 20369 mingongge mem REG 253,0 19248 18273 /usr/lib64/libdl-2.17.so
bash 20369 mingongge mem REG 253,0 174576 20696 /usr/lib64/libtinfo.so.5.9
bash 20369 mingongge mem REG 253,0 163312 1679434 /usr/lib64/ld-2.17.so
bash 20369 mingongge mem REG 253,0 26970 16806930 /usr/lib64/gconv/gconv-modules.cache
bash 20369 mingongge 0u CHR 136,1 0t0 4 /dev/pts/1
bash 20369 mingongge 1u CHR 136,1 0t0 4 /dev/pts/1
bash 20369 mingongge 2u CHR 136,1 0t0 4 /dev/pts/1
bash 20369 mingongge 255u CHR 136,1 0t0 4 /dev/pts/1
vim 20391 mingongge cwd DIR 253,0 82 462615 /home/mingongge
vim 20391 mingongge rtd DIR 253,0 262 64 /
vim 20391 mingongge txt REG 253,0 2337192 51061591 /usr/bin/vim
vim 20391 mingongge mem REG 253,0 61560 18290 /usr/lib64/libnss_files-2.17.so
vim 20391 mingongge mem REG 253,0 106172832 50333042 /usr/lib/locale/locale-archive
vim 20391 mingongge mem REG 253,0 11392 8814 /usr/lib64/libfreebl3.so
vim 20391 mingongge mem REG 253,0 14424 1679441 /usr/lib64/libutil-2.17.so
vim 20391 mingongge mem REG 253,0 40600 18271 /usr/lib64/libcrypt-2.17.so
vim 20391 mingongge mem REG 253,0 115816 18278 /usr/lib64/libnsl-2.17.so
vim 20391 mingongge mem REG 253,0 109976 18302 /usr/lib64/libresolv-2.17.so
vim 20391 mingongge mem REG 253,0 19896 46144 /usr/lib64/libattr.so.1.1.0
vim 20391 mingongge mem REG 253,0 402384 20698 /usr/lib64/libpcre.so.1.2.0
vim 20391 mingongge mem REG 253,0 2156240 18266 /usr/lib64/libc-2.17.so
vim 20391 mingongge mem REG 253,0 142144 18300 /usr/lib64/libpthread-2.17.so
vim 20391 mingongge mem REG 253,0 1647328 282389 /usr/lib64/perl5/CORE/libperl.so
vim 20391 mingongge mem REG 253,0 19248 18273 /usr/lib64/libdl-2.17.so
vim 20391 mingongge mem REG 253,0 27752 669 /usr/lib64/libgpm.so.2.1.0
vim 20391 mingongge mem REG 253,0 37064 18281 /usr/lib64/libacl.so.1.1.0
vim 20391 mingongge mem REG 253,0 174576 20696 /usr/lib64/libtinfo.so.5.9
vim 20391 mingongge mem REG 253,0 155744 1679449 /usr/lib64/libselinux.so.1
vim 20391 mingongge mem REG 253,0 1136944 18275 /usr/lib64/libm-2.17.so
vim 20391 mingongge mem REG 253,0 163312 1679434 /usr/lib64/ld-2.17.so
vim 20391 mingongge 0u CHR 136,1 0t0 4 /dev/pts/1
vim 20391 mingongge 1u CHR 136,1 0t0 4 /dev/pts/1
vim 20391 mingongge 2u CHR 136,1 0t0 4 /dev/pts/1
vim 20391 mingongge 3u REG 253,0 12288 462619 /home/mingongge/.test.sh.swp
然后我們使用下面的命令來終止這個(gè)用戶的這些操作行為
[root@CentOS7-1 ~]# kill -9 `lsof -t -u mingongge`
[root@CentOS7-1 ~]# lsof -u mingongge
#你會(huì)發(fā)現(xiàn)這個(gè)用戶的所有操作都被終止了
tee命令
tee 命令用于從標(biāo)準(zhǔn)輸入讀取,然后寫入文件或標(biāo)準(zhǔn)輸出和文件窿锉。一般用于需要同時(shí)查看數(shù)據(jù)內(nèi)容并輸出到文件時(shí)使用酌摇。
語法格式
tee [OPTION]... [FILE]...
選項(xiàng)說明
-a #追加到文件中而不是覆蓋
-i #忽略中斷信號(hào)(Ctrl+c中斷操作無效)
-p #診斷寫入非管道的錯(cuò)誤
--output-error[=MODE] #設(shè)置寫錯(cuò)誤時(shí)的行為
--help #顯示幫助信息并退出
--version #顯示版本信息并退出
MODE參數(shù)
'warn' #當(dāng)寫入到任何輸出報(bào)錯(cuò)時(shí)診斷
'warn-nopipe' #當(dāng)寫入到任何輸出(而不是管道)報(bào)錯(cuò)時(shí)診斷
'exit' #當(dāng)寫入到任何輸出報(bào)錯(cuò)時(shí)退出
'exit-nopipe' #當(dāng)寫入到任何輸出(而不是管道)報(bào)錯(cuò)時(shí)退出
應(yīng)用舉例
列出當(dāng)前目錄中所有文件擴(kuò)展名為.tar.gz的文件,每行一個(gè)文件嗡载, 然后將內(nèi)容傳輸給 wc 對(duì)行進(jìn)行計(jì)數(shù)并輸出數(shù)字窑多。通過管道傳輸?shù)?tee 后再將輸出寫入終端,并將相同的信息寫入文件 tee.txt洼滚。
1)如果 tee.txt 已經(jīng)存在埂息,它將被覆蓋,如果不存在,將被創(chuàng)建千康。
[root@centos7 ~]# ls -l *.tar.gz
-rw-r--r-- 1 root root 13034487 Aug 30 2020 goInception-linux-amd64-v1.2.3.tar.gz
-rw-r--r-- 1 root root 9363314 Aug 5 2020 httpd-2.4.46.tar.gz
-rw-r--r-- 1 root root 31674465 Mar 10 09:42 mysql_backup.tar.gz
-rw-r--r-- 1 root root 398872 Mar 28 00:11 netcat-0.7.1.tar.gz
[root@centos7 ~]# ls -l *.tar.gz | wc -l
4
[root@centos7 ~]# ls -l *.tar.gz | wc -l | tee tee.txt
4
[root@centos7 ~]# cat tee.txt
4
2)tee.txt 已經(jīng)存在享幽,它將被覆蓋
[root@centos7 ~]# cat tee.txt
4
[root@centos7 ~]# ls -1 *.txt | wc -l
3
[root@centos7 ~]# ls -l *.txt | wc -l |tee tee.txt
3
[root@centos7 ~]# cat tee.txt
3
3)追加內(nèi)容
[root@centos7 ~]# cat tee.txt
3
[root@centos7 ~]# ls -1 *.tar.gz | wc -l
4
[root@centos7 ~]# ls -1 *.tar.gz | wc -l | tee -a tee.txt
4
[root@centos7 ~]# cat tee.txt
3
4