- 6.1 壓縮打包介紹 - 6.2 gzip壓縮工具 - 6.3 bzip2壓縮工具 - 6.4 xz壓縮工具
- 6.1 壓縮打包介紹
- 6.2 gzip壓縮工具
- 6.3 bzip2壓縮工具
- 6.4 xz壓縮工具
# 6.1 壓縮打包介紹
## 常見壓縮文件
- Windows .rar .zip .7z
- Linux .zip,.gz,.bz2,.xz,
- .tar.gz,.tar.bz2,.taz.xz
## 6.2 gzip壓縮工具
首先做一個準備工作,在/tmp/下創建一個d6z的目錄,再用find命令查找/etc/目錄下的帶有字符conf的所有文件 把這些文件內容輸出到 /tmp/目錄下的一個文件裏去,把這個文件叫做1.txt
```
[[email protected] ~]# ls
111 1_heard.txt 1.txt~ 234 3.txt anaconda-ks.cfg.1
123 1_sorft.txt 1.txt.bak 2.txt.bak 4.txt biji.txt
[[email protected] ~]# cd /tmp/
[[email protected] tmp]# ls
1.txt aming.111 passwd.sh
1.txt.bak aming2 systemd-private-0e3d6e7faed94f9397845efbc07a6139-vmtoolsd.service-b9Ef5b
aming fstab xxx.conf
[[email protected] tmp]# mkdir d6z
[[email protected] tmp]# cd d6z
[[email protected] d6z]# ls
[[email protected] d6z]# find /etc/ -type f -name "*.conf"
/etc/lvm/lvm.conf
/etc/lvm/lvmlocal.conf
/etc/resolv.conf
/etc/pki/ca-trust/ca-legacy.conf
/etc/yum/pluginconf.d/fastestmirror.conf
/etc/yum/pluginconf.d/langpacks.conf
/etc/yum/protected.d/systemd.conf
/etc/yum/version-groups.conf
[[email protected] d6z]# find /etc/ -type f -name "*.conf" -exec cat {} >> 1.txt \;
[[email protected] d6z]# ls
1.txt
[[email protected]aminglinux-01 d6z]# du -sh 1.txt
252K 1.txt
[[email protected] d6z]# find /etc/ -type f -name "*.conf" -exec cat {} >> 1.txt \;
[[email protected] d6z]# du -sh 1.txt
704K 1.txt
[[email protected] d6z]# find /etc/ -type f -name "*.conf" -exec cat {} >> 1.txt \;
[[email protected] d6z]# du -sh 1.txt
1.2M 1.txt
[[email protected] d6z]# find /etc/ -type f -name "*.conf" -exec cat {} >> 1.txt \;
[[email protected] d6z]# find /etc/ -type f -name "*.conf" -exec cat {} >> 1.txt \;
命令du -sh 查看文件大小 命令wc -l 1.txt 查看文件的總行數
[[email protected] d6z]# du -sh 1.txt
2.2M 1.txt
[[email protected] d6z]# wc -l 1.txt
32360 1.txt
```
下面開始用giz去壓縮它
```
[[email protected] d6z]# du -sh 1.txt
2.2M 1.txt
這裏的2.2M大小 有點不實際,因為前面252k 我們復制了5次,2.2M有點虛的空間
[[email protected] d6z]# wc -l 1.txt
32360 1.txt
[[email protected] d6z]# gzip 1.txt
[[email protected] d6z]# ls
1.txt.gz
[[email protected] d6z]# du -sh 1.txt.gz
332K 1.txt.gz
[[email protected] d6z]# gzip -d 1.txt.gz
[[email protected] d6z]# ls
1.txt
[[email protected] d6z]# du -sh 1.txt
1.3M 1.txt
壓縮再解壓後 發現文件大小才1.3M
壓縮再解壓縮可以把那些虛的空間給擠掉
[[email protected] d6z]# du -sh 1.txt
1.3M 1.txt
[[email protected] d6z]# wc -l 1.txt
32360 1.txt
還是這麽多行
gzip 壓縮有個選項gzip-數字
(1-9的級別,1級別最松散、隨便 9級別最嚴謹 ,默認是6 不加-數字)
[[email protected] d6z]# gzip -1 1.txt
[[email protected] d6z]# du -sh 1.txt.gz
392K 1.txt.gz
之前 332k,現在392k比之前的大一些,這是因為它的壓縮級別可以調整,
這裏是最低級別,級別(1-9)
壓縮的最嚴謹,耗費cpu資源就多,壓縮的不嚴謹 耗費cpu資源最少
### gzip 解壓
1. gzip -d 文件
2. gunzip 文件
[[email protected] d6z]# gzip -1 1.txt
[[email protected] d6z]# du -sh 1.txt.gz
392K 1.txt.gz
[[email protected] d6z]# gunzip 1.txt.gz
[[email protected] d6z]# ls
1.txt
再來做一個最嚴謹的壓縮 9級別的壓縮,來看下大小和6級別一樣大
[[email protected] d6z]# gzip -9 1.txt
[[email protected] d6z]# du -sh 1.txt.gz
332K 1.txt.gz
實際上壓縮到一定的層次,基本上不會再往裏壓縮了 ,gzip 默認是6級別,取了一個適中的值
壓縮完的文件怎麽去查看呢?
不能再直接cat了,因為變成了一個二進制文件
[[email protected] d6z]# ls
1.txt.gz
[[email protected] d6z]# type 1.txt.gz
-bash: type: 1.txt.gz: 未找到
[[email protected] d6z]# file 1.txt.gz
1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Sun Aug 6 21:38:28 2017, max compression
[[email protected] d6z]# zcat 1.txt.gz
這個命令可以查看它的內容,它實際上是先解壓然後再cat
```
![mark](http://oqxf7c508.bkt.clouddn.com/blog/20170806/221434158.png?imageslim)
正常情況下1.txt 壓縮變成1.txt.gz 1.txt沒了,解壓也是,1.txt。gz 解壓變成1.txt 原來的文件 i.txt。gz 沒了
現在不想要這樣,不想要原來的文件消失
先解壓
```
[[email protected] d6z]# ls
1.txt.gz
[[email protected] d6z]# gzip -d 1.txt.gz
再壓縮1.txt 並且 把它重定向到 /tmp/1.txt.gz
[[email protected] d6z]# gzip -c 1.txt > /tmp/1.txt.gz
[[email protected] d6z]# ls
1.txt
[[email protected] d6z]# ls /tmp/1.txt.gz
/tmp/1.txt.gz
[[email protected] d6z]# file !$
file /tmp/1.txt.gz
/tmp/1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Sun Aug 6 21:38:28 2017
關於解壓 也不想刪除,直接給它指定一個地方
[[email protected] d6z]# gzip -d -c /tmp/1.txt.gz > /tmp/d6z/2.txt
-d是解壓縮,-c是指定一個路徑在/tmp/2.txt 下
[[email protected] d6z]# ls
1.txt 2.txt
[[email protected] d6z]# wc -l 1.txt 2.txt
32360 1.txt
32360 2.txt
64720 總用量
[[email protected] d6z]# du -sh *.txt
1.3M 1.txt
1.3M 2.txt
[[email protected] d6z]#
```
- [x] gzip不能壓縮目錄這是它的一個特性
## 6.3 bzip2 壓縮工具
![mark](http://oqxf7c508.bkt.clouddn.com/blog/20170807/212456786.png?imageslim)
- bzip2比gzip更加狠一點(壓縮的越狠,意味著耗費的資源就越多),默認的是9級別
```
[[email protected] d6z]# ls
1.txt 2.txt
[[email protected] d6z]# bzip2 1.txt
-bash: bzip2: 未找到命令
[[email protected] d6z]# yum install -y bzip2
```
沒有bzip2的命令,用yum命令安裝一下
```
完畢!
[[email protected] d6z]# bzip2 1.txt
[[email protected] d6z]# ls
1.txt.bz2 2.txt
[[email protected] d6z]# du -sh 1.txt.bz2
160K 1.txt.bz2
現在只有160k,壓縮的更狠
[[email protected] d6z]# du -sh 1.txt.bz2
160K 1.txt.bz2
[[email protected] d6z]# du -sh 2.txt
1.3M 2.txt
[[email protected] d6z]# bzip2 -d 1.txt.bz2
bzip2 -d 解壓
[[email protected] d6z]# bzip2 1.txt
[[email protected] d6z]# bunzip2 1.txt.bz2
也可以支持 bunzip2 這個命令也可以解壓
[[email protected] d6z]# ls
1.txt 2.txt
[[email protected] d6z]#
```
也可以支持bzip2 -c 解壓還不刪除源文件
```
[[email protected] d6z]# ls
1.txt 2.txt
[[email protected] d6z]# bzip2 -c 1.txt > /tmp/1.txt.bz2
[[email protected] d6z]# du -sh /tmp/1.txt.bz2
160K /tmp/1.txt.bz2
[[email protected] d6z]# bzip2 -d -c /tmp/1.txt.bz2 > 3.txt
[[email protected] d6z]# ls
1.txt 2.txt 3.txt
[[email protected] d6z]# du -sh 3.txt
1.3M 3.txt
[[email protected] d6z]# bzip2 -9 1.txt
[[email protected] d6z]# du -sh 1.txt.bz2
160K 1.txt.bz2
[[email protected]nglinux-01 d6z]#
bzip2默認級別就是9 所以不需要指定它
[[email protected] d6z]# file 1.txt.bz2
1.txt.bz2: bzip2 compressed data, block size = 900k
[[email protected] d6z]# ls
1.txt.bz2 2.txt 3.txt
[[email protected] d6z]# mv 1.txt.bz2 1.txt
如果把1.txt.gz 改名為 1.txt後
[[email protected] d6z]# ls
1.txt 2.txt 3.txt
[[email protected] d6z]# less 1.txt
"1.txt" may be a binary file. See it anyway?
用less查看不了,可以用file查看下 對比下1.txt 和 2.txt 顯示 2.txt是個text文件
[[email protected] d6z]# file 1.txt
1.txt: bzip2 compressed data, block size = 900k
[[email protected] d6z]# file 2.txt
2.txt: UTF-8 Unicode text, with very long lines
[[email protected] d6z]# less 2.txt
[[email protected] d6z]#
[[email protected] d6z]# mv 1.txt 1.txt.bz2
[[email protected] d6z]# ls
1.txt.bz2 2.txt 3.txt
[[email protected] d6z]#
bzip2 也支持 bzcat 查看壓縮文件
[[email protected] d6z]# bzc
bzcat bzcmp
[[email protected] d6z]# bzcat 1.txt.bz2
```
- [x] bzip2也不支持壓縮目錄
## 6.4 xz壓縮工具
![mark](http://oqxf7c508.bkt.clouddn.com/blog/20170807/222220143.png?imageslim)
xz壓縮工具比 bzip2 壓縮還要狠,一壓縮變成60k了
```
[[email protected] d6z]# ls
1.txt.bz2 2.txt 3.txt
[[email protected] d6z]# xz 2.txt
[[email protected] d6z]# ls
1.txt.bz2 2.txt.xz 3.txt
[[email protected] d6z]# du -sh 2.txt.xz
60K 2.txt.xz
[[email protected] d6z]# du -sh 3.txt
1.3M 3.txt
[[email protected] d6z]# ls
1.txt.bz2 2.txt.xz 3.txt
[[email protected] d6z]# xz -d 2.txt.xz
xz -d 解壓 unxz 也可以解壓
[[email protected] d6z]# ls
1.txt.bz2 2.txt 3.txt
[[email protected] d6z]# xz 2.txt
[[email protected] d6z]# unxz 2.txt.xz
[[email protected] d6z]# ls
1.txt.bz2 2.txt 3.txt
[[email protected] d6z]# xz -c 2.txt > /tmp/2.txt.xz
壓縮文件重定向到指定路徑下,保留原來的文件
[[email protected] d6z]# xz -d -c /tmp/2.txt.xz > ./4.txt
解壓一個xz文件然後指定到當前目錄改名為4.txt 並且保留原來的壓縮文件
[[email protected] d6z]# ls
1.txt.bz2 2.txt 3.txt 4.txt
```
- [x] xz也不支持壓縮目錄
- 6.1 壓縮打包介紹 - 6.2 gzip壓縮工具 - 6.3 bzip2壓縮工具 - 6.4 xz壓縮工具