压缩打包
  DcpJeOZ6VzTX 2023年11月02日 29 0

压缩打包

Linux系统常见的压缩包有那些类型

格式

压缩工具

.zip

zip压缩工具

.gz

gzip压缩工具,只能压缩文件,会删除源文件(通常配合tar使用)

.bz2

bzip2压缩工具,只能压缩文件,会删除源文件(通常配合tar使用)

.tar.gz

先使用tar命令归档打包,然后使用gzip压缩

.tar.bz2

先使用tar命令归档打包,然后使用bzip压缩

windows下我们接触最多的压缩文件就是.rar格式, 但Linux有自己所特有的压缩工具。 如果希望windows和Linux互相能使用的压缩工具, 建议.zip格式

gzip

# 安装gzip
yum install -y gzip

# 语法
gzip 选项 文件名...
gzip [option] [file...]
-#压缩
gzip f1
-#解压
gzip -d f1.gz

# 选项
-r 指定目录,将目录下的所有文件都单独压缩成gz包
	#(目录不会变压缩包,目录下的所有文件会变成压缩包)
	[root@localhost ~]# ll
	total 0
	drwxr-xr-x. 2 root root 58 Mar 29 17:52 dir1
	[root@localhost ~]# gzip -r dir1 (解压用gzip -rd dir1/)
	[root@localhost ~]# ll dir1/
	total 16
	-rw-r--r--. 1 root root 26 Mar 29 17:52 file6.gz
	-rw-r--r--. 1 root root 26 Mar 29 17:52 file7.gz
	-rw-r--r--. 1 root root 26 Mar 29 17:52 file8.gz
	-rw-r--r--. 1 root root 26 Mar 29 17:52 file9.gz
	[root@localhost ~]# ll
	total 0
	drwxr-xr-x. 2 root root 70 Mar 29 17:53 dir1

-d 解压(gzip -d 加压缩包名)
	[root@localhost ~]# gzip -d f1.gz

# 压缩file10文件
	gzip file10

# 特性
## gzip压缩文件 ,源文件消失
[root@localhost ~]# ll
total 12
-rw-r--r--. 1 root root   0 Mar 29 17:36 f1
-rw-r--r--. 1 root root   0 Mar 29 17:36 f2
-rw-r--r--. 1 root root   0 Mar 29 17:36 f3
-rw-r--r--. 1 root root   0 Mar 29 17:36 f4
-rw-r--r--. 1 root root   0 Mar 29 17:36 f5
[root@localhost ~]# gzip f1
[root@localhost ~]# ll
total 16
-rw-r--r--. 1 root root  23 Mar 29 17:36 f1.gz
-rw-r--r--. 1 root root   0 Mar 29 17:36 f2
-rw-r--r--. 1 root root   0 Mar 29 17:36 f3
-rw-r--r--. 1 root root   0 Mar 29 17:36 f4
-rw-r--r--. 1 root root   0 Mar 29 17:36 f5

## gzip压缩文件,每个文件都是单独的压缩包
[root@localhost ~]# gzip f* (将f开头的文件全部压缩)
gzip: f1.gz already has .gz suffix -- unchanged
[root@localhost ~]# ll
total 32
-rw-r--r--. 1 root root  23 Mar 29 17:36 f1.gz
-rw-r--r--. 1 root root  23 Mar 29 17:36 f2.gz
-rw-r--r--. 1 root root  23 Mar 29 17:36 f3.gz
-rw-r--r--. 1 root root  23 Mar 29 17:36 f4.gz
-rw-r--r--. 1 root root  23 Mar 29 17:36 f5.gz

## gzip默认只能压缩文件,不能压缩目录
[root@localhost ~]# gzip xxx
gzip: xxx is a directory -- ignored

## zcat能查看压缩包里的文件内容
[root@localhost ~]# echo '123' >f1
[root@localhost ~]# gzip f1
[root@localhost ~]# zcat f1.gz
123

## 当解压的时候,源文件出来,压缩包会消失
[root@localhost ~]# ll
total 32
-rw-r--r--. 1 root root  27 Mar 29 17:43 f1.gz
-rw-r--r--. 1 root root  23 Mar 29 17:36 f2.gz
-rw-r--r--. 1 root root  23 Mar 29 17:36 f3.gz
-rw-r--r--. 1 root root  23 Mar 29 17:36 f4.gz
-rw-r--r--. 1 root root  23 Mar 29 17:36 f5.gz
[root@localhost ~]# gzip -d f1.gz
[root@localhost ~]# ll
total 32
-rw-r--r--. 1 root root   4 Mar 29 17:43 f1
-rw-r--r--. 1 root root  23 Mar 29 17:36 f2.gz
-rw-r--r--. 1 root root  23 Mar 29 17:36 f3.gz
-rw-r--r--. 1 root root  23 Mar 29 17:36 f4.gz
-rw-r--r--. 1 root root  23 Mar 29 17:36 f5.gz

zip

# 安装zip
yum install -y zip unzip

# 语法
zip [选项] [压缩包名] [文件名...]
-r 递归处理,把目录下所有文件都压缩进去
-#压缩
zip 1.zip 1
-#解压
unzip 1.zip

# 特性
## zip打包,保留源文件
[root@localhost ~]# ll
total 4
-rw-r--r--. 1 root root   0 Mar 29 16:57 1
[root@localhost ~]# zip 1.zip 1
  adding: 1 (stored 0%)
[root@localhost ~]# ll
total 8
-rw-r--r--. 1 root root   0 Mar 29 16:57 1
-rw-r--r--. 1 root root 152 Mar 29 17:07 1.zip

## unzip解压压缩包,压缩包不会消失
[root@localhost ~]# unzip 1.zip
Archive:  1.zip
replace 1? [y]es, [n]o, [A]ll, [N]one, [r]ename: r
new name: 1.zip.1
 extracting: 1.zip.1                 
[root@localhost ~]# ll
total 8
-rw-r--r--. 1 root root   0 Mar 29 16:57 1
-rw-r--r--. 1 root root 152 Mar 29 17:07 1.zip
-rw-r--r--. 1 root root   0 Mar 29 16:57 1.zip.1

## zip可以打包目录,但是不加-r纯粹打包空目录
[root@localhost ~]# ll
total 0
drwxr-xr-x. 2 root root 103 Mar 29 16:02 xxx
[root@localhost ~]# cd xxx/
[root@localhost xxx]# ll
total 4
-rw-r--r--. 1 root root   0 Mar 29 15:43 file1
-rw-r--r--. 1 root root   0 Mar 29 15:43 file2
-rw-r--r--. 1 root root   0 Mar 29 15:43 file3
-rw-r--r--. 1 root root   0 Mar 29 15:43 file4
-rw-r--r--. 1 root root   0 Mar 29 15:43 file5
-rw-r--r--. 1 root root   0 Mar 29 15:43 file5.0
-rw-r--r--. 1 root root 160 Mar 29 16:01 file5.zip
[root@localhost xxx]# cd

## 不加-r ##
[root@localhost ~]# zip xxx.zip xxx
  adding: xxx/ (stored 0%)
[root@localhost ~]# ll
total 4
drwxr-xr-x. 2 root root 103 Mar 29 16:02 xxx
-rw-r--r--. 1 root root 158 Mar 29 16:24 xxx.zip
[root@localhost ~]# mv xxx.zip /tmp
[root@localhost ~]# cd /tmp
[root@localhost tmp]# ll
total 4
-rw-r--r--. 1 root root 158 Mar 29 16:24 xxx.zip
[root@localhost tmp]# unzip xxx.zip
Archive:  xxx.zip
   creating: xxx/
   
## 加-r ##
[root@localhost ~]# zip -r xx.zip xxx
updating: xxx/ (stored 0%)
  adding: xxx/file1 (stored 0%)
  adding: xxx/file2 (stored 0%)
  adding: xxx/file3 (stored 0%)
  adding: xxx/file4 (stored 0%)
  adding: xxx/file5 (stored 0%)
  adding: xxx/file5.zip (stored 0%)
  adding: xxx/file5.0 (stored 0%)
[root@localhost ~]# mv xx.zip /opt
[root@localhost ~]# cd /opt
[root@localhost opt]# ll
total 4
drwxr-xr-x. 2 root root   16 Mar 14 18:15 xxx
-rw-r--r--. 1 root root 1352 Mar 29 16:26 xx.zip
[root@localhost opt]# unzip xx.zip
Archive:  xx.zip
 extracting: xxx/file1               
 extracting: xxx/file2               
 extracting: xxx/file3               
 extracting: xxx/file4               
 extracting: xxx/file5               
 extracting: xxx/file5.zip           
 extracting: xxx/file5.0             

## zip如何解压 unzip 默认解压到当前目录
[root@localhost ~]# unzip 2.zip
Archive:  2.zip
replace 2? [y]es, [n]o, [A]ll, [N]one, [r]ename: r
new name: 2.zip.1
 extracting: 2.zip.1                 
[root@localhost ~]# ll
total 12
-rw-r--r--. 1 root root   0 Mar 29 16:57 2
-rw-r--r--. 1 root root 152 Mar 29 17:22 2.zip
-rw-r--r--. 1 root root   0 Mar 29 16:57 2.zip.1

## unzip -d 可以解压到其他指定的路径下
[root@localhost ~]# unzip xxx.zip -d /opt/
Archive:  xxx.zip
   creating: /opt/xxx/
 extracting: /opt/xxx/file1          
 extracting: /opt/xxx/file2          
 extracting: /opt/xxx/file3          
 extracting: /opt/xxx/file4          
 extracting: /opt/xxx/file5          
[root@localhost ~]# cd /opt
[root@localhost opt]# ll
total 0
drwxr-xr-x. 2 root root 71 Mar 29 17:00 xxx

tar

# 语法
tar 选项 包名 文件名
-#压缩
tar zcf log.tar.gz log
-#解压
tar xf log.tar.gz

# 选项
c 给文件做归档
f 后面必须接文件名
z 以gzip格式打包
v 显示过程
x 解压归档文件
C 解压指定的目录
t 列出归档文件里的文件列表
j 使用bzip2压缩归档后的文件(.tar.bz2)
J 使用xz压缩归档后的文件(tar.xz)X 排除多个文件(写入需要排除的文件名称)
h 打包软连接
P 连带绝对路径打包

# 了解即可
--hard-dereference //打包硬链接
--exclude //在打包的时候写入需要排除文件或目录

# 万能解压
tar xf

# 打包tar.gz的包
tar zcf log.tar.gz log
# 打包.xz的包
tar Jcf xxx.tar.xz xxx
# 打包.bz2的包
tar jcf xxx.tar.bz2 xxx
# 选项无论怎么组合 f后面必须加文件名 f必须放最后!!!!!!!
# tar有个安全机制,如果是以绝对路径打包,则会删除前面的/
# 为了避免这种情况出现避免解压开的时候覆盖原目录内容要压缩那个目录,就去这个目录的上一级,使用相对路径
# tar解压后,直接覆盖相同文件,不会提示是否覆盖

# 排除单个文件练习题
tar zcf log.tar.gz --exclude=10.log --
exclude=9.log /opt/
-X 指定需要排除的文件
cd /
tar zcf log.tar.gz -X paichu opt/

# 查看压缩包内容和解压
[root@zls /]# tar tf  test.tar.gz

# 将tar.gz解压至其他目录
[root@student ~]# tar xf /etc/local.tar.gz  -C /tmp

练习题

1.如何使用gzip命令对文件进行压缩、解压
压缩:gzip加要压缩的文件名
解压:gzip -d 加要解压的文件名(注意不是压缩之后的名字)
2.如何用zip命令对文件以及目录进行压缩、解压
压缩:zip -r加压缩包名 加要压缩的目录
解压:用unzip 加压缩包名
3.创建一个自己名字的文件至/opt目录
touch /opt/wxk
touch wxk     CP wxk /opt/
4.打包opt整个目录,并命名test_opt.tar.gz
先cd / ,再tar -zcf test_tar.gz opt
5.查看打包好的test_opt.tar.gz里的文件
tar -tf test_opt.tar.gz(cd /下才可以查看)
6.将打包好的test_opt.tar.gz内容指定解压至/tmp目录
tar -xf test_opt.tar.gz -C tmp
7.打包etc目录下的所有文件,不要目录只要文件
先cd到/ 下面 find etc/ -type f | xargs tar zcf etc7.tar.gz 
8.打包etc目录下的所有文件,排除passwd,shadow
先cd到/ find etc/ -type f | xargs tar zcf etc8.tar.gz --exclude=etc/{passwd,shadow}
tar zcf etc8.tar.gz --exclude=etc/{passwd,shadow} /etc/
9.打包etc目录下的所有以p开头的文件
 先cd到/ find etc/ -type f -name 'p*' | xargs tar zcf etc9.tar.gz
10.打包etc目录下所有大于1M的文件
先cd到/ find etc/ -size +1M | xargs tar zcf etc10.tar.gz
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

上一篇: rpm管理 下一篇: 文件查找Find
  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读