1. 程式人生 > 實用技巧 >-linux-檔案高階管理

-linux-檔案高階管理

一 檔案處理三劍客命令初探

三劍客命令我們將在shell程式設計裡深入講解,此處先學會最基本的使用。

1 sed

流式編輯器,主要擅長對檔案的編輯操作,我們可以事先定製好編輯檔案的指令,然後讓sed自動完成對檔案的整體編輯。

# 用法
sed 選項 '定位+命令' 檔案路徑

# 選項
-n	取消預設輸出
-r	支援擴充套件正則元字元
-i  立即編輯檔案

# 定位
行定位:
	1定位到第一行
  1,3代表第1行帶第3行
  不寫定位代表定位所有行
  
正則表示式定位:
	/egon/包含egon的行
  /^egon/ 以egon開頭的行 
  /egon$/以egon結尾的行
  
數字+正則表示式定位:
	'1,8p'代表列印1到8行,
  '1,/egon/p'則代表取從第1行到首次匹配到/egon/的行
  
# 命令
d
p
s///g
命令可以用;號連線多多條,如1d;3d;5d代表刪除1,3,5行

# =========================》用法示例:p與d
[root@arther-linux /]# sed '' file.txt 
ssss111111
sss
sss
aaa
aa
222
sss

[root@arther-linux /]# sed -n '' file.txt  # 取消了預設輸出 本身是輸出操作
[root@arther-linux /]# 

[root@arther-linux /]# sed -n '1,/aa/p' file.txt # 以正則方式匹配輸出
ssss111111
sss
sss
aaa

[root@arther-linux /]# sed -n '1,/^aa$/p' file.txt 
ssss111111
sss
sss
aaa
aa

[root@arther-linux /]# sed '1,/aaa/d' file.txt  # 使用預設輸出 刪除操作
aa
222
sss

[root@arther-linux /]# sed '1d;3d;5d' file.txt 
sss
aaa
222
sss

# =========================》用法示例: s///g
[root@arther-linux /]# cat file.txt 
ssss111111
sss
sss
aaa
aa
222aaa
sss

[root@arther-linux /]# sed 's/sss/xxx/g' file.txt # sss->xxx
xxxs111111
xxx
xxx
aaa
aa
222aaa
xxx

[root@arther-linux /]# sed '/^a/s/aa/xxx/g' file.txt 
ssss111111
sss
sss
xxxa
xxx
222aaa
sss
# 每行以a開頭的aa換成xxx

[root@arther-linux /]# sed '6s/aaa/xxx/' file.txt 
ssss111111
sss
sss
aaa
aa
222xxx
sss
# 把第6行的aaa換成xxx

[root@arther-linux /]# sed '4,6s/aaa/xxx/g' file.txt 
ssss111111
sss
sss
xxx
aa
222xxx
sss
# 4,6行的aaa換成xxx

[root@arther-linux /]# cat file.txt | sed '2,7d' # sed也支援管道
ssss111111

# 加上-i選項,直接修改檔案,通常會在除錯完畢確保沒有問題後再加-i選項

2 awk

awk主要用於處理有格式的文字,例如/etc/passwd這種

# 用法
awk 選項 'pattern{action}' 檔案路徑

# 選項
-F 指定行分隔符

# 工作流程 
awk -F: '{print $1,$3}' /etc/passwd

-1.awk會讀取檔案的一行內容然後賦值給$0
-2.然後awk會以-F指定的分隔符將該行切分成n段,最多可以達到100段,第一段給$1,第二段給$2,依次類推
-3.print輸出該行的第一段和第三段,逗號代表輸出分隔符,預設與-F保持一致
-4.重複步驟1,2,3直到檔案內容讀完

# 內建變數
$0 一整行內容 
NR 記錄號,等同於行號 
NF 以-F分隔符分隔的段數

# pattern可以是 
/正則/
	/正則/ 	# 該行內容匹配成功正則 
  $1 ~ /正則/ # 第一段內容匹配成功正則 
  $1 !~ /正則/ # 第一段內容沒有匹配成功正則 
  比較運算: 
  NR >= 3 && NR <=5 # 3到5行 
  $1 == "root" # 第一段內容等於root
  
# action可以是 
print $1,$3

# 用法示例
[root@arther-linux /]# cat a.txt
root:x:0:0:root:/root:/bin/bash 
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 
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
            
[root@arther-linux /]# awk -F: '/^root/{print $1,$3}' a.txt 
root 0
# 匹配到以root開頭的那行後,以:進行切分分成若干段,列印其中第1,3段

[root@localhost ~]# awk -F: '$1 ~ /^d/{print $1,$3}' a.txt 	
#以冒號為分隔符,從第一行開始匹配以字母d開頭的行,然後列印第一段和第三段
[root@localhost ~]# awk -F: '$1 !~ /^d/{print $1,$3}' a.txt 	
#以冒號為分隔符,從第一行開始匹配不是以字母d開頭的行,然後列印第一段和第三段
[root@localhost ~]# awk -F: 'NR>3{print $1}' a.txt 			
#以冒號為分隔符,匹配所有行號大於3的行,列印第一段
[root@localhost ~]# awk -F: '$1 == "lp"{print $0}' a.txt 		
#以冒號為分隔符,匹配第一段內容為lp的行,並列印該行全部內容
[root@localhost ~]# cat a.txt | awk -F: '{print $1}' 
# awk也支援管道

事實上awk是一門程式語言,可以獨立完成很強大的操作,我們將在shell程式設計中詳細介紹

3 grep

grep擅長過濾內容

# 用法 
grep 選項 '正則' 檔案路徑 

# 選項 
-n, --line-number 在過濾出的每一行前面加上它在檔案中的相對行號 
-i, --ignore-case 忽略大小寫 
--color 顏色 
-l, --files-with-matches 如果匹配成功,則只將檔名打印出來,失敗則不列印 通														常-rl一起用,grep -rl 'root' /etc 
-R, -r, --recursive 遞迴 

# 示例
	[root@localhost ~]# grep '^root' /etc/passwd	
  #匹配以root開頭的行
	[root@localhost ~]# grep -n 'bash$' /etc/passwd	 	
  #匹配以bash結尾的行並顯示行號
	[root@localhost ~]# grep -rl 'root' /etc	 
  #匹配/etc下檔案內容中包含root的檔名並列印(只打印檔名)
  
# grep也支援管道,我們可以發現三劍客命令都支援管道
	
	[root@localhost ~]# ps aux |grep ssh	
  # 檢視系統進行並匹配ssh服務的程序
	[root@localhost ~]# ps aux |grep [s]sh	
  # 檢視系統進行並匹配ssh服務的程序,但不匹配grep ssh本身

二 檔案管理:檔案查詢

1 檢視命令所屬檔案

[root@localhost ~]# which ip 
/usr/sbin/ip 
# ps: 一些命令的路徑都被配置到了環境變數PATH裡 echo $PATH

2 查詢檔案

find [options] [path...] [expression]

按檔名:

[root@localhost ~]# find /etc -name "ifcfg-eth0" 
[root@localhost ~]# find /etc -iname "ifcfg-eth0" # -i忽略大小寫 [root@localhost ~]# find /etc -iname "ifcfg-eth*" # *直接匹配所有

按檔案大小:

[root@localhost ~]# find /etc -size +3M # 大於3M 
[root@localhost ~]# find /etc -size 3M 
[root@localhost ~]# find /etc -size -3M 
[root@localhost ~]# find /etc -size +3M -ls # -ls找到的處理動作

指定查詢的目錄深度:

-maxdepth levels 
[root@localhost ~]# find / -maxdepth 5 -a -name "ifcfg-eth0" 
# -a並且,-o或者, 不加-a,預設就是-a

按檔案屬主、屬組找:

[root@localhost ~]# find /home -user egon # 屬主是egon的檔案 [root@localhost ~]# find /home -group it # 屬組是it組的檔案 [root@localhost ~]# find /home -user egon -group it 
[root@localhost ~]# find /home -user egon -a -group it # 同上意思一樣 [root@localhost ~]# find /home -user egon -o -group it [root@localhost ~]# find /home -nouser 
# 使用者還存在,在/etc/passwd中刪除了記錄 
[root@localhost ~]# find /home -nogroup 
# 使用者還存在,在/etc/group中刪除了記錄 
[root@localhost ~]# find /home -nouser -o -nogroup

按檔案型別:

[root@localhost ~]# find /dev -type f # f普通 
[root@localhost ~]# find /dev -type d # d目錄 
[root@localhost ~]# find /dev -type l # l連結 
[root@localhost ~]# find /dev -type b # b塊裝置 
[root@localhost ~]# find /dev -type c # c字元裝置 
[root@localhost ~]# find /dev -type s # s套接字 
[root@localhost ~]# find /dev -type p # p管道檔案

根據inode號查詢:-inum n

[root@localhost ~]# find / -inum 1811

按檔案許可權:

[root@localhost ~]# find . -perm 644 -ls 
[root@localhost ~]# find . -perm -644 -ls 
[root@localhost ~]# find . -perm -600 -ls 
[root@localhost ~]# find /sbin -perm -4000 -ls # 包含set uid [root@localhost ~]# find /sbin -perm -2000 -ls # 包含set gid [root@localhost ~]# find /sbin -perm -1000 -ls # 包含sticky

找到後處理的動作:

-print 
-ls 
-delete 
-exec 
-ok 
[root@localhost ~]# find /etc -name "ifcfg*" -print # 必須加引號 [root@localhost ~]# find /etc -name "ifcfg*" -ls 
[root@localhost ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp \; # 非互動,直接進行操作
[root@localhost ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp \; 
# 互動,每一次操作會有一次詢問,確定才能操作成功,用於篩選
[root@localhost ~]# find /etc -name "ifcfg*" -exec rm -rf {} \; [root@localhost ~]# find /etc -name "ifcfg*" -delete # 同上

擴充套件知識:find結合xargs

[root@localhost ~]# find . -name "*.txt" |xargs rm -rf		
#查詢當前目錄下*.txt格式的檔案並進行刪除
[root@localhost ~]# find /etc -name "ifcfg*" |xargs -I {} cp -rf {} /var/tmp	
#查詢/etc下名稱為ifcfg*的檔案並拷貝到/var/tmp下,-I選項表示佔位符
[root@localhost ~]# find /var/tmp -name "ifcfg*" |xargs -I {} mv {} /usr	
#查詢/var/tmp下名稱為ifcfg*的檔案並將其移動到/usr下
[root@localhost ~]# find /usr/ -name "ifcfg*" |xargs -I {} chmod 666 {}		
#查詢/usr/下名稱為ifcfg*的檔案並將其許可權修改為666

三 檔案管理:上傳與下載

1 下載

wget命令

wget -O 本地路徑 遠端包連結地址 
# 將遠端包下載到本地,-O指定下載到哪裡,可以生路-O 本地路徑 
# ps:如果wget下載提示無法建立SSL連線,
# 則加上選項--no-check-certificate wget --no-check-certificate -O 
# 本地路徑 遠端包連結地址

curl命令

# curl命令是一個利用URL規則在命令列下工作的檔案傳輸工具。它支援檔案的上傳和下載,所以是綜合傳輸 工具,但按傳統,習慣稱curl為下載工具。作為一款強力工具,curl支援包括HTTP、HTTPS、[ftp]等眾多 協議,還支援POST、cookies、認證、從指定偏移處下載部分檔案、使用者代理字串、限速、檔案大小、進 度條等特徵。做網頁處理流程和資料檢索自動化,curl可以祝一臂之力。 
[root@localhost ~]# curl -o 123.png https://www.xxx.com/img/hello.png # ps: 如果遇到下載提示無法簡歷SSL連結,使用-k選項或者--insecure curl -k -o 123.png https://www.xxx.com/img/hello.png

sz命令

# 系統預設沒有該命令,需要下載:yum install lrzsz -y 
# 將伺服器上選定的檔案下載/傳送到本機
[root@localhost ~]# sz bak.tar.gz

2 上傳

rz命令

# 系統預設沒有該命令,需要下載:yum install lrzsz -y 
# 執行該命令會彈出一個檔案選擇視窗,從本地選擇檔案上傳到伺服器。 
[root@localhost opt]# rz # 如果檔案已經存,則上傳失敗,可以用-E選項解決 [root@localhost opt]# rz -E 
# -E如果目標檔名已經存在,則重新命名傳入檔案。新檔名將新增一 個點和一個數字(0..999)

四 檔案管理:輸出與重定向

輸出即把相關物件通過輸出裝置(顯示器等)顯示出來,輸出又分正確輸出和錯誤輸出一般情況下標準輸出裝置為顯示器,標準輸入裝置為鍵盤。

Linux中用

  • 0代表標準輸入
  • 1代表標準正確輸入
  • 2代表標準錯誤輸出

![image-20201123201030101](/Users/arther_wan/Library/Application Support/typora-user-images/image-20201123201030101.png)

輸出重定向:

正常輸出是把內容輸出到顯示器上,而輸出重定向是把內容輸出到檔案中,>代表覆蓋原檔案,>>代表在原檔案中追加

ps:標準輸出的1可以省略

![image-20201123201346662](/Users/arther_wan/Library/Application Support/typora-user-images/image-20201123201346662.png)

例如:ifconfig > test.log 即把ifconfig執行顯示的正確內容寫入test.log.當前頁面不再顯示執行結果。

注意:錯誤輸出重定向>與>>後邊不要加空格

![image-20201123201645109](/Users/arther_wan/Library/Application Support/typora-user-images/image-20201123201645109.png)

注意:

  • 下述兩個命令作用相同

    命令 >>file.log 2>&1 
    命令 &>>file.log
    
  • 正確日誌和錯誤日誌分開儲存

    命令 >>file1.log 2>>file2.log
    
    [root@arther-linux test]# echo "info" &>>file1.log 2>>file2.log
    [root@arther-linux test]# cat file1.log 
    info
    [root@arther-linux test]# cat file2.log
    [root@arther-linux test]# 
    
  • 系統有個常見用法 ls &>/dev/null 正確輸出或錯誤輸出結果都不要。(null可以理解為黑洞或垃圾站)

輸入重定向:

# 沒有改變輸入的方向,預設鍵盤,此時等待輸入
No
no

#沒有改變輸入的方向,預設鍵盤,此時等待輸入 [root@egon ~]# grep 'root' oldboy root root
echo "hello cm qq:123456" >> file.txt
[root@cm ~]# tr 'cm' 'CM' < file.txt	
#從file.txt檔案中讀取內容,將小寫cm替換為大寫CM

[root@cm ~]# grep 'root' < /etc/passwd	
#從/etc/passwd檔案中讀取內容匹配root所在的行
root:x:0:0:root:/root:/bin/bash

# mysql如何恢復備份,瞭解即可,不用關注。	#將bbs.sql中讀取到的內容恢復到MySQL中。
[root@qls ~]# mysql -uroot -p123 < bbs.sql

五 檔案管理:字元處理命令

1 sort命令

用於將檔案內容加以排序

  • -n 按照數值的大小排序
  • -r 以相反的順序來排序
  • -k 以某列進行排序
  • -t 指定分隔符,預設是以空格為分隔符

準備檔案,寫入一段無序的內容

[root@arther-linux test]# cat >> file.txt <<EOF
> b:3
> c:2
> a:4
> e:5
> d:1
> f:11
> EOF
#將終端輸入內容插入file.txt檔案中,碰到字元EOF結束。

[root@arther-linux test]# sort file.txt 
a:4
b:3
c:2
d:1
e:5
f:11
# 此時已每行的首字母排序

[root@arther-linux test]# sort -t ':' -n -k2 file.txt
d:1
c:2
b:3
a:4
e:5
f:11
# 以冒號為間隔符,對數字型別做比較,比較第二個欄位的值

[root@arther-linux test]# sort -t ':' -n -r -k2 file.txt
f:11
e:5
a:4
b:3
c:2
d:1
# 在上一個基礎上進行倒敘排序

2 uniq命令

用於檢查及刪除文字檔案中重複出現的行列,一般與sort命令結合使用

  • -c 在每列旁邊顯示該行重複出現的次數
  • -d 僅顯示重複出現的行列
  • -u 僅顯示出現一次的行列(排除重複出現的,及原資料)

準備檔案,寫入一段無序的內容

[root@arther-linux test]# cat > file.txt <<EOF   # > 指覆蓋原檔案操作
> hello
> 123
> hello
> 123
> func
> EOF

[root@arther-linux test]# file.txt | uniq
bash: file.txt: 未找到命令...
[root@arther-linux test]# sort file.txt | uniq
123
func
hello
# 所以需要與sort連用,去掉重複的內容

[root@arther-linux test]# sort file.txt | uniq -c
2 123
1 func
2 hello
# 去掉重複的內容並統計出現的次數

[root@arther-linux test]# sort file.txt | uniq -d
123
hello
# 只顯示重複了內容

[root@arther-linux test]# sort file.txt | uniq -u
func
# 只顯示出現了一次的內容

3 cut命令

cut命令用來顯示行中的指定部分,刪除檔案中指定欄位

  • -d 指定欄位的分隔符,預設的欄位的分隔符為"TAB"
  • -f 顯示指定欄位的內容
[root@arther-linux /]# head -1 /1.txt 
t:x:0:0:root:/root:/bin/bash   # 獲取第一行資料

[root@arther-linux /]# head -1 /1.txt | cut -d ":" -f1,3,5
t:0:root   # 以":"切分之後獲取第1,3,5段的內容
    

4 tr命令

替換或刪除命令

  • -d 刪除命令

例1

[root@arther-linux /]# head -1 /1.txt | tr "root" "ROOT"
T:x:0:0:ROOT:/ROOT:/bin/bash
# 替換:root->ROOT

[root@arther-linux /]# head -1 /1.txt | tr -d "root"
:x:0:0::/:/bin/bash
# 該段的字元刪除

例2

[root@arther-linux /]# echo "hello arther qq:1813142161" > file.txt 
[root@arther-linux /]# tr "arther" "ARTHER" < file.txt 
HEllo ARTHER qq:1813142161
  
[root@arther-linux /]# cat file.txt | tr "arther" "ARTHER"
HEllo ARTHER qq:1813142161

5 wc命令

統計,計算數字

  • -c 統計檔案的Bytes數
  • -l 統計檔案的行數
  • -w 統計檔案中單詞的個數,預設以空白字元做為分隔符

例1

[root@arther-linux /]# ll file.txt 
-rw-r--r--. 1 root root 27 11月 23 20:57 file.txt
[root@arther-linux /]# wc -c file.txt 
27 file.txt

例2

[root@arther-linux /]# wc -l 2.txt 
28 2.txt
# 顯示總共出現多少行

[root@arther-linux /]# grep "ssa" 2.txt |wc -l
16
# 該字元在檔案中出現的次數

[root@arther-linux /]# wc -w 2.txt
160 2.txt
# 出現了多少個單詞,預設以空格為分隔符,來判定單詞的數量

六 檔案管理:打包壓縮

1 什麼是打包壓縮

​ 打包指的是將多個檔案和目錄合併為一個特殊檔案

​ 然後將該特殊檔案進行壓縮

​ 最終得到一個壓縮包

2 為什麼使用壓縮包

​ 減少佔用的體積

​ 加快網路的傳輸

3 Windows的壓縮和Linux的有什麼不同

​ windows: .zip .rar(linux不支援)

​ linux: .zip tar.gz tar.bz2 .gz .bz2

​ 如果希望windows的軟體能被linux解壓,或者linux的軟體包被windows能識別,選擇zip.

PS: 壓縮包的字尾不重要,但一定要攜帶.

4 linux下常見的壓縮包型別

格式 壓縮工具
.zip zip壓縮工具
.gz gzip壓縮工具,只能壓縮檔案,會刪除原檔案(通常配合tar使用)
.bz2 bzip2壓縮工具,只能壓縮檔案,會刪除原檔案(通常配合tar使用)
.tar.gz 先使用tar命令歸檔打包,然後使用gzip壓縮
.tar.bz2 先使用tar命令歸檔打包,然後使用bzip壓縮

ps:windows下支援.rar,linux不支援.rar

5 打包壓縮的方法

方法一:

# 1、打包 
[root@localhost test]# tar cvf etc_bak.tar /etc/ # c建立 v詳細 f打包後文件路徑 
ps: 
  打包的目標路徑如果是絕對路徑,會提示:tar: 從成員名中刪除開頭的“/”,不影響打包, 	 新增-P選項便不再提示:tar cvPf ...
  可以cd 到 /etc下然後tar cvf etc_bak.tar *打包,這樣去掉了一層資料夾
  
# 2、壓縮 
[root@localhost test]# gzip etc_bak.tar # 檔案體積變小,並且加上字尾.gz 
ps: 
  gzip -> gunzip 
  bzip2-> bunzip2
  
#3、上述兩步可以合二為一
[root@localhost test]# tar czvf etc1_bak.tar.gz /etc/  
# 選項z代表gzip壓縮演算法
[root@localhost test]# tar cjvf etc1_bak.tar.bz2 /etc/  
# 選項j代表bzip2壓縮演算法
	
	解壓命令:無論哪種壓縮格式解壓命令都相同
	tar xvf etc1_bak.tar.gz -C /usr/local/		
  #將etc1_bak.tar.gz壓縮包解壓到/usr/local下
	tar xvf etc1_bak.tar.bz2  .					
  #將etc1_bak.tar.bz2壓縮包解壓到當前目錄下

方法二:

#zip壓縮 
選項:
		-r #遞迴壓縮 壓縮目錄 
  	-q #靜默輸出 
# 示例1、 
[root@localhost ~]# zip /test/bak.zip a.txt b.txt c.txt 
# zip後的第一個引數是壓縮包 路徑,其餘為被壓縮的檔案 
adding: a.txt (stored 0%) 
adding: b.txt (stored 0%) 
adding: c.txt (stored 0%) 
  
[root@localhost ~]# ls /test/ bak.zip 
  
# 示例1、 
[root@localhost ~]# zip -rq etc.zip /etc # 加上-q後壓縮過程不再提示

6 解壓縮

#1、針對xxx.tar.gz 或者 xxx.tar.bz2,統一使用 
[root@localhost test]# tar xvf 壓縮包 -C 解壓到的目錄 # 無需指定解壓演算法,tar會自動判斷 

#2、針對xxx.zip,用unzip 
選項:
		-l #顯示壓縮包的列表資訊 
  	-q #靜默輸出 
    -d #解壓到指定的目錄 
[root@localhost test]# unzip -q xxx.zip -d /opt