文件管理之:聯(lián)網(wǎng)下載文件(wget,curl),文件上傳與下載(rz,sz)
---------------------wget,curl聯(lián)網(wǎng)下載文件-------------------
1.Centos-7系統(tǒng)最小化安裝默認沒有wget命令,需要進行安裝
[root@oldboy--day01 ~]# yum install wget -y
2.找到需要下載下載的資源
? 復(fù)制資源的鏈接地址
3.在Linux上使用wget命令進行下載(默認安裝到當前目錄來)
[root@oldboy--day01 ~]# wget http://fj.xuliangwei.com/public/weixin.py
4.由于我們下載的是文件,所以我們可以用cat less more 查看該文件
[root@oldboy--day01 ~]# cat weixin.py
5.使用wget下載資源時,指定保存的位置,并重命名
[root@oldboy--day01 ~]# wget -O /opt/tt.png http://fj.xuliangwei.com/public/ks.jpeg
6.下載資源時,如果不想重命名只想修改保存的的路徑,請帶上原有的名字
[root@oldboy--day01 ~]# wget -O /opt/ks.jpeg http://fj.xuliangwei.com/public/ks.jpeg
curl? 瀏覽網(wǎng)絡(luò)上的資源
1.在線瀏覽網(wǎng)站資源內(nèi)容(源代碼)
[root@oldboy--day01 ~]# curl? http://fj.xuliangwei.com/public/weixin.py
2.使用curl將內(nèi)容保存到本地,并重命名(如果沒有明確指定路徑,則表示當前目錄)
[root@oldboy--day01 ~]# curl -o weixin.txt http://fj.xuliangwei.com/public/weixin.py
3.將資源保存至指定路徑
[root@oldboy--day01 ~]# curl -o /opt/weixin.py http://fj.xuliangwei.com/public/weixin.py
PS:通常情況下我們推薦使用wget下載,但由于系統(tǒng)有時候默認沒有wget,偶爾會使用一下curl
練習(xí):
1.wget保存至本地/etc/yum.repos.d/CentOS-Base.repo? http://mirrors.aliyun.com/repo/Centos-7.repo
[root@oldboy--day01 ~]# wget -O /etc/yum.repos.d/CentOS-Base.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@oldboy--day01 ~]# curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
3.最后執(zhí)行一條命令檢查 yum makecache
----------------rz和sz? #上傳和下載文件(Windows->Linux)------------------
? ?PS:如果無法將文件直接拖拽進Linux服務(wù)器
? 1.要么沒有安裝lrzsz,建議安裝?
? ? ? yum install lrzsz -y
? 2.上傳的文件是個空文件
? ? ?rz ? ? ? ?#只能上傳文件,不支持上傳文件夾,不支持上傳大于4G的文件,也不支持斷點續(xù)傳
? ? ?sz ? ? ? #/path/file? #只能下載文件(任意單個文件),不支持下載文件夾
文件管理之:命令查找(which,whereis,find)
---------------命令查找-------------
1.查找一個命令的絕對路徑
當我們想執(zhí)行一個命令的絕對路徑時,先使用which command 查詢絕對路徑
? which ls #查找ls命令的絕對路徑
2.whereis也是用來查詢命令的絕對路徑,幫助手冊,等
? ?whereis ls? //查找命令的路徑,幫助手冊.等
? ?whereis -b ls? //僅顯示命令所在的路徑
3.對于內(nèi)核相關(guān)的一些命令,使用whichwhereis是無法查詢到,需要使用type命令查詢
? type -a ls? #查看命令的絕對路徑(包括別名)
? ?對于后面使用一個命令的聚堆路徑時.
文件管理之:字符處理命令(sort,unip,cut,sed,grep,awk,wc)
------------------------sort 排序---------------------
在有些情況下,需要對應(yīng)一個無序的文本進行數(shù)據(jù)的排序,這時就需要使用sort進行排序了
sort [OPTION]...[FILE]...
?-r: 倒序 -n:按數(shù)字排序 -t: 指定分隔符(默認空格) -k: 指定第幾列,指定幾列幾字符(指定1,1 3.1,3.3)
1.首先創(chuàng)建一個文件,寫入一個無序的內(nèi)容
b:3
c:2
a:4
e:5
d:1
f:11
-----------------------------------------------------------------------------------------------------------
[root@oldboy--day01 ~]# cat >>file.txt<<EOF
> b:3
> c:2
> a:4
> e:5
> d:1
> f:11
> EOF
2.使用sort下面對輸出的內(nèi)容進行排序
a:4
b:3
c:2
d:1
-------------------------------------------------------------------------------------------------------------
[root@oldboy--day01 ~]# sort file.txt
a:4
b:3
c:2
d:1
e:5
f:11
結(jié)果并不是按照數(shù)字排序,而是按字母排序
處理方法:可以使用-t指定分隔符,使用-k 指定需要排序的列
[root@oldboy--day01 ~]# sort -t ":" -k2 file.txt
d:1
f:11
c:2
b:3
a:4
e:5
可以看出并不是按照數(shù)字大小進行排序的而是以第二列的第一個字符大小進行排序的
如果想要按數(shù)字大小的方式 進行排序,需要會用參數(shù) -name就可以使用unip命令解決這個問題
[root@oldboy--day01 ~]# sort -t ":"? -k2 file.txt -n
d:1
c:2
b:3
a:4
e:5
f:11
------------------------unip去重-----------------------------
如果文件中有許多行完全相同的內(nèi)容,當前是希望能刪除重復(fù)的行,同時還可以統(tǒng)計出完全想聽的行的總數(shù),
name就可以使用unip命令解決這個問題(但必須配合sort使用).
unip ? [OPTION]...[INPUT [OUTPUT]]
?選項: -c 計算重復(fù)的行
?1.創(chuàng)建一個file1.txt文件:
abc
123
abc
123
----------------------------
[root@oldboy--day01 ~]# cat >>file1.txt<<EOF
> abc
> 123
> abc
> 123
> EOF
2.unip需要和sort一起使用,先使用sort排序,讓重復(fù)內(nèi)容連接到一起
123
123
abc
abc
-----------------
[root@oldboy--day01 ~]# sort file1.txt
123
123
abc
abc
3.使用uniq去除相鄰重復(fù)的行
[root@oldboy--day01 ~]# sort file.txt |uniq
123
abc
4.-c參數(shù)能統(tǒng)計出文件中每行內(nèi)容重復(fù)的次數(shù)
[root@oldboy--day01 ~]# sort file.txt |uniq -c
2 123
2 abc
---------------------------cut截取字段----------------
cut OPTION...[FILE]...
? 選項:-d 指定分隔符 -f 數(shù)字,取第幾列 -f3,6第三列和6列 -c 按字符取(空格也算)
? ?[root@oldboy--day01 ~]# cat >>file2.txt <<EOF
? ?Im xlw, is QQ 552408925
? ?EOF
實現(xiàn): 篩選出文件里 xlw以及552408925
[root@oldboy--day01 ~]# awk '{print $2,$5}' file2.txt | awk -F "," '{print $1,$2}'
xlw 552408925
[root@oldboy--day01 ~]# cut -d " " -f 2,5 file2.txt | awk -F "," '{print $1,$2}'
xlw 552408925
[root@oldboy--day01 ~]# cut -d " " -f 2,5 file2.txt | sed 's#,##g'
xlw 552408925
[root@oldboy--day01 ~]# sed 's#,##g' file2.txt | awk '{print $2,$5}'
xlw 552408925
#PS: 實際生產(chǎn)使用過程中,很少使用到cut,通常都是使用awk,因為awk 是取列專業(yè)戶
---------------------wc統(tǒng)計行號--------------------
wc [OPTION]... [FILE]...
選項:-l顯示文件行數(shù)
1.wc -l /etc/fstab #統(tǒng)計/etc/fstab文件有多少行
2.wc -l /etc/services #統(tǒng)計/etc/services 文件行號
練習(xí)題: 過濾出/etc/passwd以nologin結(jié)尾的內(nèi)容,并統(tǒng)計有多少行
1.先篩選出目標的行---篩選出/etc/passwd中以nologin結(jié)尾的行
[root@oldboy--day01 ~]# grep "nologin$" /etc/passwd?
?2.然后進行統(tǒng)計
[root@oldboy--day01 ~]# grep "nologin$" /etc/passwd | wc - l
18
?擴展統(tǒng)計文件行號的方法
[root@oldboy--day01 ~]# cat -n /etc/services | tail -1
[root@oldboy--day01 ~]# grep -n ".*" /etc/services | tail -1
1.習(xí)題: 分析如下日志,統(tǒng)計每個域名被訪問的次數(shù)。
[root@oldboy--day01 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@oldboy--day01 ~]# cut -d "/" -f 3 web.log | sort | uniq -c | sort -nr
[root@oldboy--day01 ~]# 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@oldboy--day01 ~]# ifconfig ens32|grep "netmask" | awk '{print $2}'
10.0.0.200
[root@oldboy--day01 ~]# ifconfig ens32 | awk '/netmask/' | awk '{print $2}'
10.0.0.200
[root@oldboy--day01 ~]# ifconfig ens32 | awk '/netmask/ {print $2}'
10.0.0.200
習(xí)題:分析如下日志,請?zhí)崛〕鲈L問次數(shù)最高的TOP10IP地址
[root@oldboy--day01 ~]# awk '{print $1}' fj.xuliangwei.com.log |sort |uniq -c|sort -nr | head
習(xí)題: 將/etc/passwd文件中的第一行中的第一列和最后一列位置進行 交換。
習(xí)題: 將/etc/sysconfig/selinux 文件中的SELINUX=enforcing替換成 SELINUX=disabled
取列:cut awk()(推薦)
替換:sed
取行: grep awk
---------------------wc統(tǒng)計行號--------------------
統(tǒng)計文件總共有多少行
wc [OPTION]....[FILE]...
選項: -l顯示文件行數(shù) -c顯示文件字節(jié) -w顯示文件單詞
1.wc -l顯示文件行數(shù) -c顯示文件字節(jié) -w顯示穩(wěn)基建單詞
2.wc -l/etc/fstab #統(tǒng)計/etc/fstab文件有多少行
3.wc -l /etc/services #統(tǒng)計/etc/services 文件行號
擴展方法
[root@oldboy--day01 ~]# grep -n ".*" /etc/services | tail -l
[root@oldboy--day01 ~]# awk '{print NR $0}' /etc/services | tail -l
[root@oldboy--day01 ~]# cat -n /etc/services | tail -l