檔案的壓縮與解壓
阿新 • • 發佈:2020-11-07
為什麼要使用檔案壓縮 什麼樣樣的情況下使用壓縮
備份 資料傳輸 從伺服器把資料備份到備份伺服器 需要打包
1)節省磁碟空間
2)減少寬頻使用
3)減少負載 減少IO口使用
tar命令
語法格式: tar zcvf 包名.tar.gz 打包內容 多個檔案 多個目錄 引數: z gzip 壓縮 c 建立 v 顯示過程 f 指定文名稱 簡寫 zcf 批量打包 --exclude=PATTERN 排除不需要打包的檔案 --exclude-from=FILE 將不需要的檔案建立一個檔案 排除檔案裡的打包 x 解壓縮 C 指定解壓位置 t 檢視檔案中的檔名稱
打包
打包檔案
[root@oldboyedu ~]# #打包當前的hosts檔案
[root@oldboyedu ~]# tar zcvf hosts.tar.gz hosts
打包多個檔案
[root@oldboyedu ~]# tar zcvf hosts.tar.gz hosts oldboy.txt passwd
hosts
oldboy.txt
passwd
打包目錄 [root@oldboyedu ~]# tar zcf etc.tar.gz /etc tar: Removing leading `/' from member names # 如果全路徑打包會提示從成員中刪除/ 保護系統安全 不讓提示 使用相對路徑打包 [root@oldboyedu ~]# cd / [root@oldboyedu /]# tar zcf etc.tar.gz etc [root@oldboyedu /]#
打包後的檔案直接放在某個目錄
[root@oldboyedu /]# ll /opt/
total 0
[root@oldboyedu /]# tar zcf /opt/etc.tar.gz etc
[root@oldboyedu /]# ll opt/
total 10012
-rw-r--r-- 1 root root 10248462 Nov 6 10:42 etc.tar.gz
批量打包 --exclude=PATTERN 排除不需要打包的檔案 [root@oldboyedu ~]# tar zcvf test.tar.gz --exclude=all.hosts ./* ./all.tar.gz ./dir/ ./dir/oldboy/ ./hehe.txt ./hosts ./oldboy.txt ./passwd ./test. ./test.avi ./test.sh -exclude-from=FILE將不需要的檔案建立一個檔案 排除檔案裡的打包 [root@oldboyedu ~]# tar zcvf test.tar.gz --exclude-from=exclude.txt ./* ./exclude.txt ./passwd ./test. ./test.avi ./test.sh
解壓
tar xf 包名
[root@oldboyedu opt]# tar xf hosts.tar.gz
解壓到固定的位置
tar xf 包名 -C 路徑 表示解壓到指定位置
[root@oldboyedu ~]# ll /opt/
total 0
[root@oldboyedu ~]# tar xf /tmp/hosts.tar.gz -C /opt/
[root@oldboyedu ~]# ll /opt/
total 82516
-rw-r--r-- 1 root root 84485563 Nov 5 10:43 hosts
-rw-r--r-- 1 root root 85 Nov 4 10:56 oldboy.txt
-rw-r--r-- 1 root root 986 Nov 6 10:16 passwd
檢視壓縮包中的檔名稱
...
[root@oldboyedu ~]# tar tf /tmp/hosts.tar.gz
hosts
oldboy.txt
passwd
zip打包與解壓
打包
zip 包名.zip 需要打包的內容
解壓
unzip 包名字
-d 指定解壓的位置