20-文件压缩打包-tar,zip,gzip

Posted 人生玩家

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了20-文件压缩打包-tar,zip,gzip相关的知识,希望对你有一定的参考价值。

文件压缩打包一、压缩包格式2、为什么使用压缩3、压缩格式及命令二、压缩命令1、gzip2、zip3、压缩命令-tar3.1特性

思维导图

20-文件压缩打包-tar,zip,gzip_压缩包

一、压缩包格式

#windows:
.zip
.tar
.tar.gz
.gz
----------------------------
.rar
.7z
.bz
.bz2
.xz

2、为什么使用压缩

1、文件或目录太大, 需要压缩传输
2、服务或安装包需要解压

3、压缩格式及命令

格式

linux命令

.zip

zip

.gz

gzip

.tar

tar

.tar.gz

tar、gzip

二、压缩命令

1、gzip

#1、安装gzip命令 
yum install -y gzip

#2、gzip命令使用
gzip 普通文件名
-r 递归压缩
1 ? 03:26:42 root@yyds,10.0.0.100:~ # gzip file1.txt
0 ? 03:27:09 root@yyds,10.0.0.100:~ # ll
total 4
-rw-r--r--. 1 root root 0 Apr 18 03:25 file10.txt
-rw-r--r--. 1 root root 30 Apr 18 03:25 file1.txt.gz
-rw-r--r--. 1 root root 0 Apr 18 03:25 file2.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file3.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file4.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file5.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file6.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file7.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file8.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file9.txt
drwxr-xr-x. 2 root root 6 Apr 18 03:26 moon
0 ? 03:27:14 root@yyds,10.0.0.100:~ # gzip moon
gzip: moon is a directory -- ignored
2 ? 03:28:02 root@yyds,10.0.0.100:~ # gzip -r ./
0 ? 03:28:17 root@yyds,10.0.0.100:~ # ll
total 40
-rw-r--r--. 1 root root 31 Apr 18 03:25 file10.txt.gz
-rw-r--r--. 1 root root 30 Apr 18 03:25 file1.txt.gz
-rw-r--r--. 1 root root 30 Apr 18 03:25 file2.txt.gz
-rw-r--r--. 1 root root 30 Apr 18 03:25 file3.txt.gz
-rw-r--r--. 1 root root 30 Apr 18 03:25 file4.txt.gz
-rw-r--r--. 1 root root 30 Apr 18 03:25 file5.txt.gz
-rw-r--r--. 1 root root 30 Apr 18 03:25 file6.txt.gz
-rw-r--r--. 1 root root 30 Apr 18 03:25 file7.txt.gz
-rw-r--r--. 1 root root 30 Apr 18 03:25 file8.txt.gz
-rw-r--r--. 1 root root 30 Apr 18 03:25 file9.txt.gz
drwxr-xr-x. 2 root root 6 Apr 18 03:26 moon
0 ✓ 03:28:19 root@yyds,10.0.0.100:~ # gzip -d -r ./
0 ✓ 03:29:53 root@yyds,10.0.0.100:~ # ll
total 0
-rw-r--r--. 1 root root 0 Apr 18 03:25 file10.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file1.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file2.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file3.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file4.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file5.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file6.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file7.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file8.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file9.txt
drwxr-xr-x. 2 root root 6 Apr 18 03:26 moon


#3、特性
1、压缩文件后,源文件不存在
2、只能压缩文件,不能压缩目录
3、压缩后直接查看文件内容zcat
4、压缩后,压缩包的位置在源文件的目录下
5、一个压缩包中只有一个文件
6、解压后,压缩包没了。只有源文件

#4、解压命令
gzip -d 解压源文件

2、zip

# 1、安装zip和unzip命令
yum install -y zip
yum install -y unzip
# 2、zip命令
zip 压缩包名 文件
0 ✓ 03:58:36 root@yyds,10.0.0.100:~ # ll
total 4
-rw-r--r--. 1 root root 0 Apr 18 03:25 file1.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file2.tx
-rw-r--r--. 1 root root 0 Apr 18 03:25 file9.txt
-rw-r--r--. 1 root root 168 Apr 18 03:58 gg.zip
drwxr-xr-x. 2 root root 6 Apr 18 03:26 moon

#3、压缩并指定位置
0 ✓ 03:58:46 root@yyds,10.0.0.100:~ # zip /opt/cool file2.txt file3.txt
adding: file2.txt (stored 0%)
adding: file3.txt (stored 0%)
0 ✓ 04:01:04 root@yyds,10.0.0.100:~ # ll /opt/
total 8
-rw-r--r--. 1 root root 314 Apr 18 04:01 cool.zip
-rw-r--r--. 1 root root 314 Apr 18 03:39 star.zip



#4、特性:
1、压缩后,源文件仍然存在
2、可以指定压缩后保存的路径
3、可以压缩目录,也可以压缩文件,也可以多个文件一起压缩
4、压缩目录需要加选项-r,如果不加选项,只有一个空目录,里面没有文件
5、解压后,压缩包不会消失,如果同一目录下出现同名文件则会询问是否要覆盖

#5、选项
-r 递归压缩,包括目录下所有文件
0 ✓ 04:01:15 root@yyds,10.0.0.100:~ # zip -r /opt/cow ./
adding: moon/ (stored 0%)
adding: .bash_history (deflated 79%)
adding: file3.txt (stored 0%)
adding: file4.txt (stored 0%)
adding: file5.txt (stored 0%)
adding: file6.txt (stored 0%)
adding: file7.txt (stored 0%)
adding: file8.txt (stored 0%)
adding: file9.txt (stored 0%)
adding: file10.txt (stored 0%)
adding: file1.txt (stored 0%)
adding: file2.txt (stored 0%)
adding: gg.zip (stored 0%)
0 ✓ 04:08:55 root@yyds,10.0.0.100:~ # ll /opt
total 12
-rw-r--r--. 1 root root 314 Apr 18 04:01 cool.zip
-rw-r--r--. 1 root root 2291 Apr 18 04:08 cow.zip
-rw-r--r--. 1 root root 314 Apr 18 03:39 star.zip

#解压命令 unzip 压缩包
-解压没有指定路径,默认为当前目录下,解压最好切换到目标目录下
0 ✓ 04:20:07 root@yyds,10.0.0.100:~ # ll
total 4
-rw-r--r--. 1 root root 0 Apr 18 03:25 file10.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file1.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file2.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file3.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file4.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file5.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file6.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file7.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file8.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file9.txt
-rw-r--r--. 1 root root 168 Apr 18 03:58 gg.zip
drwxr-xr-x. 2 root root 6 Apr 18 03:26 moon
0 ✓ 04:20:26 root@yyds,10.0.0.100:~ # rm -fr /opt/*
0 ✓ 04:20:39 root@yyds,10.0.0.100:~ # ll /opt/
total 0
0 ✓ 04:20:44 root@yyds,10.0.0.100:~ # cd /opt
0 ✓ 04:22:49 root@yyds,10.0.0.100:/opt # unzip /root/gg.zip
Archive: /root/gg.zip
extracting: file1.txt
0 ✓ 04:24:39 root@yyds,10.0.0.100:/opt # ll
total 0
-rw-r--r--. 1 root root 0 Apr 18 03:25 file1.txt



# -l 查看压缩包里有哪些文件
0 ✓ 04:09:18 root@yyds,10.0.0.100:~ # unzip -l /opt/cow.zip
Archive: /opt/cow.zip
Length Date Time Name
--------- ---------- ----- ----
0 04-18-2022 03:26 moon/
996 04-17-2022 20:26 .bash_history
0 04-18-2022 03:25 file3.txt
0 04-18-2022 03:25 file4.txt
0 04-18-2022 03:25 file5.txt
0 04-18-2022 03:25 file6.txt
0 04-18-2022 03:25 file7.txt
0 04-18-2022 03:25 file8.txt
0 04-18-2022 03:25 file9.txt
0 04-18-2022 03:25 file10.txt
0 04-18-2022 03:25 file1.txt
0 04-18-2022 03:25 file2.txt
168 04-18-2022 03:58 gg.zip
--------- -------
1164 13 files

# unzip -d 指定解压路径
- 0 ✓ 04:42:52 root@yyds,10.0.0.100:/opt # unzip -d /root/ /tmp/zoo.zip
Archive: /tmp/zoo.zip
creating: /root/fog/
creating: /root/fog/pig/
creating: /root/fog/pig/aa/
0 ✓ 04:44:26 root@yyds,10.0.0.100:/opt # cd ~
0 ✓ 04:44:35 root@yyds,10.0.0.100:~ # ll
total 4
-rw-r--r--. 1 root root 0 Apr 18 03:25 file10.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file1.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file2.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file3.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file4.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file5.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file6.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file7.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file8.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file9.txt
drwxr-xr-x. 3 root root 17 Apr 18 04:39 fog
-rw-r--r--. 1 root root 168 Apr 18 03:58 gg.zip
drwxr-xr-x. 2 root root 6 Apr 18 03:26 moon
0 ✓ 04:44:37 root@yyds,10.0.0.100:~ # tree fog
fog
└── pig
└── aa

2 directories, 0 files

20-文件压缩打包-tar,zip,gzip_压缩包_02

3、压缩命令-tar

tar命令本身是归档
#选项
c:归档
f: 指定包名
z:使用gzip把归档文件压缩
v:显示压缩解压过程
x:解压归档文件
C:指定解压位置(路径)
t:查看压缩包的内容都有哪些
j:使用bzip2压缩文件
X:排除指定的文件
--exclude 排除指定文件
h:打包软链接文件
# zcf
-- x-shell 中如果是“tar、tgz、tar.gz、zip”或其它压缩后缀颜色都会标红,但不一定是压缩文件。
0 ✓ 05:31:30 root@yyds,10.0.0.100:~ # tar zcf file9.tar.gz file9.txt
0 ✓ 05:34:32 root@yyds,10.0.0.100:~ # ll

-rw-r--r--. 1 root root 0 Apr 18 03:25 file7.txt
-rw-r--r--. 1 root root 0 Apr 18 03:25 file8.txt
-rw-r--r--. 1 root root 116 Apr 18 05:34 file9.tar.gz
-rw-r--r--. 1 root root 0 Apr 18 03:25 file9.txt

# 创建过程
1)先归档
[root@localhost ~]# tar cf tmp2.tar /tmp
2)再使用gzip压缩
[root@localhost ~]# gzip tmp2.tar

# -X 排除指定文件
0 ✓ 06:27:46 root@yyds,10.0.0.100:~ # ll ./moon/
ls: cannot access ./moon/: No such file or directory
2 ✗ 06:27:59 root@yyds,10.0.0.100:~ # ll /moon/
total 12
-rw-r--r--. 1 root root 9 Apr 18 06:26 1.txt
-rw-r--r--. 1 root root 13 Apr 18 06:27 21.txt
-rw-r--r--. 1 root root 13 Apr 18 06:27 31.txt
drwxr-xr-x. 2 root root 6 Apr 18 06:26 ffg
1 ✗ 06:30:10 root@yyds,10.0.0.100:~ # vim zz.txt
0 ✓ 06:30:41 root@yyds,10.0.0.100:~ # cat zz.txt
31.txt
0 ✓ 06:30:53 root@yyds,10.0.0.100:~ # ll
total 4
-rw-r--r--. 1 root root 7 Apr 18 06:30 zz.txt
0 ✓ 06:31:57 root@yyds,10.0.0.100:~ # tar zcf lqy.tzp -X zz.txt /moon
tar: Removing leading `/ from member names
0 ✓ 06:34:33 root@yyds,10.0.0.100:~ # tar tf lqy.tzp
moon/
moon/ffg/
moon/1.txt
moon/21.txt

--exclude举例
0 ✓ 06:40:48 root@yyds,10.0.0.100:~ # ll /moon
total 12
-rw-r--r--. 1 root root 9 Apr 18 06:26 1.txt
-rw-r--r--. 1 root root 13 Apr 18 06:27 21.txt
-rw-r--r--. 1 root root 13 Apr 18 06:27 31.txt
drwxr-xr-x. 2 root root 6 Apr 18 06:26 ffg
0 ✓ 06:41:00 root@yyds,10.0.0.100:~ # tar zcf haha.tzp --clude=1.txt --clude=21.txt /moon
tar: unrecognized option --clude=1.txt
Try `tar --help or `tar --usage for more information.
64 ✗ 06:47:37 root@yyds,10.0.0.100:~ # tar zcf haha.tzp --exclude=1.txt --exclude=21.txt /moon
tar: Removing leading `/ from member names
2 ✗ 06:48:38 root@yyds,10.0.0.100:~ # tar tf haha.tzp
moon/
moon/ffg/
moon/31.txt

# 压缩包的位置可以指定任意目录
2 ✗ 07:00:46 root@yyds,10.0.0.100:~ # tar zcf /aim/yiyi/hei.tar.gz /opt car1.txt car2.txt
tar: Removing leading `/ from member names
0 ✓ 07:01:07 root@yyds,10.0.0.100:~ # cd /aim/yiyi

# tar tf 可以查看压缩包中有哪些文件,但不能查看内容
0 ✓ 07:01:33 root@yyds,10.0.0.100:/aim/yiyi # tar tf hei.tar.gz
opt/
opt/.bash_history
opt/god1.txt
opt/god2.txt
opt/god3.txt
opt/god4.txt
opt/god5.txt
opt/fog/
opt/fog/pig/
opt/fog/pig/aa/
car1.txt
car2.txt

3.1特性

20-文件压缩打包-tar,zip,gzip_压缩包_03

# 特性
1.压缩文件后,源文件存在
2.目录和文件都可以压缩
3.压缩后,压缩包的文件位置可以指定任意目录
4.tar tf 可以查看压缩包有哪些文件,但查看不了文件内容
5.一个压缩包中可有多个文件或目录
6.解压后,压缩包还在,源文件也在,还可以指定解压路径 -C
7.使用zcf压缩,zxf解压
使用jcf压缩,jxf解压
使用Jcf压缩,Jxf解压

万能解压命令 xf
#注意事项:
1.tar命令在解压文件时,如果有文件名冲突则不会访问,直接覆盖
2。tar命令在打包时,会自动删除绝对路径的“/”
3.打包,尽量使用相对路径,cd到需要打包的目录或文件的上一级目录

0 ✓ 07:22:07 root@yyds,10.0.0.100:/moon/star # cd /
0 ✓ 07:23:21 root@yyds,10.0.0.100:/ # tar zcf babay.tzp root/car3.txt
2 ✗ 07:23:52 root@yyds,10.0.0.100:/ # tar tf babay.tzp
root/car3.txt
# 避免出现删根的情况和提示,可以先切换到要压缩的目录下面,
207:00:46 root@yyds,10.0.0.100:~ # tar zcf /aim/yiyi/hei.tar.gz /opt car1.txt car2.txt
tar: Removing leading `/ from member names
0 ✓ 07:01:07 root@yyds,10.0.0.100:~ # cd /aim/yiyi

0 ? 18:30:47 root@yyds,10.0.0.100:~ # cd /opt
0 ? 18:32:16 root@yyds,10.0.0.100:/opt # tar zcf /root/heihei.tar.gz fog god1.txt
0 ? 18:33:44 root@yyds,10.0.0.100:/opt # ll /root
total 8
-rw-r--r--. 1 root root 25 Apr 18 18:21 1.txt
-rw-r--r--. 1 root root 0 Apr 18 18:22 2.txt
-rw-r--r--. 1 root root 0 Apr 18 18:22 3.txt
-rw-r--r--. 1 root root 0 Apr 18 18:22 4.txt
-rw-r--r--. 1 root root 0 Apr 18 18:22 5.txt
-rw-r--r--. 1 root root 813 Apr 18 18:33 heihei.tar.gz
0 ? 18:34:00 root@yyds,10.0.0.100:/opt # file /root/heihei.tar.gz
/root/heihei.tar.gz: gzip compressed data, from Unix, last modified: Mon Apr 18 18:33:44 2022
0 ? 18:34:11 root@yyds,10.0.0.100:/opt # tar tf /root/heihei.tar.gz
fog/
fog/pig/
fog/pig/aa/
god1.txt
# 压缩包不能添加文件 否则压缩包会损坏,无法解压和查看
0 ? 18:38:41 root@yyds,10.0.0.100:~ # cp 3.txt heihei.tar.gz
0 ? 18:39:01 root@yyds,10.0.0.100:~ # tar tf heihei.tar.gz
tar: This does not look like a tar archive

gzip: stdin: unexpected end of file
tar: Child returned status 1
tar: Error is not recoverable: exiting now

20-文件压缩打包-tar,zip,gzip_压缩包_04

以上是关于20-文件压缩打包-tar,zip,gzip的主要内容,如果未能解决你的问题,请参考以下文章

[拾 得] zip gzip bzip2 & tar 压缩/打包 四大金刚

Linux压缩和解压包常用命令:gzip tar zip/unzip命令使用示例

Linux的压缩命令(tar,gzip,zip)

跪求!ZIP.GZIP.TAR有什么区别?那个压缩的程度大?急!

Linux文件压缩和打包

打包压缩命令