文件壓縮與解壓縮
一般什么情況下使用文件壓縮?
備份數據,數據傳輸
節省磁盤空間
減少帶寬使用
減少負載 減少IO操作
什么情況下進行壓縮比較合適?
錯過業務高峰期,由於文件的壓縮會瞬間加大cpu的負載,所以如果壓縮的文件過大,應在服務器業務低谷期進行數據壓縮備份
tar 命令壓縮與解壓縮
語法格式:
tar zcvf 壓縮包名稱.tar.gz 要壓縮的內容 多個文件 多個目錄
如:tar zcvf 123.tar.gz 123.txt 456.txt 789.txt
參數: z gzip壓縮
c 創建
v 顯示過程
f 指定文件名稱
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
解壓
語法格式:
tar xf 壓縮包名稱
默認解壓到當前目錄下,可加參數-C來指定解壓到哪個目錄
tar xf 壓縮包名稱 -C 指定的目錄
如: tar xf 123.tar.gz -C /tmp/
查看壓縮包中的文件名稱
tar tf 123.tar.gz
批量打包文件中的內容
```python
批量打包文件中的內容
--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
```
zip 壓縮和解壓縮
打包
zip 包名字 需要打包的內容
解壓
unzip 包名字
-d 指定解壓的位置
