[6] Linux文件压缩打包
6.1 压缩打包介绍
- 打包
是将多个文件或目录变成一个总的文件,文件不会变小,说不定还会变大,增加一些附加的信息来注明文件的信息,比如位置 - 压缩
将一个大的文件通过某个压缩算法变成小文件。注意压缩只是对一个文件进行操作,当要对多个文件进行压缩时就要借助于打包了,先打包再压缩。
6.2 gzip压缩工具
- gzip 不支持压缩目录
用法:gzip [选项] [文件]
-d: 解压缩时使用
-n: 压缩等级,1压缩最差,9压缩最好,6为默认
-c: 解压缩文件的源文件不会删除
- gunzip同样可以解压缩文件
- file 查看文件属性
[root@localhost ~]# file 123.txt.gz
123.txt.gz: gzip compressed data, was "123.txt", from Unix, last modified: Mon Jun 19 23:46:51 2017
- zcat 查看压缩文件内容,无需使用-d参数
[root@localhost ~]# zcat 123.txt.gz
aglajglajglajlgla
- -c参数
- 保证解压后,源文件不会消失
[root@localhost ~]# gzip -dc 123.txt.gz > /root/123.txt
[root@localhost ~]# ls
111 123.txt.gz initial-setup-ks.cfg 公共 视频 文档 音乐
123.txt anaconda-ks.cfg passwd 模板 图片 下载 桌面
- 保证压缩后,源文件不会消失
[root@localhost ~]# gzip -c 123.txt > /root/123.txt.gz
[root@localhost ~]# ls
111 123.txt.gz initial-setup-ks.cfg 公共 视频 文档 音乐
123.txt anaconda-ks.cfg passwd 模板 图片 下载 桌面
6.3 bzip压缩工具
- bzip2 不支持压缩目录
用法:zip2 [选项] [文件]
-d: 解压缩
-z: 压缩
-c: 解压缩文件的源文件不会删除
- bunzip同样可以解压缩文件,无需使用-d参数
- file 查看文件属性
[root@localhost ~]# file 123.txt.bz2
123.txt.bz2: bzip2 compressed data, block size = 900k
- bzcat 查看压缩文件内容
[root@localhost ~]# bzcat 123.txt.bz2
aglajglajglajlgla
- -c参数
- 保证解压后,源文件不会消失
[root@localhost ~]# bzip2 -dc 123.txt.bz2 > /root/123.txt
[root@localhost ~]# ls
111 123.txt.bz2 initial-setup-ks.cfg 公共 视频 文档 音乐
123.txt anaconda-ks.cfg passwd 模板 图片 下载 桌面
- 保证压缩后,源文件不会消失
[root@localhost ~]# bzip2 -c 123.txt > /root/123.txt.bz2
[root@localhost ~]# ls
111 123.txt.bz2 initial-setup-ks.cfg 公共 视频 文档 音乐
123.txt anaconda-ks.cfg passwd 模板 图片 下载 桌面
6.4 xz压缩工具
- xz 不支持压缩目录
-d: 解压缩
-z: 压缩
-c: 解压缩文件的源文件不会删除
unxz同样可以解压缩文件,无需使用-d参数
file 查看文件属性
[root@localhost ~]# file 123.txt.bz2
123.txt.bz2: bzip2 compressed data, block size = 900k
- xzcat 查看压缩文件内容
[root@localhost ~]# xzcat 123.txt.xz
aglajglajglajlgla
- -c参数
- 保证解压后,源文件不会消失
[root@localhost ~]# xz -dc 123.txt.xz > /root/123.txt
[root@localhost ~]# ls
111 123.txt.xz initial-setup-ks.cfg 公共 视频 文档 音乐
123.txt anaconda-ks.cfg passwd 模板 图片 下载 桌面
- 保证压缩后,源文件不会消失
[root@localhost ~]# xz -c 123.txt > /root/123.txt.xz
[root@localhost ~]# ls
111 123.txt.xz initial-setup-ks.cfg 公共 视频 文档 音乐
123.txt anaconda-ks.cfg passwd 模板 图片 下载 桌面
6.5 zip压缩工具
- 加上-r参数支持压缩目录,zip压缩文件无法查看压缩包内容
用法: zip [压缩包名] [源文件]
[root@localhost ~]# zip -r 111 111/
adding: 111/ (stored 0%)
adding: 111/123.txt (deflated 87%)
adding: 111/456/ (stored 0%)
[root@localhost ~]# ls
111 initial-setup-ks.cfg 公共 视频 文档 音乐 111.zip anaconda-ks.cfg passwd 模板 图片 下载 桌面
- unzip解压缩
用法: unzip [压缩包] -d [指定解压 后文件路径]
[root@localhost ~]# unzip 111,zip.zip -d /tmp/
Archive: 111,zip.zip
creating: /tmp/111/
inflating: /tmp/111/123.txt
creating: /tmp/111/456/
[root@localhost ~]# ls /tmp/
111
- unzip查看文件列表
zip压缩包无法使用工具查看压缩内容,只能使用unzip -l查看压缩列表。
[root@localhost ~]# unzip -l 111.zip
Archive: .zip
Length Date Time Name
--------- ---------- ----- ----
0 06-24-2017 20:10 111/
1162548 06-24-2017 20:09 111/123.txt
0 06-24-2017 20:10 111/456/
--------- -------
1162548 3 files
6.6 tar打包
- Linux系统中备份文件的可靠方法,几乎可以工作于任何环境中,它的使用权限是所有用户。
用法: tar [选项][打包后的文件名] [源文件]
-x: 解包或者解压缩
-c: 建立一个tar包或者压缩文件包
-v: 可视化
-f: 后面跟文件名
- 打包文件
[root@localhost ~]# tar -cvf test.tar 111 passwd 123.txt.xz
111/
111/123.txt
111/456/
passwd
123.txt.xz
- 解包文件
[root@localhost ~]# tar -xvf 111.tar
111/
111/123.txt
111/456/
- -tf查看打包文件列表
[root@localhost ~]# tar -tf test.tar
111/
111/123.txt
111/456/
passwd
123.txt.xz
- –exclude 过滤不需要打包的文件
–exclude filename 在打包或者压缩时,不要将filename文件包括在内。
[root@localhost ~]# tar cvf root.tar /root --exclude /root/111/
6.7 打包并压缩
- 打包后压缩tar包
-z: 同时用gzip压缩
-j: 同时用bzip2压缩
-J: 同时使用xz压缩
- 打包并压缩文件
[root@localhost ~]# tar -zcvf 111.tar.gz 111/
111/
111/456/
111/123.txt
[root@localhost ~]# tar -jcvf 111.tar.bz2 111/
111/
111/456/
111/123.txt
[root@localhost ~]# tar -Jcvf 111.tar.xz 111/
111/
111/456/
111/123.txt
[root@localhost ~]# ll
总用量 6172
drwxr-xr-x 3 root root 32 6月 24 20:10 111
-rw-r--r-- 1 root root 1167360 6月 24 20:50 111.tar
-rw-r--r-- 1 root root 75963 6月 24 21:16 111.tar.bz2
-rw-r--r-- 1 root root 155177 6月 24 21:16 111.tar.gz
-rw-r--r-- 1 root root 49512 6月 24 21:17 111.tar.xz
- 解包文件
[root@localhost ~]# tar -zxvf 111.tar.gz
111/
111/456/
111/123.txt
[root@localhost ~]# tar -jxvf 111.tar.bz2
111/
111/456/
111/123.txt
[root@localhost ~]# tar -Jxvf 111.tar.xz
111/
111/456/
111/123.txt
总结
vim可以查看gzip、bzip2、xz的压缩文件内容,可以查看zip、tar包的列表。
在gzip、bzip2、zip、xz的压缩工具中只有zip的压缩和解压缩区别于其他压缩方式。
比如:
1. zip的使用时必须指定压缩后的文件名。zip [file.zip] [file] ,使用方法和tar类似 tar -cvf file.tar file
2. zip解压不使用-d参数,使用unzip命令。
3. zip中可以压缩文件也可以压缩目录。而其他三个压缩工具只能压缩文件。
4. zip和tar很相似,比如zip和tar在压缩或者打包后源文件都还在,而使用其他三个压缩工具则源文件消失。