Linux系統檔案的壓縮解壓縮
阿新 • • 發佈:2022-04-18
檔案的壓縮打包
目錄壓縮包格式
windows:
.zip
.tar
.tar.gz
.gz
.rar
.7z
.bz
.bz2
.xz
為什麼使用壓縮
- 1.檔案或目錄太大,需要壓縮傳輸
- 2.服務安裝包都需要解壓
Linux常用壓縮格式及命令
格式 | Linux命令 |
---|---|
.zip | zip |
.gz | gzip |
.tar | tar |
.tar.gz | tar、gzip |
壓縮命令-gzip
# gzip命令的安裝:(centOS7自帶命令) [root@localhost ~]# yum install -y gzip # gzip命令用法: gzip 普通檔名 # 選項 -r:遞迴壓縮 (gzip遞迴壓縮會自動對目錄下個每個普通檔案分別壓縮,不可對上級目錄進行壓縮) # 檢視壓縮包中檔案的內容命令 zcat # 解壓命令 gzip -d 壓縮包名 ## 特性: 1.壓縮檔案後,原始檔不存在 2.只能壓縮檔案,不能壓縮目錄 3.壓縮後,壓縮包的位置在原始檔的目錄下 4.壓縮後可以直接檢視檔案內容zcat 5.一個壓縮包中,只會有一個檔案 6.解壓後,壓縮包沒了,只剩原始檔 # 示例: [root@localhost~]# ll /cheshi003/toni/ total 20 -rw-r--r-- 1 root root 29 May 1 00:29 aaa1.txt.gz -rw-r--r-- 1 root root 29 May 1 00:29 aaa2.txt.gz -rw-r--r-- 1 root root 29 May 1 00:29 aaa3.txt.gz -rw-r--r-- 1 root root 29 May 1 00:29 aaa4.txt.gz -rw-r--r-- 1 root root 29 May 1 00:29 aaa5.txt.gz [root@localhost~]# gzip -r /cheshi003/toni/ [root@localhost~]# ll /cheshi003/toni/ total 24 -rw-r--r-- 1 root root 33 May 1 00:57 aaa1.txt.gz -rw-r--r-- 1 root root 33 May 1 00:57 aaa2.txt.gz -rw-r--r-- 1 root root 33 May 1 00:58 aaa3.txt.gz -rw-r--r-- 1 root root 33 May 1 00:58 aaa4.txt.gz -rw-r--r-- 1 root root 33 May 1 00:59 aaa5.txt.gz [root@localhost~]# file /cheshi003/toni/aaa1.txt.gz /cheshi003/toni/aaa1.txt.gz: gzip compressed data, was "aaa1.txt", from Unix, last modified: Sun May 1 00:57:44 2022 [root@localhost~]# zcat /cheshi003/toni/aaa1.txt.gz bbb # 解壓/遞迴解壓: 1) [root@localhost~]# gzip -d /cheshi003/toni/aaa1.txt.gz [root@localhost~]# ll /cheshi003/toni/ total 24 -rw-r--r-- 1 root root 4 May 1 00:57 aaa1.txt -rw-r--r-- 1 root root 33 May 1 00:57 aaa2.txt.gz -rw-r--r-- 1 root root 33 May 1 00:58 aaa3.txt.gz -rw-r--r-- 1 root root 33 May 1 00:58 aaa4.txt.gz -rw-r--r-- 1 root root 33 May 1 00:59 aaa5.txt.gz -rw-r--r-- 1 root root 33 May 1 00:56 aaaa.txt.gz 2) [root@localhost~]# gzip -dr /cheshi003/toni/ [root@localhost~]# ll /cheshi003/toni/ total 24 -rw-r--r-- 1 root root 4 May 1 00:57 aaa1.txt -rw-r--r-- 1 root root 4 May 1 00:57 aaa2.txt -rw-r--r-- 1 root root 4 May 1 00:58 aaa3.txt -rw-r--r-- 1 root root 4 May 1 00:58 aaa4.txt -rw-r--r-- 1 root root 4 May 1 00:59 aaa5.txt -rw-r--r-- 1 root root 4 May 1 00:56 aaaa.txt
壓縮命令-zip
# zip和unzip命令: [root@localhost ~]# yum install -y zip [root@localhost ~]# yum install -y unzip # zip壓縮命令: zip 壓縮包名 檔案或目錄 壓縮目錄需要加選項 -r 如果不加,壓縮後,只有一個空目錄,沒有裡面的檔案 命令: 壓縮包名 需要放進壓縮包的檔案 [root@localhost ~]# zip txt.zip 1.txt 2.txt 3.txt # 選項 -r:遞迴壓縮,包括目錄下的所有檔案 [root@localhost~]# zip -r /ceshi002/ys.zip /ceshi001/ys ## 壓縮並指定壓縮包的存放位置: [root@localhost ~]# zip /opt/zls.zip 1.txt 2.txt 3.txt 4.txt ## 特性: 1.壓縮檔案後,原始檔存在 2.可以指定壓縮後儲存的路徑 3.可以壓縮目錄,也可以壓縮檔案,也可以指定多個檔案一起壓縮 4.壓縮目錄需要加選項,如果不加,壓縮後,只有一個空目錄,沒有裡面的檔案 5.解壓後,壓縮包不會消失,如果同一目錄下出現同名檔案則會詢問是否要覆蓋 ### 示例: [root@localhost~]# ll /ceshi001/ys total 0 -rw-r--r-- 1 root root 0 Apr 30 21:50 file1 -rw-r--r-- 1 root root 0 Apr 30 21:50 file2 -rw-r--r-- 1 root root 0 Apr 30 21:50 file3 -rw-r--r-- 1 root root 0 Apr 30 21:50 file4 -rw-r--r-- 1 root root 0 Apr 30 21:50 file5 [root@localhost~]# zip -r /ceshi002/zzz.zip /ceshi001/ys adding: ceshi001/ys/ (stored 0%) adding: ceshi001/ys/file1 (stored 0%) adding: ceshi001/ys/file2 (stored 0%) adding: ceshi001/ys/file3 (stored 0%) adding: ceshi001/ys/file4 (stored 0%) adding: ceshi001/ys/file5 (stored 0%) [root@localhost~]# ll /ceshi002 total 4 -rw-r--r-- 1 root root 984 May 1 02:36 zzz.zip [root@localhost~]# file /ceshi002/zzz.zip /ceshi002/zzz.zip: Zip archive data, at least v1.0 to extract # unzip解壓縮命令: unzip 壓縮包名 unzip在預設情況下,是解壓縮檔案到當前工作目錄,如果當前目錄中存在和壓縮檔案中同名的檔案,將提示使用者 [root@localhost~]# unzip /ceshi002/zzz.zip Archive: /ceshi002/zzz.zip replace ceshi001/ys/file1? [y]es, [n]o, [A]ll, [N]one, [r]ename: A extracting: ceshi001/ys/file1 extracting: ceshi001/ys/file2 extracting: ceshi001/ys/file3 extracting: ceshi001/ys/file4 extracting: ceshi001/ys/file5 ## unzip -l:檢視壓縮包裡面都有哪些檔案 (無法看見壓縮包裡檔案裡的內容) ###示例: [root@localhost~]# unzip -l /ceshi002/ys.zip Archive: /ceshi002/ys.zip Length Date Time Name --------- ---------- ----- ---- 0 04-30-2022 21:53 ceshi001/ys/ 0 04-30-2022 21:50 ceshi001/ys/file1 0 04-30-2022 21:50 ceshi001/ys/file2 0 04-30-2022 21:50 ceshi001/ys/file3 0 04-30-2022 21:50 ceshi001/ys/file4 0 04-30-2022 21:50 ceshi001/ys/file5 --------- ------- 0 6 files ## unzip -d:指定解壓路徑 [root@localhost~]# unzip /ceshi002/zzz.zip -d /ceshi002 ### 示例: [root@localhost~]# unzip /ceshi002/zzz.zip -d /ceshi002 Archive: /ceshi002/zzz.zip creating: /ceshi002/ceshi001/ys/ extracting: /ceshi002/ceshi001/ys/file1 extracting: /ceshi002/ceshi001/ys/file2 extracting: /ceshi002/ceshi001/ys/file3 extracting: /ceshi002/ceshi001/ys/file4 extracting: /ceshi002/ceshi001/ys/file5 [root@localhost~]# ll /ceshi002/ceshi001/ys total 0 -rw-r--r-- 1 root root 0 Apr 30 21:50 file1 -rw-r--r-- 1 root root 0 Apr 30 21:50 file2 -rw-r--r-- 1 root root 0 Apr 30 21:50 file3 -rw-r--r-- 1 root root 0 Apr 30 21:50 file4 -rw-r--r-- 1 root root 0 Apr 30 21:50 file5 ### 過程演示: [root@localhost~]# tree /ceshi001 /ceshi001 ├── ys │ ├── file1 │ ├── file2 │ ├── file3 │ ├── file4 │ └── file5 └── ys02 ├── aaa.txt └── abc.txt 2 directories, 7 files [root@localhost~]# mkdir /ceshi002 [root@localhost~]# zip -r /ceshi002/zzz.zip /ceshi001/ys adding: ceshi001/ys/ (stored 0%) adding: ceshi001/ys/file1 (stored 0%) adding: ceshi001/ys/file2 (stored 0%) adding: ceshi001/ys/file3 (stored 0%) adding: ceshi001/ys/file4 (stored 0%) adding: ceshi001/ys/file5 (stored 0%) [root@localhost~]# ll /ceshi002 total 4 -rw-r--r-- 1 root root 984 May 1 02:36 zzz.zip [root@localhost~]# file /ceshi002/zzz.zip /ceshi002/zzz.zip: Zip archive data, at least v1.0 to extract [root@localhost~]# unzip -l /ceshi002/zzz.zip Archive: /ceshi002/zzz.zip Length Date Time Name --------- ---------- ----- ---- 0 04-30-2022 21:53 ceshi001/ys/ 0 04-30-2022 21:50 ceshi001/ys/file1 0 04-30-2022 21:50 ceshi001/ys/file2 0 04-30-2022 21:50 ceshi001/ys/file3 0 04-30-2022 21:50 ceshi001/ys/file4 0 04-30-2022 21:50 ceshi001/ys/file5 --------- ------- 0 6 files [root@localhost~]# unzip /ceshi002/zzz.zip -d /ceshi002 Archive: /ceshi002/zzz.zip creating: /ceshi002/ceshi001/ys/ extracting: /ceshi002/ceshi001/ys/file1 extracting: /ceshi002/ceshi001/ys/file2 extracting: /ceshi002/ceshi001/ys/file3 extracting: /ceshi002/ceshi001/ys/file4 extracting: /ceshi002/ceshi001/ys/file5 [root@localhost~]# ll /ceshi002/ceshi001/ys total 0 -rw-r--r-- 1 root root 0 Apr 30 21:50 file1 -rw-r--r-- 1 root root 0 Apr 30 21:50 file2 -rw-r--r-- 1 root root 0 Apr 30 21:50 file3 -rw-r--r-- 1 root root 0 Apr 30 21:50 file4 -rw-r--r-- 1 root root 0 Apr 30 21:50 file5
壓縮命令-tar
# tar命令本身是歸檔 ## 選項:(正常使用不需要-、 特殊情況比如-C就會需要) c:歸檔 f:指定包名 z:使用gzip把歸檔檔案壓縮(.tar.gz) v:顯示壓縮/解壓的過程 x:解壓歸檔檔案 C:指定解壓的位置(路徑) t:檢視壓縮包裡的檔案都有哪些(要配合f來指定檢視的壓縮包) j:使用bzip2壓縮檔案 J:壓縮成.xz包 X:排除指定的檔案 --exclude:排除指定檔案 ## 特性 1.壓縮檔案後,原始檔存在 2.目錄和檔案都可以壓縮 3.壓縮後,壓縮包的位置可以指定任意目錄 [root@localhost ~]# tar zcf /usr/local/lx.tar.gz /etc /opt /tmp 4.可以檢視壓縮包裡有哪些檔案,但是檢視不了檔案內容 [root@localhost ~]# tar tf /usr/local/lx.tar.gz 5.一個壓縮包中,可以有多個檔案或目錄 6.解壓後,壓縮包還在,原始檔也可以隨意指定路徑 -C 7.使用zcf壓縮,zxf解壓 使用jcf壓縮,jxf解壓 使用Jcf壓縮,Jxf解壓 萬能解壓命令:xf ## 注意: 1.tar命令在解壓開檔案時,如果有檔名衝突,則不會詢問,直接覆蓋 2.tar命令,在打包時,會自動刪除絕對路徑的"/" 3.以後打包,儘量使用相對路徑,cd到需要打包目錄或檔案的上級目錄 [root@localhost ~]# cd / [root@localhost /]# tar zcf /usr/local/src/opt.tgz opt/ ## zcf舉例: [root@localhost~]# tree /ceshi003 /ceshi003 ├── AA1 │ ├── a1.txt │ ├── a2.txt │ ├── a3.txt │ ├── a4.txt │ └── a5.txt └── AA2 [root@localhost~]# tar zcf /ceshi004/ttt.tar.gz /ceshi003/AA1 tar: Removing leading `/' from member names # 這裡是警告在打包的過程中 tar命令自動刪除絕對路徑的"/" 所以: [root@localhost~]# cd / [root@localhost/]# tar zcf /ceshi004/ttt.tar.gz ceshi003/AA1 [root@localhost/]# tar tf /ceshi004/ttt.tar.gz ceshi003/AA1/ ceshi003/AA1/a1.txt ceshi003/AA1/a2.txt ceshi003/AA1/a3.txt ceshi003/AA1/a4.txt ceshi003/AA1/a5.txt 相當於是兩個步驟: 1)先歸檔 [root@localhost/]# tar cf /ceshi004/ttt.tar ceshi003/AA1 2)再使用gzip壓縮 [root@localhost/]# gzip /ceshi004/ttt.tar ## zxf舉例: ## C舉例:這個時候C需要加上-即:-C [root@localhost/]# tar zxf /ceshi004/ttt.tar.gz -C /ceshi004/B1/ [root@localhost/]# ll /ceshi004/B1/ceshi003/AA1 total 0 -rw-r--r-- 1 root root 0 May 1 03:22 a1.txt -rw-r--r-- 1 root root 0 May 1 03:22 a2.txt -rw-r--r-- 1 root root 0 May 1 03:22 a3.txt -rw-r--r-- 1 root root 0 May 1 03:22 a4.txt -rw-r--r-- 1 root root 0 May 1 03:22 a5.txt ## 萬能解壓命令:xf [root@localhost/]# tar xf /ceshi004/ttt333.tar.gz -C /ceshi004/B2 [root@localhost/]# ll /ceshi004/B2/ceshi003/AA1 total 0 -rw-r--r-- 1 root root 0 May 1 03:22 a3.txt -rw-r--r-- 1 root root 0 May 1 03:22 a4.txt -rw-r--r-- 1 root root 0 May 1 03:22 a5.txt ## X舉例: [root@localhost~]# tree /ceshi003 /ceshi003 ├── AA1 │ ├── a1.txt │ ├── a2.txt │ ├── a3.txt │ ├── a4.txt │ └── a5.txt └── AA2 [root@localhost/]# cat /ceshi001/paicu.txt a1.txt a2.txt [root@localhost/]# tar zcf /ceshi004/ttt222.tar.gz -X /ceshi001/paicu.txt ceshi003/AA1 [root@localhost/]# tar tf /ceshi004/ttt222.tar.gz ceshi003/AA1/ ceshi003/AA1/a3.txt ceshi003/AA1/a4.txt ceshi003/AA1/a5.txt ## --exclude舉例: [root@localhost/]# tar zcf /ceshi004/ttt333.tar.gz --exclude=a1.txt --exclude=a2.txt ceshi003/AA1 [root@localhost/]# tar tf /ceshi004/ttt333.tar.gz ceshi003/AA1/ ceshi003/AA1/a3.txt ceshi003/AA1/a4.txt ceshi003/AA1/a5.txt
思維導圖
![檔案的壓縮打包](C:\Users\admin\Desktop\作業_陳國寶\陳國寶 作業day_2022.4.18_檔案壓縮打包解壓縮-find\檔案的壓縮打包.jpg)