Linux 下有关压缩的那些格式----
.tar |.tar.gz | .tgz | .gz |.Z |.bz2 |.xz | ……
Linux 下有关压缩的那些命令----
1.gzip
hzn@hzn-Lenovo-XiaoXinAir-15IML-2019:~$ cp /etc/services /tmp/
hzn@hzn-Lenovo-XiaoXinAir-15IML-2019:~$ cd /tmp
-rw-r--r-- 1 root root 18774 2月 10 2019 /etc/services
hzn@hzn-Lenovo-XiaoXinAir-15IML-2019:/tmp$ ls -l /etc/services ./services*
-rw-r--r-- 1 root root 18774 2月 10 2019 /etc/services
-rw-r--r-- 1 hzn hzn 18774 1月 22 17:14 ./services
hzn@hzn-Lenovo-XiaoXinAir-15IML-2019:/tmp$ gzip -v services
services: 61.2% -- replaced with services.gz
hzn@hzn-Lenovo-XiaoXinAir-15IML-2019:/tmp$ ll /etc/services ./services.*
-rw-r--r-- 1 root root 18774 2月 10 2019 /etc/services
-rw-r--r-- 1 hzn hzn 7307 1月 22 17:14 ./services.gz
可见gzip会让源文件不复存在,压缩为.gz
hzn@hzn-Lenovo-XiaoXinAir-15IML-2019:/tmp$ gzip -d services.gz
hzn@hzn-Lenovo-XiaoXinAir-15IML-2019:/tmp$ ls -l services*
-rw-r--r-- 1 hzn hzn 18774 1月 22 17:14 services
gzip -d 可以解压.gz文件
另外 zcat 压缩文件 可查看压缩文件信息
2.bzip2
压缩比比gzip大,语法跟gzip类似
hzn@hzn-Lenovo-XiaoXinAir-15IML-2019:/tmp$ bzip2 -v services
services: 2.695:1, 2.968 bits/byte, 62.90% saved, 18774 in, 6966 out.
hzn@hzn-Lenovo-XiaoXinAir-15IML-2019:/tmp$ ll services*
-rw-r--r-- 1 hzn hzn 6966 1月 22 17:14 services.bz2
-rw-r--r-- 1 hzn hzn 7307 1月 22 17:30 services.gz
可见压缩后.bz2文件大小比.gz文件小
hzn@hzn-Lenovo-XiaoXinAir-15IML-2019:/tmp$ bzip2 -d services.bz2
hzn@hzn-Lenovo-XiaoXinAir-15IML-2019:/tmp$ ll services
-rw-r--r-- 1 hzn hzn 18774 1月 22 17:14 services
hzn@hzn-Lenovo-XiaoXinAir-15IML-2019:/tmp$ ll services*
-rw-r--r-- 1 hzn hzn 18774 1月 22 17:14 services
-rw-r--r-- 1 hzn hzn 7307 1月 22 17:30 services.gz
bzip2 -d 可以解压 services.bz2
bcat 可以查看压缩文件内容
3.xz
这个压缩比更大,语法基本相同
hzn@hzn-Lenovo-XiaoXinAir-15IML-2019:/tmp$ xz -v services
services (1/1)
100 % 6,920 B / 18.3 KiB = 0.369
hzn@hzn-Lenovo-XiaoXinAir-15IML-2019:/tmp$ ls -l services.*
-rw-r--r-- 1 hzn hzn 6966 1月 22 17:46 services.bz2
-rw-r--r-- 1 hzn hzn 7307 1月 22 17:30 services.gz
-rw-r--r-- 1 hzn hzn 6920 1月 22 17:14 services.xz
同理看到大小更小了
同理xz -d 可解压.xz文件
xcat 可看压缩文件内容
zip
这个感觉我以前在win下看到最多
压缩(以下使用递归-r)
hzn@sadl:/tmp$ mkdir a
hzn@sadl:/tmp$ cd a
hzn@sadl:/tmp/a$ mkdir aaa
hzn@sadl:/tmp/a$ touch c
hzn@sadl:/tmp/a$ touch a.c
hzn@sadl:/tmp/a$ cd aaa/
hzn@sadl:/tmp/a/aaa$ touch q
hzn@sadl:/tmp/a/aaa$ cd ..
hzn@sadl:/tmp/a$ ls
aaa a.c c
hzn@sadl:/tmp/a$ cd ..
hzn@sadl:/tmp$ zip -r a.zip /a
zip warning: name not matched: /a
zip error: Nothing to do! (try: zip -r a.zip . -i /a)
hzn@sadl:/tmp$ zip -r a.zip a
adding: a/ (stored 0%)
adding: a/aaa/ (stored 0%)
adding: a/aaa/q (stored 0%)
adding: a/a.c (stored 0%)
adding: a/c (stored 0%)
hzn@sadl:/tmp$ cd a/
hzn@sadl:/tmp/a$ cd ..
hzn@sadl:/tmp$ ls
a
a.zip
...
解压
hzn@sadl:/tmp$ cd templearn/
hzn@sadl:/tmp/templearn$ ls
google-access-helper-2.3.0.zip
hzn@sadl:/tmp/templearn$ unzip google-access-helper-2.3.0.zip
Archive: google-access-helper-2.3.0.zip
c1ab38c32ee17141826a25e306bede5236872fc3
creating: google-access-helper-2.3.0/
inflating: google-access-helper-2.3.0/README.md
creating: google-access-helper-2.3.0/_locales/
creating: google-access-helper-2.3.0/_locales/en/
inflating: google-access-helper-2.3.0/_locales/en/messages.json
creating: google-access-helper-2.3.0/_locales/zh_CN/
inflating: google-access-helper-2.3.0/_locales/zh_CN/messages.json
inflating: google-access-helper-2.3.0/bg.js
inflating: google-access-helper-2.3.0/first.html
creating: google-access-helper-2.3.0/icons/
extracting: google-access-helper-2.3.0/icons/icon-128.png
extracting: google-access-helper-2.3.0/icons/icon-16.png
extracting: google-access-helper-2.3.0/icons/icon-32.png
extracting: google-access-helper-2.3.0/icons/icon-40.png
extracting: google-access-helper-2.3.0/icons/icon.png
extracting: google-access-helper-2.3.0/icons/inactive.png
creating: google-access-helper-2.3.0/img/
inflating: google-access-helper-2.3.0/img/abs-icon-big.png
inflating: google-access-helper-2.3.0/img/chrome.png
inflating: google-access-helper-2.3.0/img/gmail.png
inflating: google-access-helper-2.3.0/img/google.png
inflating: google-access-helper-2.3.0/img/googleplus.png
extracting: google-access-helper-2.3.0/img/settings.png
creating: google-access-helper-2.3.0/lib/
inflating: google-access-helper-2.3.0/lib/sea.js
inflating: google-access-helper-2.3.0/manifest.json
inflating: google-access-helper-2.3.0/options.html
inflating: google-access-helper-2.3.0/options.js
inflating: google-access-helper-2.3.0/popup.html
inflating: google-access-helper-2.3.0/popup.js
inflating: google-access-helper-2.3.0/straightGoogle.js
inflating: google-access-helper-2.3.0/warming.html
hzn@sadl:/tmp/templearn$ ls
google-access-helper-2.3.0 google-access-helper-2.3.0.zip
tar
-v 是将正在处理的文件名显示出来
-p是保留备份数据的原有权限和属性
gzip | bzip2 | xz | ||
---|---|---|---|---|
-z | -j | -J | ||
压缩 | -c | tar -jcv -f filename.tar.bz2 | ||
解压 | -x | tar-jxv -f filename.tar.bz2 -C dirname |
hzn@sadl:/tmp$ tar -jcv -f a.tar.bz2 a
a/
a/aaa/
a/aaa/q
a/a.c
a/c
hzn@sadl:/tmp$ tar -jxv -f a.tar.bz2 -C .
a/
a/aaa/
a/aaa/q
a/a.c
a/c
更多细节不咎
关于压缩原理,可以去借鉴哈弗曼编码,哈弗曼树