先说结论
gzip只能对文件进行压缩和解压,不能进行打包
tar可以对文件进行打包解包 还能对打包后的文件进行压缩解压缩
一般情况下不会单独使用gzip用tar命令就够了
- 用tar打压缩包
tar -czf xxx.tar.gz 要打包的文件
-v视情况加或不加 - 用tar看压缩包
tar -tf xxx.tar.gz
-z省略 -v视情况加或不加 - 用tar解压缩包
tar -xf xx.tar.gz -C 要解压的位置
-z省略 -v视情况加或不加
gzip
格式
gzip [OPTION]... [FILE]... []表示可选 ...表示可重复
作用
把指定的文件进行压缩或者解压缩
选项
- -d, --decompress 解压缩
- -l, --list 显示压缩文件的详细信息
- -r, --recursive 对文件夹中的内容递归进行压缩或者解压缩
- -v, --verbose 显示压缩或者解压缩详细过程
举例
实验环境
[root@centos76 data]# tree
.
├── a.txt
├── b.txt
├── c.txt
└── dir1
├── 1.txt
├── 2.txt
└── 3.txt
压缩单个文件
默认情况下不加选项 gzip是进行压缩操作 加上-v显示详细过程
[root@centos76 data]# gzip -v a.txt
a.txt: 80.2% -- replaced with a.txt.gz
[root@centos76 data]# ll
总用量 12
-rw-r--r--. 1 root root 40 07/29 05:52 a.txt.gz
-rw-r--r--. 1 root root 79 07/29 05:52 b.txt
-rw-r--r--. 1 root root 80 07/29 05:53 c.txt
drwxr-xr-x. 2 root root 45 07/29 06:04 dir1
对a.txt进行压缩,压缩后会把a.txt文件转换为a.txt.gz文件 a.txt.gz文件占用的空间更小
注意压缩后源文件会消失
显示压缩文件详情
-l选项可以查看压缩文件的详情 注意不能用于查看普通文件
[root@centos76 data]# gzip -l a.txt.gz
compressed uncompressed ratio uncompressed_name
40 81 80.2% a.txt
解压缩单个文件
-d选项用于解压缩 加上-v显示解压过程
[root@centos76 data]# gzip -dv a.txt.gz
a.txt.gz: 80.2% -- replaced with a.txt
[root@centos76 data]# ll
总用量 12
-rw-r--r--. 1 root root 81 07/29 05:52 a.txt
-rw-r--r--. 1 root root 79 07/29 05:52 b.txt
-rw-r--r--. 1 root root 80 07/29 05:53 c.txt
drwxr-xr-x. 2 root root 45 07/29 06:04 dir1
压缩解压缩多个文件
gzip 可以同时压缩解压缩多个文件 但是是分别对每一个文件进行压缩解压缩
[root@centos76 data]# gzip -v ./*
./a.txt: 80.2% -- replaced with ./a.txt.gz
./b.txt: 78.5% -- replaced with ./b.txt.gz
./c.txt: 83.8% -- replaced with ./c.txt.gz
gzip: ./dir1 is a directory -- ignored
[root@centos76 data]# ll
总用量 12
-rw-r--r--. 1 root root 40 07/29 05:52 a.txt.gz
-rw-r--r--. 1 root root 41 07/29 05:52 b.txt.gz
-rw-r--r--. 1 root root 37 07/29 05:53 c.txt.gz
drwxr-xr-x. 2 root root 45 07/29 06:04 dir1
[root@centos76 data]# gzip -dv ./*
./a.txt.gz: 80.2% -- replaced with ./a.txt
./b.txt.gz: 78.5% -- replaced with ./b.txt
./c.txt.gz: 83.8% -- replaced with ./c.txt
gzip: ./dir1 is a directory -- ignored
[root@centos76 data]# ll
总用量 12
-rw-r--r--. 1 root root 81 07/29 05:52 a.txt
-rw-r--r--. 1 root root 79 07/29 05:52 b.txt
-rw-r--r--. 1 root root 80 07/29 05:53 c.txt
drwxr-xr-x. 2 root root 45 07/29 06:04 dir1
看上面的例子可知
- 压缩多个文件的时候,并不会有打包操作,而是对每个文件单独压缩
- 遇到目录的时候会忽略
递归的压缩解压缩
如果需要对文件夹、子文件夹中的内容进行递归的操作 加上-r参数即可
[root@centos76 data]# gzip -rv ./*
./a.txt: 80.2% -- replaced with ./a.txt.gz
./b.txt: 78.5% -- replaced with ./b.txt.gz
./c.txt: 83.8% -- replaced with ./c.txt.gz
./dir1/1.txt: 0.0% -- replaced with ./dir1/1.txt.gz
./dir1/2.txt: 0.0% -- replaced with ./dir1/2.txt.gz
./dir1/3.txt: 0.0% -- replaced with ./dir1/3.txt.gz
[root@centos76 data]# gzip -drv ./*
./a.txt.gz: 80.2% -- replaced with ./a.txt
./b.txt.gz: 78.5% -- replaced with ./b.txt
./c.txt.gz: 83.8% -- replaced with ./c.txt
./dir1/1.txt.gz: 0.0% -- replaced with ./dir1/1.txt
./dir1/2.txt.gz: 0.0% -- replaced with ./dir1/2.txt
./dir1/3.txt.gz: 0.0% -- replaced with ./dir1/3.txt
tar
格式
tar [OPTION]... [FILE]... []表示可选 ...表示可重复
作用
把指定的文件进行打包或者解包
选项
- -c, --create 打包
- -x, --extract, --get 解包
- -t, --list 列出包中内容
- -v, --verbose 显示压缩或者解压缩详细过程
- -f, --file=ARCHIVE 指定包的名称
- -j, --bzip2 通过 bzip2 压缩包
- -z, --gzip, --gunzip, --ungzip 通过 gzip 压缩包
- -C, --directory=DIR 改变解压路径
举例
打包
-c打包 -f指定包的名字 规范是xxx.tar
注意打包后源文件不会消失
[root@centos76 data]# tar -cf package.tar ./*
[root@centos76 data]# ll
总用量 24
-rw-r--r--. 1 root root 81 07/29 05:52 a.txt
-rw-r--r--. 1 root root 79 07/29 05:52 b.txt
-rw-r--r--. 1 root root 80 07/29 05:53 c.txt
drwxr-xr-x. 2 root root 45 07/29 06:21 dir1
-rw-r--r--. 1 root root 10240 07/29 06:37 package.tar
查看包中的内容
-t 查看包中的内容 -f指定包名 加上-v信息更完整
[root@centos76 data]# tar -tf package.tar
./a.txt
./b.txt
./c.txt
./dir1/
./dir1/1.txt
./dir1/2.txt
./dir1/3.txt
[root@centos76 data]# tar -tvf package.tar
-rw-r--r-- root/root 81 2022-07-29 05:52 ./a.txt
-rw-r--r-- root/root 79 2022-07-29 05:52 ./b.txt
-rw-r--r-- root/root 80 2022-07-29 05:53 ./c.txt
drwxr-xr-x root/root 0 2022-07-29 06:21 ./dir1/
-rw-r--r-- root/root 0 2022-07-29 06:04 ./dir1/1.txt
-rw-r--r-- root/root 0 2022-07-29 06:04 ./dir1/2.txt
-rw-r--r-- root/root 0 2022-07-29 06:04 ./dir1/3.txt
解包
-x 解包 -f指定文件名 -v显示过程
# 先把原来的文件删了 只留一个package.tar
[root@centos76 data]# rm -rf *.txt
[root@centos76 data]# rm -rf dir1
[root@centos76 data]# ll
总用量 12
-rw-r--r--. 1 root root 10240 07/29 06:37 package.tar
# 解包
[root@centos76 data]# tar -xvf package.tar
./a.txt
./b.txt
./c.txt
./dir1/
./dir1/1.txt
./dir1/2.txt
./dir1/3.txt
[root@centos76 data]# ll
总用量 24
-rw-r--r--. 1 root root 81 07/29 05:52 a.txt
-rw-r--r--. 1 root root 79 07/29 05:52 b.txt
-rw-r--r--. 1 root root 80 07/29 05:53 c.txt
drwxr-xr-x. 2 root root 45 07/29 06:21 dir1
-rw-r--r--. 1 root root 10240 07/29 06:37 package.tar
解包后会不会删除包本身
打包并压缩
一般打完包后会对包进行压缩,以节省空间。
可以先用tar打包,再用gzip进行压缩,不过tar可以直接调用gzip,只需要加上-z参数。
# 先把之前的包删了
[root@centos76 data]# rm -rf package.tar
# 文件名规范 xxx.tar.gz 一目了然,先打包成tar文件,又对tar文件进行压缩,成为gz文件
[root@centos76 data]# tar -czf package.tar.gz ./*
[root@centos76 data]# ll
总用量 16
-rw-r--r--. 1 root root 81 07/29 05:52 a.txt
-rw-r--r--. 1 root root 79 07/29 05:52 b.txt
-rw-r--r--. 1 root root 80 07/29 05:53 c.txt
drwxr-xr-x. 2 root root 45 07/29 06:21 dir1
-rw-r--r--. 1 root root 267 07/29 06:54 package.tar.gz
解压缩包
-x 解包 -z表示要解的包压缩过 -f指定文件 -v显示过程
[root@centos76 data]# tar -xzvf package.tar.gz
./a.txt
./b.txt
./c.txt
./dir1/
./dir1/1.txt
./dir1/2.txt
./dir1/3.txt
其实这里指定z是告诉tar命令要用gzip解压然后再解包,如果不指定的话,tar会自动判断,所以这里-z可以省略,-v根据自己需求加或者不加
解压缩包并指定解压路径
tar默认把包解压到当前目录,加上-C参数就可以指定解压目录
tar -xf package.tar.gz -C /root/data/
[root@centos76 data]# tar -xf package.tar.gz -C /root/data/
[root@centos76 data]# ll
总用量 16
-rw-r--r--. 1 root root 81 07/29 05:52 a.txt
-rw-r--r--. 1 root root 79 07/29 05:52 b.txt
-rw-r--r--. 1 root root 80 07/29 05:53 c.txt
drwxr-xr-x. 2 root root 45 07/29 06:21 dir1
-rw-r--r--. 1 root root 267 07/29 06:54 package.tar.gz
总结
- 用tar打压缩包
tar -czf xxx.tar.gz 要打包的文件
-v视情况加或不加 - 用tar看压缩包
tar -tf xxx.tar.gz
-z省略 -v视情况加或不加 - 用tar解压缩包
tar -xf xx.tar.gz -C 要解压的位置
-z省略 -v视情况加或不加