文件管理之:聯(lián)網(wǎng)下載文件(wget侍匙、curl)、文件上傳與下載(rz欧募、sz)
----wget茄茁、curl聯(lián)網(wǎng)下載文件----
1.CentOS7 系統(tǒng)最小化安裝默認(rèn)沒(méi)有wget命令,需要進(jìn)行安裝
[root@oldboyedu ~]# yum install wget -y
2.找到我們需要下載的資源
復(fù)制資源的鏈接地址---->
3.在linux上使用wget命令進(jìn)行下載(默認(rèn)下載到當(dāng)前目錄來(lái))
[root@oldboyedu ~]# wget
http://fj.xuliangwei.com/public/weixin.py
4.由于我們下載的是文件,所有我們可以使用cat less more 查看該
文件
[root@oldboyedu ~]# cat weixin.py
5.使用wget下載資源時(shí),指定保存的位置,并重新命名
[root@oldboyedu ~]# wget -O /opt/tt.png
http://fj.xuliangwei.com/public/ks.jpeg
6.下載資源時(shí),如果不想重新命名只想修改保存的路徑,請(qǐng)帶上原有的名稱
[root@oldboyedu ~]# wget -O /opt/ks.jpeg
http://fj.xuliangwei.com/public/ks.jpeg
---curl---
curl 瀏覽網(wǎng)絡(luò)上的資源
1.在線瀏覽網(wǎng)站資源內(nèi)容(源代碼)
[root@oldboyedu ~]# curl
http://fj.xuliangwei.com/public/weixin.py
2.使用curl將內(nèi)容保存至本地,并重命名(如果沒(méi)有明確指定路徑,則表示
當(dāng)前目錄)
[root@oldboyedu ~]# curl -o wei.txt
http://fj.xuliangwei.com/public/weixin.py
3.將資源保存至指定的路徑
[root@oldboyedu ~]# curl -o /opt/weixin.py
http://fj.xuliangwei.com/public/weixin.py
PS: 通常情況下我們推薦使用wget下載,但由于系統(tǒng)很多時(shí)候默認(rèn)沒(méi)有
按照wget 會(huì)偶爾使用一下curl
練習(xí): 使用兩種方式下載如下的兩個(gè)文件
1.wget保存至本地 /etc/yum.repos.d/CentOS-Base.repo
http://mirrors.aliyun.com/repo/Centos-7.repo
[root@oldboyedu ~]# wget -O /etc/yum.repos.d/CentOSBase.repo
http://mirrors.aliyun.com/repo/Centos-7.repo
2.curl保存至本地 /etc/yum.repos.d/epel.repo
http://mirrors.aliyun.com/repo/epel-7.repo
[root@oldboyedu ~]# curl -o /etc/yum.repos.d/epel.repo
http://mirrors.aliyun.com/repo/epel
3.最后執(zhí)行一條命令檢查 yum makecache
-----rzsz上傳下載文件---
如果無(wú)法將文件直接拖拽進(jìn)Linux服務(wù)器
1.要么沒(méi)有安裝lrzsz ,建議安裝 yum install lrzsz -y
2.你上傳的是一個(gè)空文件
rz #只能上傳文件碉输,不支持上傳文件夾籽前,不支持大于4個(gè)G上傳,也不
支持?jǐn)帱c(diǎn)續(xù)傳
sz /path/file #只能下載文件 (任意單個(gè)文件)敷钾,不支持下載文件夾
4.文件管理之:文件或命令查找(which枝哄、whereis、find)
--------命令查找-------
1.查找一個(gè)命令的絕對(duì)路徑
當(dāng)我們想執(zhí)行一個(gè)命令的絕對(duì)路徑時(shí),先使用which command 查詢絕對(duì)路徑
which ls #查找ls命令的絕對(duì)路徑
2.whereis也使用來(lái)查詢命令的絕對(duì)路徑
whereis ls //查找命令的路徑阻荒、幫助手冊(cè)挠锥、等
whereis -b ls //僅顯示命令所在的路徑
3.對(duì)于內(nèi)核相關(guān)的一些命令,使用which whereis是無(wú)法查詢到,需要使用type命令查詢
type -a ls #查看命令的絕對(duì)路徑(包括別名)
對(duì)于后面要使用一個(gè)命令的絕對(duì)時(shí).
例如:
[root@oldboy ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@oldboy ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
[root@oldboy ~]# whereis -b ls
ls: /usr/bin/ls
[root@oldboy ~]# type for
for is a shell keyword
5.文件管理之:文件內(nèi)容處理命令(sort、uniq侨赡、cut蓖租、sed粱侣、awk、wc蓖宦、)
----------------------------------------sort排序----------------------------------------
在有些情況下齐婴,需要對(duì)應(yīng)一個(gè)無(wú)序的文本文件進(jìn)行數(shù)據(jù)的排序,這時(shí)
就需要使用sort進(jìn)行排序了稠茂。
sort [OPTION]... [FILE]...
-r:倒序
-n:按數(shù)字排序
-t:指定分隔符(默認(rèn)空格)
-k:指定第幾列, 指定幾列幾字符(指定1,1 3.1,3.3)
1.首先創(chuàng)建一個(gè)文件柠偶,寫(xiě)入一寫(xiě)無(wú)序的內(nèi)容
[root@xuliangwei ~]# cat >> file.txt <<EOF
b:3
c:2
a:4
e:5
d:1
f:11
EOF
2.使用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
按照排序的方式, 只會(huì)看到第一個(gè)字符,11的第一個(gè)字符是1, 按照字符
來(lái)排序確實(shí)比2小该肴。
如果想要按照數(shù)字的方式進(jìn)行排序, 需要使用 -n參數(shù)。
[root@xuliangwei ~]# sort -t ":" -n -k2 p.txt
d:1
c:2
b:3
a:4
e:5
f:11
測(cè)試案例藐不,下載文件
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使用)涎嚼。
uniq [OPTION]... [INPUT [OUTPUT]]
選項(xiàng):-c 計(jì)算重復(fù)的行
1.創(chuàng)建一個(gè)file.txt文件:
[root@xuliangwei ~]# cat >>file1.txt <<EOF
abc
123
abc
123
EOF
2.uniq需要和sort一起使用, 先使用sort排序, 讓重復(fù)內(nèi)容連續(xù)在一
起
[root@xuliangwei ~]# sort file.txt
123
123
abc
abc
3.使用uniq去除相鄰重復(fù)的行
[root@xuliangwei ~]# sort file.txt |uniq
123
abc
4.-c參數(shù)能統(tǒng)計(jì)出文件中每行內(nèi)容重復(fù)的次數(shù)
[root@xuliangwei ~]# sort file.txt |uniq -c
2 123
2 abc
------cut截取字段(不太重要)---
cut OPTION... [FILE]...
選項(xiàng):
-d 指定分隔符
-f 數(shù)字,取第幾列 –f3,6三列和6列 -c 按字符取(空格也算)
[root@oldboyedu ~]# cat >>file2.txt <<EOF
Im xlw, is QQ 552408925
EOF
實(shí)現(xiàn): 篩選出文件里 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
PS: 實(shí)際生產(chǎn)使用過(guò)程中,很少使用到cut,通常都是使用awk,因?yàn)閍wk是取列專業(yè)戶
---wc統(tǒng)計(jì)行號(hào)---
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)
練習(xí)題: 過(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
18
擴(kuò)展統(tǒng)計(jì)文件行號(hào)的方法
[root@oldboyedu ~]# cat -n /etc/services | tail -1
[root@oldboyedu ~]# grep -n ".*" /etc/services | tail -1
習(xí)題: 分析如下日志挑秉,統(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
習(xí)題: 使用awk取出系統(tǒng)的IP地址,思路如下:
1.我要取的值在哪里 ifconfig ens32
2.如何縮小取值范圍(行)
3.如何精確具體內(nèi)容(列)
.先拿到結(jié)果,然后提取有關(guān)鍵字那一行,最后使用awk取出那一列
[root@oldboyedu ~]# ifconfig ens32|grep "netmask" | awk '{print $2}'
10.0.0.200
今日內(nèi)容
文件相關(guān)
1.wget curl 下載互聯(lián)網(wǎng)上的文件
2.rz sz 下載linux服務(wù)器上的文件,或上傳文件至linux服務(wù)器
3.which whereis type 查找一個(gè)命令的絕對(duì)路徑
4.cut awk 取列 grep 取行 wc 統(tǒng)計(jì)內(nèi)容
cut+awk awk grep+awk grep+wc