2019-07-30
wget,curl:聯(lián)網(wǎng)下載文件
系統(tǒng)最小化安裝默認(rèn)是沒(méi)有wget命令的,需要安裝
[root@oldboyedu ~]# yum install wget -y
.在linux上使用wget命令進(jìn)行下載(默認(rèn)下載到當(dāng)前目錄來(lái))
[root@oldboyedu ~]# wget http://fj.xuliangwei.com/public/weixin.py
由于我們下載的是文件,所以我們可以用查看文件的命令來(lái)查看我們下載的文件
.使用wget下載資源時(shí),指定保存的位置,并重新命名
[root@oldboyedu ~]# wget -O /opt/tt.png http://fj.xuliangwei.com/public/ks.jpeg
.下載資源時(shí),如果不想重新命名只想修改保存的路徑,請(qǐng)帶上原有的名稱
[root@oldboyedu ~]# wget -O /opt/ks.jpeg http://fj.xuliangwei.com/public/ks.jpeg
curl:瀏覽網(wǎng)絡(luò)上的資源
在線瀏覽網(wǎng)站資源內(nèi)容(源代碼)
[root@oldboyedu ~]# curl http://fj.xuliangwei.com/public/weixin.py
使用curl將內(nèi)容保存至本地,并重命名(如果沒(méi)有明確指定路徑,則表示 當(dāng)前目錄)
[root@oldboyedu ~]# curl -o wei.txt http://fj.xuliangwei.com/public/weixin.py
將資源保存至指定的路徑
[root@oldboyedu ~]# curl -o /opt/weixin.py?http://fj.xuliangwei.com/public/weixin.py
注意:通常情況下我們推薦使用wget下載,但由于系統(tǒng)很多時(shí)候默認(rèn)沒(méi)有 按照wget 會(huì)偶爾使用一下curl
rz,sz:上傳下載文件,只能上傳下載文件,不支持文件夾,不支持大于4個(gè)G的上傳文件,也不支持?jǐn)帱c(diǎn)續(xù)傳
注意:如果無(wú)法將文件直接拖拽進(jìn)Linux服務(wù)器
1.要么沒(méi)有安裝lrzsz ,建議安裝 ? yum install lrzsz -y?
2.你上傳的是一個(gè)空文件
which ,whereis ,find:文件或者命令查找
查找一個(gè)命令的絕對(duì)路徑
當(dāng)我們想執(zhí)行一個(gè)命令的絕對(duì)路徑時(shí),先使用which command 查詢絕對(duì)路徑
which ls? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #查找ls命令的絕對(duì)路徑
whereis也使用來(lái)查詢命令的絕對(duì)路徑
whereis ls? ? ? ? ? ? ? ? ? ? ? ? ? #查找命令的路徑、幫助手冊(cè)、等
whereis -b ls? ? ? ? ? ? ? ? ? ? ? #僅顯示命令所在的路徑
對(duì)于內(nèi)核相關(guān)的一些命令,使用which whereis是無(wú)法查詢到,需要使 用type命令查詢
type -a ls? ? ? ? ? ? ? ? ? ? ? ? ? ? #查看命令的絕對(duì)路徑(包括別名)
sort:排序
在有些情況下,需要對(duì)應(yīng)一個(gè)無(wú)序的文本文件進(jìn)行數(shù)據(jù)的排序,這時(shí) 就需要使用sort進(jìn)行排序了。
語(yǔ)法:sort [OPTION]... [FILE]...
-r:倒序 -n:按數(shù)字排序 -t:指定分隔符(默認(rèn)空格) -k:指定第幾 列, 指定幾列幾字符(指定1,1 3.1,3.3)
首先創(chuàng)建一個(gè)文件,寫入一寫無(wú)序的內(nèi)容 [root@xuliangwei ~]# cat >> file.txt <<EOF?
b:3?
c:2?
a:4?
e:5?
d:1?
f:11?
EOF
使用sort下面對(duì)輸出的內(nèi)容進(jìn)行排序 [root@xuliangwei ~]# sort file.txt?
a:4?
b:3?
c:2?
d:1?
e:5?
f:11
結(jié)果并不是按照數(shù)字排序,而是按字母排序
可以使用-t指定分隔符, 使用-k指定需要排序的列。
[root@xuliangwei ~]# sort -t ":" -k2 sort.txt?
d:1?
f:11? ? ? ? ? ? ? ? ? ? ? ? #第二行為什么是11?不應(yīng)該按照順序排列??
c:2?
b:3?
a:4?
e:5?
f:11
案例:下載文件 http://fj.xuliangwei.com/public/ip.txt,對(duì)該文件進(jìn)行排序
[root@xuliangwei ~]# sort -t. -k3.1,3.1nr -k4.1,4.3nr ip.txt
uniq:去重
如果文件中有多行完全相同的內(nèi)容,當(dāng)前是希望能刪除重復(fù)的行,同時(shí)還可以統(tǒng)計(jì)出完全相同的行出現(xiàn)的總次數(shù), 那么就可以使用uniq命令解決這個(gè)問(wèn)題(但是必須配合sort使用)。
語(yǔ)法:uniq [OPTION]... [INPUT [OUTPUT]]
選項(xiàng):-c 計(jì)算重復(fù)的行
創(chuàng)建一個(gè)file.txt文件:
[root@xuliangwei ~]# cat >>file1.txt <<EOF?
abc?
123?
abc?
123?
EOF
uniq需要和sort一起使用, 先使用sort排序, 讓重復(fù)內(nèi)容連續(xù)在一 起
[root@xuliangwei ~]# sort file.txt?
123?
123?
abc?
abc
使用uniq去除相鄰重復(fù)的行
[root@xuliangwei ~]# sort file.txt |uniq?
123?
abc
-c參數(shù)能統(tǒng)計(jì)出文件中每行內(nèi)容重復(fù)的次數(shù)
[root@xuliangwei ~]# sort file.txt |uniq -c ? ? ?
2 123 ? ? ?
2 abc
cut:截取字段(我們常用awk)
語(yǔ)法:cut OPTION... [FILE]...
選項(xiàng):-d 指定分隔符 -f 數(shù)字,取第幾列 –f3,6三列和6列 -c 按字符 取(空格也算)
[root@oldboyedu ~]# cat >>file2.txt <<EOF?
Im xlw, is QQ 552408925?
EOF
篩選出文件里 xlw以及552408925
[root@oldboyedu ~]# awk '{print $2,$5}' file2.txt | awk -F "," '{print $1,$2}'?
xlw ?552408925
[root@oldboyedu ~]# cut -d " " -f 2,5 file2.txt | awk -F "," '{print $1,$2}'?
xlw ?552408925
[root@oldboyedu ~]# cut -d " " -f 2,5 file2.txt | sed 's#,##g'?
xlw 552408925
[root@oldboyedu ~]# sed 's#,##g' file2.txt | awk '{print $2,$5}'?
xlw 552408925
注意:實(shí)際生產(chǎn)使用過(guò)程中,很少使用到cut,通常都是使用awk,因?yàn)閍wk 是取列專業(yè)戶
wc:統(tǒng)計(jì)行號(hào)
語(yǔ)法:wc [OPTION]... [FILE]...
選項(xiàng):-l顯示文件行數(shù)
wc -l /etc/fstab? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#統(tǒng)計(jì)/etc/fstab文件有多少行
wc -l /etc/services? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #統(tǒng)計(jì)/etc/services 文件行號(hào)
案例:過(guò)濾出/etc/passwd以nologin結(jié)尾的內(nèi)容,并統(tǒng)計(jì)有多少行
1.先篩選出目標(biāo)的行
2.然后進(jìn)行統(tǒng)計(jì)
[root@oldboyedu ~]# grep "nologin$" /etc/passwd | wc l
擴(kuò)展統(tǒng)計(jì)文件行號(hào)的方法
[root@oldboyedu ~]# cat -n /etc/services | tail -1?
[root@oldboyedu ~]# grep -n ".*" /etc/services | tail -1
案例:分析下面日志,統(tǒng)計(jì)每個(gè)域名被訪問(wèn)的次數(shù)。
[root@student tmp]# cat >> web.log <<EOF?
http://www.xuliangwei.com/index.html?
http://www.xuliangwei.com/1.html?
http://post.xuliangwei.com/index.html?
http://mp3.xuliangwei.com/index.html?
http://www.xuliangwei.com/3.html?
http://post.xuliangwei.com/2.html?
EOF
[root@oldboyedu ~]# cut -d "/" -f 3 web.log | sort | uniq -c | sort -nr?
[root@oldboyedu ~]# awk -F "/" '{print $3}' web.log |sort |uniq -c |sort -nr
使用awk取出系統(tǒng)的IP地址,思路如下:
1.我要取的值在哪里?
2.如何縮小取值范圍(行)
3.如何精確具體內(nèi)容(列)
先拿到結(jié)果,然后提取有關(guān)鍵字那一行,最后使用awk取出那一列
[root@oldboyedu ~]# ifconfig ens32|grep "netmask" | awk '{print $2}'
10.0.0.200
[root@oldboyedu ~]# ifconfig ens32 | awk '/netmask/' | awk '{print $2}'?
10.0.0.200
[root@oldboyedu ~]# ifconfig ens32 | awk '/netmask/ {print $2}'?
10.0.0.200
分析如下日志,請(qǐng)?zhí)崛〕鲈L問(wèn)次數(shù)最高的TOP10IP地址
[root@oldboyedu ~]# awk '{print $1}' fj.xuliangwei.com.log |sort |uniq -c|sort -nr | head
注意:以上都是重點(diǎn)
