Linux压缩和解压缩

一、生成测试文件(dd命令)

1.生成一个大小为100M的全是二进制的0的文件

# 生成100MB的全零文件
[root@xnha ~]# cd /tmp
[root@xnha tmp]# dd if=/dev/zero of=/tmp/testfile bs=1M count=100 #若原本文件中有内容则从头开始覆盖100M
记录了100+0 的读入 #100指完整读取的块数,0指部分读取的快数
记录了100+0 的写出 #同理
104857600 bytes (105 MB, 100 MiB) copied, 0.0855957 s, 1.2 GB/s #总共复制的字节数 耗时 平均速度
# 为什么要重复100次不直接写入100M?
# 1.系统资源限制:dd 在每次读写时会分配一个缓冲区,大小等于 bs 的值。如果设置 bs=100M,dd 需要一次性分配 100MB 的内存缓冲区。在内存有限的系统上,这可能会导致I/O性能问题,甚至因内存不足而失败。
# 2.中断和错误处理:如果用大块大小(bs=100M)且操作中断,可能丢失整个 100MB 的写入进度。而小块大小(bs=1M)中断时,已写入的部分已完成,损失较小。
[root@xnha tmp]# ll /tmp |grep testfile #列出/tmp下所有文件目录详细信息→筛选
-rw-r--r--. 1 root root 104857600 3月 9 10:29 testfile
[root@xnha tmp]# ll -h /tmp |grep testfile #-h 输出人类能看懂的大小
-rw-r--r--. 1 root root 100M 3月 9 10:29 testfile
# 参数说明
if=/dev/zero # 输入源(Linux一个特殊虚拟设备文件,读取会返回无限的零字节) if = input file
• of=/tmp/testfile # 输出文件路径 of = output file
• bs=1M # 块大小(1MB)
• count=100 # 块数量

image-20250803135012229

设备文件 输出内容 典型用途
/dev/zero 无限连续的0x00字节 创建空白文件,初始化存储空间
/dev/null 黑洞设备(丢弃所有写入) 屏蔽程序输出

**/dev/null **

  • 写入:任何写入 /dev/null 的数据都会被系统丢弃,不占用磁盘空间。
  • 读取:从 /dev/null 读取数据会立即返回空(EOF,文件结束标志),不会产生任何实际数据。

2. 查看文件

[root@xnha ~]# file /tmp/testfile #file 识别文件类型
/tmp/testfile: data #由于/tmp/testfile是一个全零文件,file命令通常会识别它为数据文件,因为它没有特定的文件格式。
[root@xnha ~]# file /tmp/file111.txt
/tmp/file111.txt: ASCII text
[root@xnha tmp]# cat testfile #空白文件
[root@xnha tmp]# hexdump testfile # 使用十六进制的方法查看文件
0000000 0000 0000 0000 0000 0000 0000 0000 0000 #起始文件偏移量16 内容
* #重复内容省略
6400000 #文件结束偏移量
[root@xnha tmp]#
image-20250803135921798

二、压缩命令 gzip

命令格式:

gzip [选项] 文件名
gunzip [选项] 文件名.gz # 解压等价于gzip -d

选项解读:

参数 说明
-d 解压文件 ( gunzip 的等价操作)
-k 保留原始文件(压缩/解压后不删除)
-1--9 压缩级别: 1 最快压缩率最低, -9 最慢压缩率最高(默认 -6 )
-v 显示压缩率等详细信息
-c 输出到标准输出(不修改文件)
-t 测试压缩文件完整性
-r 递归处理目录(需结合 tar 使用)
-l 查看压缩文件信息(压缩前后大小、压缩率)

使用方法:

1.基础压缩

[root@xnha tmp]# gzip testfile #生成压缩文件testfile.gz 并删除源文件
[root@xnha tmp]# ll -h /tmp | grep testfile
-rw-r--r--. 1 root root 100K 3月 9 10:29 testfile.gz
[root@xnha tmp]# file testfile.gz
testfile.gz: gzip compressed data, was "testfile", last modified: Sun Mar 9 14:29:48 2025, from Unix, original size 104857600 #文件路径 文件格式 压缩前名称 最后修改时间 源系统 原始文件大小
[root@xnha tmp]# gzip -k testfile #生成testfile.gz,并保留源文件
[root@xnha tmp]# ll -h /tmp | grep testfile
-rw-r--r--. 1 root root 100M 3月 9 10:29 testfile
-rw-r--r--. 1 root root 100K 3月 9 10:29 testfile.gz

image-20250803140224283

2.解压操作

[root@xnha tmp]# gunzip testfile.gz #解压不保留.gz文件
[root@xnha tmp]# ll -h /tmp | grep testfile
-rw-r--r--. 1 root root 100M 3月 9 10:29 testfile
[root@xnha tmp]# gzip -d -k testfile.gz # 解压并保留.gz文件
gzip: testfile already exists; do you wish to overwrite (y or n)? y

image-20250803140619667

3.查看压缩内容

zcat file.gz # 不解压查看内容
gzip -l backup.gz # 显示压缩率:
compressed uncompressed ratio uncompressed_name
736513 2473400 70.3% backup

image-20250803140932795

三、压缩命令bzip2

bzip2 是 Linux 中基于 Burrows-Wheeler 变换 的高效压缩工具,相比 gzip 压缩率更高,但速度较
慢。压缩后生成 .bz2 后缀文件,默认删除原始文件。

命令格式:

bzip2 [选项] 文件名 # 压缩文件
bunzip2 [选项] 文件名.bz2 # 解压(等价于 bzip2 -d)

参数解读:

参数 说明
-d 解压文件(同 bunzip2 )
-k 保留原始文件(压缩/解压后不删除)
-1--9 压缩级别: -1 最快压缩率最低, -9 最慢压缩率最高(默认 -9 )
-v 显示压缩进度和详细信息
-c 输出到标准输出(不修改文件)
-t 测试压缩文件完整性
-f 强制覆盖已存在的输出文件
-s 降低内存占用(牺牲速度)
-z 显式指定压缩模式(默认行为)

使用方法:

1.基础压缩

[root@xnha tmp]# mkdir 222 #创建222目录
[root@xnha tmp]# cp /tmp/testfile /tmp/222 #复制文件testfile到222目录下
[root@xnha tmp]# cd 222/ #进入222目录
[root@xnha 222]# ls #查看
testfile
[root@xnha 222]# bzip2 testfile #生成压缩文件
testfile.bz2,删除原文件
[root@xnha 222]# ll -h testfile.bz2 #查看文件信息
-rw-r--r--. 1 root root 113 3月 9 11:00 testfile.bz2
bzip2 -k data.log #保留原文件

image-20250803141643372

2.解压操作

[root@xnha 222]# bunzip2 testfile.bz2 #解压并删除.bz2 文件
[root@xnha 222]# ls testfile #查看
[root@xnha 222]# bzip2 -dk testfile.bz2 #解压并保留 .bz2 文件
[root@xnha 222]# ls #查看
testfile testfile.bz2

image-20250803142135832

3.查看压缩内容

bzcat file.bz2 # 不解压查看内容
bzip2 -l archive.bz2 # 显示压缩信息:
Compression ratio: 4.20:1 #压缩比
compressed size: 102400 bytes #压缩后大小
uncompressed size: 430080 bytes #压缩前大小

image-20250803142728252

四、打包命令tar

实验目标

建立两个大小为10M 和 20M的文件 分别命名为big1和big2将其放入/tmp/test123 文件夹中。尝试对文
件夹进行压缩

1.创建两个文件

[root@xnha 222]# dd if=/dev/zero of=big2 bs=20M count=1
记录了1+0 的读入
记录了1+0 的写出
20971520 bytes (21 MB, 20 MiB) copied, 0.0111189 s, 1.9 GB/s
[root@xnha 222]# dd if=/dev/zero of=big1 bs=10M count=1
记录了1+0 的读入
记录了1+0 的写出
10485760 bytes (10 MB, 10 MiB) copied, 0.00505431 s, 2.1 GB/s
[root@xnha 222]# ls #查看
big1 big2

image-20250803142833395

2.创建目录,移动文件

[root@xnha 222]# cd /tmp #切换到/tmp目录
[root@xnha tmp]# mkdir test123 #创建目录/test123
[root@xnha test123]# mv /tmp/222/big[12] /tmp/test123/ #将big1和big2同时移到/tmp/test123/
[root@xnha test123]# ls #查看
big1 big2

image-20250803143157237

image-20250803143214748

3.尝试使用gzip和bzip2对test123文件夹进行压缩(报错)

[root@xnha tmp]# gzip test123
gzip: test123 is a directory -- ignored
[root@xnha tmp]# bzip2 test123
bzip2: Input file test123 is a directory.

image-20250803143303448

tar命令功能

tar (Tape Archive)是 Linux 系统中用于 打包文件/目录 的归档工具,支持保留文件权限、时间戳和
目录结构(不对文件/目录本身造成任何影响,产生新文件)。常与压缩工具( gzip 、bzip2 、xz )
结合使用,形成 .tar.gz 、.tar.bz2 、.tar.xz 等压缩包

基础语法:

tar [主选项] [辅选项] 文件名或目录

参数说明:

主选项模式(必选其一)
参数 说明
-c 创建新归档文件(Create)
-x 解压归档文件(eXtract)
-t 查看归档内容列表(List)
-r 追加文件到已有归档(需先解压再重新打包)
常用辅选项
参数 说明
-f 指定归档后的文件位置(必须放在最后)
-v 显示操作详情(Verbose)
-z 使用 gzip 压缩(生成 .tar.gz )
-j 使用 bzip2 压缩(生成 .tar.bz2 )
-J 使用 xz 压缩(生成 .tar.xz )
-C 解压到指定目录(Change directory)
–exclude 排除指定文件/目录
–wildcards 使用通配符匹配文件

典型使用场景

1.打包与压缩
# 打包目录(不压缩)
[root@xnha tmp]# tar -cf /root/test321.tar test123 #-c 创建新归档文件 -f 指定文件名 /root/test321.tar打包生成文件位置 test123指定打包的目录为当前目录/tmp下的 test123
文件名(必须放最后,后面跟文件名) 只能是-cf
#打包的文件需要用相对路径,用绝对路径会将tmp一起打包
[root@xnha tmp]# ll -h /root
总用量 31M
drwxr-xr-x. 2 root root 6 3月 3 21:51 公共
drwxr-xr-x. 2 root root 6 3月 3 21:51 模板
drwxr-xr-x. 2 root root 6 3月 3 21:51 视频
drwxr-xr-x. 2 root root 6 3月 3 21:51 图片
drwxr-xr-x. 2 root root 6 3月 3 21:51 文档
drwxr-xr-x. 2 root root 6 3月 3 21:51 下载
drwxr-xr-x. 2 root root 6 3月 3 21:51 音乐
drwxr-xr-x. 2 root root 6 3月 4 01:57 桌面
-rw-------. 1 root root 1.5K 3月 3 21:50 anaconda-ks.cfg
-rw-r--r--. 1 root root 4 3月 9 10:38 file111.txt
-rw-r--r--. 1 alice root 16 3月 7 00:03 file1.txt
-rw-r--r--. 1 root root 14 3月 7 00:02 file.txt
-rw-r--r--. 1 root root 1.7K 3月 3 21:50 initial-setup-ks.cfg
-rw-r--r--. 1 root root 31M 3月 9 11:24 test321.tar
[root@xnha tmp]# ls | grep test123
test123
[root@xnha tmp]# cd /root
[root@xnha ~]# gzip test321.tar #对打包之后的文件进行压缩
[root@xnha ~]# ll -h |grep test321
-rw-r--r--. 1 root root 31K 3月 9 11:24 test321.tar.gz

# 打包并压缩为 gzip 格式
[root@xnha tmp]# ls /root/桌面
test123
[root@xnha tmp]# ls /root
公共 图片 音乐 file111.txt initial-setup-ks.cfg
模板 文档 桌面 file1.txt test123
视频 下载 anaconda-ks.cfg file.txt test321.tar
[root@xnha tmp]# tar -zcf /root/桌面/test123.tar.gz test123 #-zcf创建新归档文件指定文件名并调用gzip
[root@xnha tmp]# ls /root/桌面
test123 test123.tar.gz
# 打包并压缩为 bzip2 格式
tar -cjvf data.tar.bz2 /var/log/ #-v显示操作详情
# 打包并压缩为 xz 格式(高压缩率)
tar -cJvf archive.tar.xz /opt/app/

image-20250803143917271

image-20250803145624521

2.解压操作
# 解压 .tar 文件
[root@xnha ~]# tar -xf test321.tar
[root@xnha ~]# ll -h |grep test321
-rw-r--r--. 1 root root 31M 3月 9 11:24 test321.tar
[root@xnha ~]# ls
公共 图片 音乐 file111.txt initial-setup-ks.cfg
模板 文档 桌面 file1.txt test123
视频 下载 anaconda-ks.cfg file.txt test321.tar
[root@xnha ~]# tar -xf test321.tar -C /root/桌面 #-C指定解压路径
[root@xnha ~]# ls /root/桌面
test123
# 解压 .tar.gz 到指定目录
[root@xnha tmp]# mkdir 39
[root@xnha tmp]# tar -zxf /root/桌面/test123.tar.gz -C /tmp/39/
[root@xnha tmp]# ls ./39 #/tmp/39 .当前工作路径/tmp
test123
# 解压 .tar.bz2 并显示进度
tar -xjvf data.tar.bz2

image-20250803152817414

image-20250803153156051

3.查看归档内容
# 列出 .tar 文件内容
[root@xnha ~]# gunzip test321.tar.gz
[root@xnha ~]# tar -tf test321.tar #只看查看不解压
test123/
test123/big1
test123/big2
# 列出 .tar.gz 中的文件
[root@xnha tmp]# tar -ztf /root/桌面/test123.tar.gz
test123/
test123/big1
test123/big2
# 搜索归档内文件
tar -tzvf data.tar.bz2 | grep "error.log"

image-20250803150152631

4.高级用法
# 排除特定文件/目录
tar -czvf site.tar.gz --exclude="*.tmp" --exclude="cache/" /var/www/ #排除所有以 .tmp 结尾的文件,排除 /var/www/ 下的 cache 目录及其内容
# 使用通配符打包多个文件
tar -czvf logs.tar.gz /var/log/*.log #指定打包 /var/log/ 目录下以 .log 结尾的文件
# 增量备份(仅打包新修改的文件)
tar -czvf incremental_backup.tar.gz --newer-mtime="2023-01-01" /data/ #仅包括修改时间晚于 2023-01-01的文件或目录(增量备份)