180天Linux小白到大神-Linux压缩打包

08.Linux压缩打包

08.Linux压缩打包

1.文件打包与压缩概述

1.1 什么是文件压缩

1.2 为什么需要文件压缩

1.3 不同系统的压缩格式互通

1.4 Linux下常见的压缩包类型

2.文件打包与压缩-gzip

2.1 gzip压缩文件

2.2 gzip 应用场景

3.文件打包与压缩-zip

3.1 zip压缩文件

3.2 unzip解压文件

4.文件打包与压缩-tar

4.1 使用tar压缩文件

4.2 使用tar列出文件

4.3 使用tar解压文件

本章课程内容大纲

1.什么是文件压缩。

2.为什么要对文件进行压缩。

  1. Windows 和 Linux 压缩如何互通。

  2. Linux 下常见压缩包类型

  3. gzip、zip、tar 工具实战

1.文件打包与压缩概述

1.1 什么是文件压缩

将多个文件或目录合并成为一个特殊的文件【压缩包】。

比如:搬家…脑补画面 images

1.2 为什么需要文件压缩

当我们在传输大量的文件时,通常都会选择将该文件进行压缩,然后在进行传输。

首先:压缩后的文件会比压缩前的文件小。一个

28G 的文件夹压缩后能达到 6G

其次:多个文件传输很慢,但单个文件传输会很

快,同时还能节省网络的消耗。

(比如:搬家时,单行李往外拿和打包后往外拿?你懂我意思?)

1.3 不同系统的压缩格式互通

Windows 的压缩包与 Linux 的压缩包能否互通? 在 windows 系统下,我们接触最多的压缩格式是

rar 或 zip

在 Linux 系统上使用最多的压缩格式是 zip 和

tar.gz

在 Linux 上的压缩格式放在 windows 系统下都是可以正常打开的。

所以一般 Windows 和 Linux 互通通常选择 zip 格式;

值得注意的是 Linux 不支持 Windows 下的 RAR 格式的压缩文件;

windows linux macos 支持 zip

tar.gz 属于linux系统特有一种格式;windows任然可

以识别并解压;

1.4 Linux下常见的压缩包类型

格式 压缩工具

.zip zip压缩工具

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

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

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

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

2.文件打包与压缩-gzip

gzip 仅能打包文件,并且打包后会删除源文件

2.1 gzip压缩文件

[root@xuliangwei ~]# yum install gzip -y

[root@xuliangwei ~]# gzip file # 对

文件进行压缩

[root@xuliangwei ~]# zcat file.gz # 查

看gz压缩后的文件

[root@xuliangwei ~]# gzip -d file.gz # 解

压gzip的压缩包

bzip2

[root@xuliangwei ~]# yum install bzip2 -y

[root@xuliangwei ~]# bzip2 file #

对文件进行压缩

[root@xuliangwei ~]# bzcat file.bz2 #

查看gz压缩后的文件

[root@xuliangwei ~]# bzip2 -d file.bz2 #

解压gzip的压缩包

2.2 gzip 应用场景

使用场景:当需要让某个配置文件不生效时,且又不想删除;

[root@xuliangwei ~]# gzip CentOS-Vault.repo

–> CentOS-Vault.repo.gz [root@xuliangwei ~]# zcat CentOS-Vault.repo.gz # --> 查看不想解压的压缩包文件内

3.文件打包与压缩-zip

使用 zip 命令可以对文件进行压缩打包,解压则需要使用 unzip 命令

3.1 zip压缩文件

1.默认最小化安装的操作系统,没有 zip 和 unzip 工具,所以需要安装

[root@xuliangwei ~]# yum install zip unzip -y

2.使用 zip 压缩文件

[root@xuliangwei ~]# zip filename.zip filename

3.使用 zip 压缩目录

[root@xuliangwei ~]# zip -r dir.zip dir/

3.2 unzip解压文件

1.解压 zip 文件包, 默认解压至当前目录

[root@xuliangwei ~]# unzip filename.zip

2.解压 zip 包内容至 /opt 目录

[root@xuliangwei ~]# unzip filename.zip -d /opt/

3.不解压压缩包,查看压缩包中的内容

[root@xuliangwei ~]# unzip -l filename.zip

[root@node ~]# wget

https://ftp.yz.yamagata-

u.ac.jp/pub/network/apache/tomcat/tomcat-

9/v9.0.50/bin/apache-tomcat-9.0.50.zip

[root@node ~]# mkdir /app [root@node ~]# unzip apache-tomcat-9.0.50.zip -d /app/

4.文件打包与压缩-tar

tar 命令是 linux 下最常用的压缩与解压缩,支持文件和目录的压缩归档;

tar 语法:tar [-zjxcvfpP] filename

选 含义 选项 含义

c 创建新的归档文 z 使用gzip压缩归档
件 后的文件(.tar.gz)

		使用bzip2压缩归	

x 对归档文件解包 j 档后的文件
(.tar.bz2)

t 列出归档文件里 J 使用xz压缩归档后
的文件列表 的文件(tar.xz)

v 输出命令的归档 C 指定解压目录位置
或解包的过程

f 指定包文件名, X 排除多个文件
多参数f写最后

	--	排除文件或目录	
	exclude		

常用打包与压缩组合命令

组合 组合含义 组合 组合含义
命令 命令

czf 打包文件为 zxf 解压tar.gz格式
tar.gz格式 文件

cjf 打包文件为 jxf 解压tar.bz格式
tar.bz格式 文件

cJf 打包文件为 xf 智能解压文件
tar.xz格式

	tf	查看压缩包文	
		件内容	

4.1 使用tar压缩文件

1.将文件或目录进行打包压缩

#1.以gzip归档方式打包并压缩

[root@xuliangwei ~]# tar czf test.tar.gz test/ test2/

#2.打包/tmp下所有文件

[root@xuliangwei /]# find /tmp -type f | xargs tar czf tmp.tar.gz [root@xuliangwei /]# tar czf tmp.tar.gz $(find /tmp -type f)

将前者执行的命令作为参数传递后者tar这个命令使用;

[root@node ~]# find ./ -maxdepth 1 -type f

! -name “.gz" -a ! -name ".zip” -a ! - name “*.bz2” | xargs tar czf root_all.tar.gz

bzip2格式

[root@node ~]# tar cjf etc.tar.bz2 /etc/ [root@node ~]# tar xf etc.tar.bz2 -C /tmp/ [root@node ~]# ls /tmp/ etc

2.排除文件,并打包压缩

#1.排除单个文件

[root@xuliangwei /]# tar czf etc.tar.gz – exclude=etc/services etc/

#2.排除多个文件

[root@xuliangwei /]# tar czf etc.tar.gz – exclude=etc/services --exclude=etc/rc.local etc/

#3.将需要排除的文件写入文件中

[root@xuliangwei /]# cat paichu.list

etc/services

etc/rc.local

etc/rc.d/rc.local

#指定需要排除的文件列表, 最后进行打包压缩

[root@xuliangwei /]# tar czfX etc.tar.gz paichu.list etc/

#示例1----------------------

[root@node ~]# tar tf etc.tar.gz | grep -E “hostname ∣ h o s t s |hosts hosts

etc/hosts

etc/sysconfig/network-scripts/hostname

etc/hostname

[root@node ~]# tar czf etc2.tar.gz – exclude=etc/hosts --exclude=etc/hostname /etc/

[root@node ~]# tar tf etc2.tar.gz | grep -

E “hostname ∣ h o s t s |hosts hosts” etc/sysconfig/network-scripts/hostname

#示例1----------------------

[root@node ~]# cat pai.txt

etc/hostname

etc/hosts

etc/passwd

etc/shadow

[root@node ~]# tar czfX etc5.tar.gz pai.txt /etc/

[root@node ~]# tar tf etc5.tar.gz |grep

“hostname$”

etc/sysconfig/network-scripts/hostname

4.2 使用tar列出文件

查看压缩包内容,但不解压

[root@xuliangwei /]# tar tf test.tar.gz

4.3 使用tar解压文件

1.默认解压文件至当前目录

#1.解压至当前目录

[root@xuliangwei /]# tar xf test.tar.gz

2.指定解压内容存储至 /opt 目录

[root@student ~]# tar xf /etc/local.tar.gz -C /tmp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值