文件太大,导致无法通过email发送,占用大量磁盘空间。这个时候压缩技术就派上用场了。
压缩技术原理
计算机系统中都是使用bytes单位来计量的,计算机中最小的计量单位是bit,1byte=8bits。如果要记录一个数字1,对应的比特(二进制)是:
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
|---|---|---|---|---|---|---|---|
1会在最右边占据1个bit,而其他7个bit都是0。在这个例子中,那7个bit应该是“空的”才对。
压缩就是通过一些复杂的计算方式,将这些没有使用到的空间‘丢’出来,以让文件占用的空间变小。
另外一种压缩技术,是将重复的数据进行统计记录。 举例来说,如果你的数据为'111....'共有100个1时, 那么压缩技术会记录为'100个1'而不是真的有100个1的位存在。
简单的说,你可以将他想成,其实磁盘里面有相当多的'空间'存在,并不是完全填满的, 而'压缩'的技术就是将这些'空间'填满,以让整个档案占用的容量下降! 不过,这些'压缩过的文件'并无法直接被我们的操作系统所使用的,因此, 若要使用这些被压缩过的文件,则必须将他'还原'回来未压缩前的模样, 那就是所谓的'解压缩'。
压缩与解压缩的好处:
压缩过的文件容量变小了,占用更少的磁盘容量
网络数据的传输中,由于数量量的降低,可以让网络带宽传输更多的数据。目前很多网站也利用文件压缩技术来进行数据的传送。你在网站上面看到的数据在经过网络传输时,使用的是‘压缩过的资料’,等到这些压缩过的资料到达你的计算机时,再进行解压缩。
gzip(GNU Zip)是 Linux/Unix 中最常用的文件压缩命令,主要用于压缩单个文件,压缩后的文件扩展名通常为 .gz。
特点:
只能压缩文件,不能直接压缩目录
默认会删除原文件(保留压缩文件)
压缩率较高,速度较快
常与 tar 配合使用(.tar.gz 或 .tgz)
gzip [选项] 文件gzip -r 目录
常用选项
| 选项 | 长选项 | 说明 | 示例 |
|---|---|---|---|
| -1 | --fast | 最快压缩,压缩率最低 | gzip -1 file |
| -2~-8 | 不同压缩等级 | gzip -5 file | |
| -9 | --best | 最大压缩率,速度最慢 | gzip -9 file |
| -d | --decompress | 解压文件 | gzip -d file.gz |
| -k | --keep | 保留原文件 | gzip -k file |
| -c | --stdout | 输出到标准输出,不修改原文件 | gzip -c file > file.gz |
| -l | --list | 查看压缩文件信息 | gzip -l file.gz |
| -r | --recursive | 递归压缩目录中的文件 | gzip -r dir |
| -f | --force | 强制覆盖 | gzip -f file |
| -t | --test | 测试压缩文件是否损坏 | gzip -t file.gz |
| -v | --verbose | 显示详细信息 | gzip -v file |
| -n | --no-name | 不保存原文件名和时间 | gzip -n file |
| -N | --name | 保存原文件名和时间 | gzip -N file |
| 命令 | 功能 | 是否删除 .gz 文件 |
|---|---|---|
gzip | 压缩 | 默认删除原文件 |
gunzip | 解压 | 默认删除 .gz 文件 |
gzip -d | 解压 | 等同于 gunzip |
zcat | 查看压缩文件内容 | 不删除任何文件 |
| 级别 | 参数 | 压缩速度 | 压缩率 | CPU 消耗 | 适用场景 |
|---|---|---|---|---|---|
| 1 | -1 | 最快 | 最低 | 最低 | 实时日志、快速压缩 |
| 2~5 | -2~-5 | 较快 | 中等 | 较低 | 日常使用 |
| 6 | 默认 | 平衡 | 较高 | 中等 | 默认推荐 |
| 7~8 | -7、-8 | 较慢 | 较高 | 较高 | 备份、归档 |
| 9 | -9 | 最慢 | 最高 | 最高 | 长期存储、节省磁盘空间 |
[root@localhost data]# ll | grep messages-rw-------1 root root 2621696月 3009:37 messages-rw-------1 root root 2621696月 3009:37 messages.bak[root@localhost data]# gzip messages[root@localhost data]# ll | grep messages #压缩生成message.gz文件,源文件message被删除-rw-------1 root root 2621696月 3009:37 messages.bak-rw-------1 root root 379526月 3009:37 messages.gz[root@localhost data]# ll | grep messages-rw-------1 root root 2621696月 3009:43 messages-rw-------1 root root 2621696月 3009:37 messages.bak-rw-------1 root root 379526月 3009:43 messages.gz[root@localhost data]# rm -rf messages.gz[root@localhost data]# gzip -k messages #压缩生成message.gz文件,源文件message未被删除[root@localhost data]# ll | grep messages-rw-------1 root root 2621696月 3009:43 messages-rw-------1 root root 2621696月 3009:37 messages.bak-rw-------1 root root 379526月 3009:43 messages.gz[root@localhost data]#[root@localhost data]# gzip -d messages.gz #解压后,messages.gz文件被删除 gzip: messages already exists; do you wish to overwrite (y or n)? y[root@localhost data]# ll | grep messages-rw-------1 root root 2621696月 3009:43 messages-rw-------1 root root 2621696月 3009:37 messages.bak[root@localhost data]# gzip -k messages[root@localhost data]# ll | grep messages-rw-------1 root root 2621696月 3009:43 messages-rw-------1 root root 2621696月 3009:37 messages.bak-rw-------1 root root 379526月 3009:43 messages.gz[root@localhost data]# gzip -k -d messages.gz #加上-k选项,解压缩不会删除压缩文件gzip: messages already exists; do you wish to overwrite (y or n)? y[root@localhost data]# ll | grep messages-rw-------1 root root 2621696月 3009:43 messages-rw-------1 root root 2621696月 3009:37 messages.bak-rw-------1 root root 379526月 3009:43 messages.gz[root@localhost data]# gzip -t messages.gz #无返回,说明压缩文件正常[root@localhost data]# echo $?0[root@localhost data]# gzip -v messages #显示压缩过程 gzip: messages.gz already exists; do you wish to overwrite (y or n)? ymessages: 85.5% -- replaced with messages.gz[root@localhost data]# ls testdir/ -l总用量 780-rw-------1 root root 2621696月 3009:55 messages1-rw-------1 root root 2621696月 3009:55 messages2-rw-------1 root root 2621696月 3009:55 messages3[root@localhost data]# gzip -r -k testdir/[root@localhost data]# ls testdir/ -l总用量 900-rw-------1 root root 2621696月 3009:55 messages1-rw-------1 root root 379536月 3009:55 messages1.gz-rw-------1 root root 2621696月 3009:55 messages2-rw-------1 root root 379536月 3009:55 messages2.gz-rw-------1 root root 2621696月 3009:55 messages3-rw-------1 root root 379536月 3009:55 messages3.gz
[root@localhost data]# ls testdir/ -l总用量 780-rw-------1 root root 2621696月 3009:55 messages1-rw-------1 root root 2621696月 3009:55 messages2-rw-------1 root root 2621696月 3009:55 messages3[root@localhost data]# tar -cvzf testdir.tar.gz testdir #打包并压缩目录testdirtestdir/testdir/messages3testdir/messages2testdir/messages1[root@localhost data]# ls | grep testtestdirtestdir.tar.gz #生成的压缩文件[root@localhost data]# tar -xvf testdir.tar.gz #解包解压缩testdir/testdir/messages3testdir/messages2testdir/messages1[root@localhost data]# ls | grep testtestdirtestdir.tar.gz
| 参数 | 作用 |
|---|---|
| -c | 创建归档 |
| -x | 解包 |
| -z | 使用 gzip 压缩/解压 |
| -f | 指定文件名 |
| -v | 显示详细过程(可选) |
bzip2 是 Linux/Unix 中常用的文件压缩命令,采用 Burrows-Wheeler Transform (BWT) 压缩算法,相比 gzip压缩率更高,但压缩和解压速度更慢。压缩后的文件扩展名为 .bz2。
[root@localhost data]# tar cvjf testdir.tar.bz2 testdirtestdir/testdir/messages3testdir/messages2testdir/messages1[root@localhost data]# ls -l | grep testdirdrwxr-xr-x 2 root root 40966月 3010:05 testdir-rw-r--r--1 root root 454526月 3011:22 testdir.tar.bz2-rw-r--r--1 root root 1138916月 3011:21 testdir.tar.gz[root@localhost data]# du -sh * | grep testdir #通过对比,可以发现bz2的后的文件比gzip压缩的小很多784K testdir48K testdir.tar.bz2112K testdir.tar.gz
特点:
只能压缩单个文件,不能直接压缩目录(通常与 tar 配合使用)。
默认会删除原文件,只保留 .bz2 文件。
压缩率通常高于 gzip,但低于 xz。
常用于源码包、备份归档等场景。
bzip2 [选项] 文件...| 选项 | 长选项 | 说明 | 示例 |
|---|---|---|---|
| -1 | --fast | 最快压缩,压缩率最低 | bzip2 -1 file |
| -2~-8 | 不同压缩等级 | bzip2 -5 file | |
| -9 | --best | 最高压缩率(默认) | bzip2 -9 file |
| -d | --decompress | 解压文件 | bzip2 -d file.bz2 |
| -k | --keep | 保留原文件 | bzip2 -k file |
| -c | --stdout | 输出到标准输出 | bzip2 -c file > file.bz2 |
| -f | --force | 强制覆盖 | bzip2 -f file |
| -t | --test | 测试压缩文件完整性 | bzip2 -t file.bz2 |
| -v | --verbose | 显示详细信息 | bzip2 -v file |
| -q | --quiet | 安静模式 | bzip2 -q file |
| 命令 | 功能 |
|---|---|
bzip2 | 压缩文件 |
bunzip2 | 解压文件 |
bzcat | 查看压缩文件内容 |
bzip2recover | 尝试恢复损坏的 .bz2 文件 |
[root@localhost data]# ls | grep messagesmessages1messages2[root@localhost data]# bzip2 messages1 #默认压缩后,源文件被删除 [root@localhost data]# bzip2 -k messages2 #-k选项,压缩不删除源文件[root@localhost data]# ls | grep messagesmessages1.bz2messages2messages2.bz2[root@localhost data]# cp messages2 messages3[root@localhost data]# bzip2 -k -v messages3 #-v选项,显示详细过程 messages3: 7.600:1, 1.053 bits/byte, 86.84% saved, 262169in, 34497 out.[root@localhost data]# ls | grep messagesmessages1.bz2messages2messages2.bz2messages3messages3.bz2[root@localhost data]# bzip2 -t messages1.bz2 #测试压缩包是否被损坏[root@localhost data]# bzip2 -d messages1.bz2 #解压默认也删除压缩包文件[root@localhost data]# ls | grep messagesmessages1messages2messages2.bz2messages3messages3.bz2[root@localhost data]# rm -rf messages2[root@localhost data]# bunzip2 -k messages2.bz2 #解压时,加-k选项,保留压缩包[root@localhost data]# ls | grep messagesmessages1messages2messages2.bz2messages3messages3.bz2
由于 bzip2 不能直接压缩目录,因此通常配合 tar。
打包并压缩目录
tar -cjf backup.tar.bz2 backup/参数说明:
| 参数 | 说明 |
|---|---|
| -c | 创建归档 |
| -j | 使用 bzip2 压缩 |
| -f | 指定文件名 |
解压
tar -xjf backup.tar.bz2xz 是 Linux/Unix 中一种高压缩率文件压缩工具,采用 LZMA2(Lempel-Ziv-Markov chain Algorithm 2) 算法,在三者(gzip、bzip2、xz)中通常具有最高的压缩率,但压缩和解压速度相对较慢,CPU 和内存消耗也更高。
特点:
只能压缩单个文件(压缩目录需配合 tar)
默认删除原文件,仅保留 .xz 文件
压缩率最高,尤其适合大文件和长期归档
Linux 内核源码、许多发行版软件包都广泛使用 .tar.xz
xz [选项] 文件...| 选项 | 长选项 | 说明 | 示例 |
|---|---|---|---|
| -0 ~ -9 | 指定压缩级别(默认 -6) | xz -9 file | |
| -e | --extreme | 极限压缩模式(耗时更长) | xz -9e file |
| -d | --decompress | 解压 | xz -d file.xz |
| -k | --keep | 保留原文件 | xz -k file |
| -c | --stdout | 输出到标准输出 | xz -c file > file.xz |
| -f | --force | 强制覆盖 | xz -f file |
| -t | --test | 测试压缩包完整性 | xz -t file.xz |
| -l | --list | 查看压缩包信息 | xz -l file.xz |
| -v | --verbose | 显示详细信息 | xz -v file |
| -T0 | --threads=0 | 使用全部 CPU 核心,默认是单线程 | xz -T0 file |
| -T2 | --threads=2 | 使用 2 个线程 | xz -T2 file |
[root@localhost testdir]# lsmessages1 messages2 messages3[root@localhost testdir]# gzip -k messages1[root@localhost testdir]# bzip2 -k messages2[root@localhost testdir]# xz -k messages3[root@localhost testdir]# du -sh * #三种压缩对比,xz的最小,其次是bz2260K messages140K messages1.gz260K messages236K messages2.bz2260K messages332K messages3.xz[root@localhost testdir]# rm -rf messages3.xz[root@localhost testdir]# xz -k -9 messages3[root@localhost testdir]# du -sh *260K messages140K messages1.gz260K messages236K messages2.bz2260K messages332K messages3.xz[root@localhost testdir]# rm -rf messages3.xz[root@localhost testdir]# xz -k -T2 messages3 #指定2线程[root@localhost testdir]# xz -l messages3.xz 流 块 压缩大小 解压大小 比例 校验 文件名 1 1 30.0 KiB 256.0 KiB 0.117 CRC64 messages3.xz------------------------------------------------------------------------------------------------------| 字段 | 含义 || ------------ | ----- || Compressed | 压缩后大小 || Uncompressed | 原始大小 || Ratio | 压缩比 || Check | 校验算法 || Filename | 文件名 |-------------------------------------------------------------------------------------------------------[root@localhost testdir]# xz -v -k messages3messages3 (1/1) 100 % 30.0 KiB / 256.0 KiB = 0.117[root@localhost testdir]# rm -rf messages3[root@localhost testdir]# ll总用量 628-rw------- 1 root root 262169 6月 30 09:55 messages1-rw------- 1 root root 37953 6月 30 09:55 messages1.gz-rw------- 1 root root 262169 6月 30 09:55 messages2-rw------- 1 root root 34497 6月 30 09:55 messages2.bz2-rw------- 1 root root 30704 6月 30 09:55 messages3.xz[root@localhost testdir]# xz -d -k messages3.xz[root@localhost testdir]# ll总用量 888-rw------- 1 root root 262169 6月 30 09:55 messages1-rw------- 1 root root 37953 6月 30 09:55 messages1.gz-rw------- 1 root root 262169 6月 30 09:55 messages2-rw------- 1 root root 34497 6月 30 09:55 messages2.bz2-rw------- 1 root root 262169 6月 30 09:55 messages3-rw------- 1 root root 30704 6月 30 09:55 messages3.xz
打包并压缩目录
tar -cJf backup.tar.xz backup/参数说明:
| 参数 | 说明 |
|---|---|
| -c | 创建归档 |
| -J | 使用 xz 压缩 |
| -f | 指定文件名 |
解压
tar -xJf backup.tar.xz查看归档内容
tar -tJf backup.tar.xz[root@localhost data]# tar -cvJf testdir.tar.xz testdir/testdir/testdir/messages3testdir/messages3.xztestdir/messages2testdir/messages1testdir/messages2.bz2testdir/messages1.gz[root@localhost data]# ll | grep testdirdrwxr-xr-x 2 root root 4096 7月 1 09:49 testdir-rw-r--r-- 1 root root 134172 7月 1 09:54 testdir.tar.xz[root@localhost data]# tar -tJf testdir.tar.xz #查看压缩包中有什么文件testdir/testdir/messages3testdir/messages3.xztestdir/messages2testdir/messages1testdir/messages2.bz2testdir/messages1.gz[root@localhost data]# rm -rf testdir[root@localhost data]# tar -xvJf testdir.tar.xztestdir/testdir/messages3testdir/messages3.xztestdir/messages2testdir/messages1testdir/messages2.bz2testdir/messages1.gz[root@localhost data]# ll | grep testdirdrwxr-xr-x 2 root root 4096 7月 1 09:49 testdir-rw-r--r-- 1 root root 134172 7月 1 09:54 testdir.tar.xz
| 特性 | gzip | bzip2 | xz |
|---|---|---|---|
| 扩展名 | .gz | .bz2 | .xz |
| 算法 | DEFLATE | BWT + Huffman | LZMA2 |
| 压缩速度 | 快 | 较慢 | 最慢 |
| 解压速度 | 很快 | 较慢 | 较慢(通常比压缩快) |
| 压缩率 | 较高 | 高 | 最高 |
| CPU 占用 | 低 | 中 | 高 |
| 内存占用 | 低 | 中 | 高 |
| 默认压缩级别 | 6 | 9 | 6 |
| 是否支持多线程 | 否(传统 gzip) | 否 | 支持(-T) |
| 是否支持目录 | 否 | 否 | 否 |
tar(Tape Archive)是 Linux 中最常用的归档(打包)工具。它的主要作用是将多个文件或目录打包成一个归档文件,本身并不负责压缩;通常会与 gzip、bzip2、xz 等压缩工具配合使用,生成 .tar.gz、.tar.bz2、.tar.xz 等文件。
特点:
可以打包单个文件、多个文件或整个目录。
保留文件权限、所有者、时间戳、软链接等元数据。
可与多种压缩算法结合。
是 Linux 备份、软件发布和文件传输中最常用的工具之一。
多个文件 ├── file1 ├── file2 └── file3 │ ▼ tar 打包 │ ▼ archive.tar (仅归档,不压缩) │ ▼gzip / bzip2 / xz │ ▼archive.tar.gzarchive.tar.bz2archive.tar.xz
tar [选项] 归档文件 [文件或目录]| 选项 | 长选项 | 作用 |
|---|---|---|
-c | --create | 创建归档,打包文件(目录) |
-x | --extract | 解压归档 |
-t | --list | 查看归档内容 |
-f | --file | 指定归档文件 |
-v | --verbose | 显示详细过程 |
-C | --directory | 指定解压或打包目录 |
-z | 使用 gzip | |
-j | 使用 bzip2 | |
-J | 使用 xz | |
-a | --auto-compress | 自动识别压缩格式(GNU tar) |
-p | --preserve-permissions | 保留权限 |
-P | --absolute-names | 保留绝对路径 |
--exclude | 排除文件或目录 | |
--strip-components=N | 解压时去掉前 N 层目录 | |
-k | --keep-old-files | 不覆盖已有文件 |
--overwrite | 强制覆盖已有文件 |
[root@localhost testdir]# ll总用量 784-rw------- 1 root root 262169 6月 30 09:55 messages1-rw------- 1 root root 262169 6月 30 09:55 messages2-rw------- 1 root root 262169 6月 30 09:55 messages3drwxr-xr-x 2 root root 4096 7月 1 14:39 test[root@localhost testdir]# ls testmessages1 messages2 messages3[root@localhost testdir]# tar -cvf message.tar messages1 messages2 messages3 #将三个文件打包到message.tar文件中messages1messages2messages3[root@localhost testdir]# ll总用量 1564-rw------- 1 root root 262169 6月 30 09:55 messages1-rw------- 1 root root 262169 6月 30 09:55 messages2-rw------- 1 root root 262169 6月 30 09:55 messages3-rw-r--r-- 1 root root 798720 7月 1 14:40 message.tardrwxr-xr-x 2 root root 4096 7月 1 14:39 test[root@localhost testdir]# tar -cvf test.tar test/ #打包目录,生成test.tar文件test/test/messages3test/messages2test/messages1[root@localhost testdir]# ll总用量 2344-rw------- 1 root root 262169 6月 30 09:55 messages1-rw------- 1 root root 262169 6月 30 09:55 messages2-rw------- 1 root root 262169 6月 30 09:55 messages3-rw-r--r-- 1 root root 798720 7月 1 14:40 message.tardrwxr-xr-x 2 root root 4096 7月 1 14:39 test-rw-r--r-- 1 root root 798720 7月 1 14:41 test.tar[root@localhost testdir]# tar -cvzf test.tar.gz test #打包并使用 gzip 压缩test/test/messages3test/messages2test/messages1[root@localhost testdir]# tar -cvjf test.tar.bz2 test #打包并使用 bzip2test/test/messages3test/messages2test/messages1[root@localhost testdir]# tar -cvJf test.tar.xz test #打包并使用 xztest/test/messages3test/messages2test/messages1[root@localhost testdir]# ll总用量 2536-rw------- 1 root root 262169 6月 30 09:55 messages1-rw------- 1 root root 262169 6月 30 09:55 messages2-rw------- 1 root root 262169 6月 30 09:55 messages3-rw-r--r-- 1 root root 798720 7月 1 14:40 message.tardrwxr-xr-x 2 root root 4096 7月 1 14:39 test-rw-r--r-- 1 root root 798720 7月 1 14:41 test.tar-rw-r--r-- 1 root root 45245 7月 1 14:48 test.tar.bz2-rw-r--r-- 1 root root 113875 7月 1 14:48 test.tar.gz-rw-r--r-- 1 root root 30932 7月 1 14:48 test.tar.xz[root@localhost testdir]# mkdir test{1..4} #创建4个目录[root@localhost testdir]# ll总用量 2552-rw------- 1 root root 262169 6月 30 09:55 messages1-rw------- 1 root root 262169 6月 30 09:55 messages2-rw------- 1 root root 262169 6月 30 09:55 messages3-rw-r--r-- 1 root root 798720 7月 1 14:40 message.tardrwxr-xr-x 2 root root 4096 7月 1 14:39 testdrwxr-xr-x 2 root root 4096 7月 1 14:51 test1drwxr-xr-x 2 root root 4096 7月 1 14:51 test2drwxr-xr-x 2 root root 4096 7月 1 14:51 test3drwxr-xr-x 2 root root 4096 7月 1 14:51 test4-rw-r--r-- 1 root root 798720 7月 1 14:41 test.tar-rw-r--r-- 1 root root 45245 7月 1 14:48 test.tar.bz2-rw-r--r-- 1 root root 113875 7月 1 14:48 test.tar.gz-rw-r--r-- 1 root root 30932 7月 1 14:48 test.tar.xz[root@localhost testdir]# tar -xvf test.tar -C test1 #解包test.tar文件到test1目录test/test/messages3test/messages2test/messages1[root@localhost testdir]# ls test1test[root@localhost testdir]# tar -xvzf test.tar.gz -C test2 #解压 .tar.gz到test2目录test/test/messages3test/messages2test/messages1[root@localhost testdir]# tar -xvjf test.tar.bz2 -C test3 #解压 .tar.bz2到test3目录test/test/messages3test/messages2test/messages1[root@localhost testdir]# tar -xvJf test.tar.xz -C test4 #解压 .tar.xz到test4目录test/test/messages3test/messages2test/messages1[root@localhost testdir]# ll总用量 1580-rw------- 1 root root 262169 6月 30 09:55 messages1-rw------- 1 root root 262169 6月 30 09:55 messages2-rw------- 1 root root 262169 6月 30 09:55 messages3-rw-r--r-- 1 root root 798720 7月 1 14:40 message.tardrwxr-xr-x 2 root root 4096 7月 1 14:39 testdrwxr-xr-x 3 root root 4096 7月 1 14:52 test1drwxr-xr-x 3 root root 4096 7月 1 14:55 test2drwxr-xr-x 3 root root 4096 7月 1 14:55 test3drwxr-xr-x 3 root root 4096 7月 1 14:56 test4[root@localhost testdir]# tar -cvzf /data/testdir/test1/test.tar.gz test #指定压缩包文件的目录为指定目录test/test/messages3test/messages2test/messages1[root@localhost testdir]# ls test1/test test.tar.gz[root@localhost testdir]# tar -tvzf test1/test.tar.gz #查看压缩包内的文件drwxr-xr-x root/root 0 2026-07-01 14:39 test/-rw------- root/root 262169 2026-07-01 14:39 test/messages3-rw------- root/root 262169 2026-07-01 14:39 test/messages2-rw------- root/root 262169 2026-07-01 14:39 test/messages1[root@localhost testdir]#-----------------------------------------------------------------------------------------------[root@localhost testdir]# lstest1 test2 test3 test4[root@localhost testdir]# ls test1test test.tar.gz[root@localhost testdir]# tar -tvzf test1/test.tar.gz #查看压缩包内的文件drwxr-xr-x root/root 0 2026-07-01 14:39 test/-rw------- root/root 262169 2026-07-01 14:39 test/messages3-rw------- root/root 262169 2026-07-01 14:39 test/messages2-rw------- root/root 262169 2026-07-01 14:39 test/messages1[root@localhost testdir]# tar -xzvf test1/test.tar.gz test/messages1 #只解压message1文件出来test/messages1[root@localhost testdir]# lstest test1 test2 test3 test4[root@localhost testdir]# ls testmessages1[root@localhost testdir]#----------------------------------------------------------------------------------------------------tar结合xz使用时,如何使用多线程,通过tar --use-compress-program,把 xz 的所有参数传给 tar命令----------------------------------------------------------------------------------------------------[root@localhost testdir]# tar -cvf test.tar.xz --use-compress-program="xz -T4" test1/testtest1/test/test1/test/messages3test1/test/messages2test1/test/messages1[root@localhost testdir]# lstest1 test2 test3 test4 test.tar.xz
GNU tar 是 tar 命令的一个具体、也是最主流的实现版本。它由自由软件基金会(Free Software Foundation)开发和维护,相比最初的 tar,增加了许多强大的功能和扩展。
怎么确认自己使用的tar是不是GUN tar,通过tar --version命令查看版本,如果输出信息中明确包含 "GNU tar" 字样,那就可以确认是 GNU 版本。
[root@localhost testdir]# tar --versiontar (GNU tar) 1.34Copyright (C) 2021 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.This is free software: you are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law.由 John Gilmore 和 Jay Fenlason 所写。[root@localhost testdir]#[root@localhost testdir]# lstest[root@localhost testdir]# ls testmessages1 messages2 messages3[root@localhost testdir]# tar -cvaf test.tar.gz test #根据压缩文件的后缀,自动选择对应的压缩技术test/test/messages3test/messages2test/messages1[root@localhost testdir]# file test.tar.gztest.tar.gz: gzip compressed data, from Unix, original size modulo 2^32 798720[root@localhost testdir]# tar -cvaf test.tar.bz2 testtest/test/messages3test/messages2test/messages1[root@localhost testdir]# file test.tar.bz2test.tar.bz2: bzip2 compressed data, block size = 900k[root@localhost testdir]# tar -cvaf test.tar.xz testtest/test/messages3test/messages2test/messages1[root@localhost testdir]# lstest test.tar.bz2 test.tar.gz test.tar.xz[root@localhost testdir]# file test.tar.xztest.tar.xz: XZ compressed data
GNU tar 支持根据文件后缀名自动选择合适的压缩技术进行解压缩或者压缩:
压缩:
tar -caf backup.tar.gz backup/tar -caf backup.tar.bz2 backup/tar -caf backup.tar.xz backup/
解压:
tar -xaf backup.tar.gztar -xaf backup.tar.bz2tar -xaf backup.tar.xz
| 压缩格式 | 创建 | 解压 | 查看 |
|---|---|---|---|
.tar | tar -cvf | tar -xvf | tar -tvf |
.tar.gz / .tgz | tar -czvf | tar -xzvf | tar -tzvf |
.tar.bz2 | tar -cjvf | tar -xjvf | tar -tjvf |
.tar.xz | tar -cJvf | tar -xJvf | tar -tJvf |
.tar.gz / .tgz或者.tar.bz2或者.tar.xz【GUN tar支持】 | tar -cvaf | tar -xvaf | tar -tvaf |
dd 是 Linux 中一个非常重要的底层数据复制工具,能够以块(Block)*为单位进行数据读写,可以在*文件、磁盘、分区、设备之间进行复制,因此常被称为 Linux 的底层拷贝命令。
特点:
可以复制文件、磁盘、分区、ISO 镜像等。
可以进行磁盘备份、恢复、制作启动盘。
可以转换数据格式(ASCII/EBCDIC、大小写转换等)。
不依赖文件系统,可直接操作块设备。
功能非常强大,但也非常危险,写错目标设备可能导致整个磁盘数据被覆盖。
输入(if) │ ▼ 按块(Block)读取 │ 可进行格式转换(conv) │ ▼ 按块(Block)写入 │ ▼ 输出(of)
dd if=<输入> of=<输出> [选项]| 参数 | 含义 | 示例 |
|---|---|---|
if= | 输入文件(Input File) | if=/dev/sda |
of= | 输出文件(Output File) | of=backup.img |
bs= | 块大小(Block Size) | bs=1M |
ibs= | 输入块大小 | ibs=4K |
obs= | 输出块大小 | obs=4K |
count= | 复制块数 | count=100 |
skip= | 跳过输入前 N 个块 | skip=1 |
seek= | 跳过输出前 N 个块 | seek=1 |
status=progress | 显示实时进度 | status=progress |
conv= | 数据转换 | conv=sync,noerror |
dd if=file1 of=file2 bs=1M等价于cp file1 file2
命令演示:
[root@localhost testdir]# lsmessages1[root@localhost testdir]# dd if=messages1 of=messages1.bak bs=4k记录了64+1 的读入记录了64+1 的写出262169字节(262 kB,256 KiB)已复制,0.000703059 s,373 MB/s[root@localhost testdir]# ll总用量 520-rw------- 1 root root 262169 7月 1 15:37 messages1-rw-r--r-- 1 root root 262169 7月 1 16:51 messages1.bak[root@localhost testdir]# file messages1 messages1.bakmessages1: UTF-8 Unicode text, with very long linesmessages1.bak: UTF-8 Unicode text, with very long lines
dd if=/dev/sda of=/backup/sda.img bs=64M status=progressdd if=/dev/sda1 of=/backup/sda1.img bs=64M status=progress
说明:
输入:整个 /dev/sda
输出:镜像文件 sda.img
如果sda是一块100G的磁盘,但是实际数据只有2G,那么生成的sda.img也是100G。因为 dd 是一个块级别的工具,它不关心磁盘上有没有数据,也不理解“文件”或“空余空间”的概念。
恢复:
dd if=/backup/sda.img of=/dev/sda bs=64M status=progress警告: 恢复会覆盖目标磁盘上的所有数据。
命令演示:
/dev/sda1 分区挂载至/boot目录,备份sda1磁盘分区
[root@localhost ~]# df -h文件系统 容量 已用 可用 已用% 挂载点devtmpfs 4.0M 0 4.0M 0% /devtmpfs 1.4G 0 1.4G 0% /dev/shmtmpfs 550M 8.5M 542M 2% /run/dev/mapper/cs-root 17G 13G 4.2G 76% //dev/sda1 960M 306M 655M 32% /boot/dev/mapper/vg--data-lv--data 482G 2.2G 455G 1% /datatmpfs 275M 52K 275M 1% /run/user/42tmpfs 275M 36K 275M 1% /run/user/0overlay 17G 13G 4.2G 76% /var/lib/docker/overlay2/78d7b0b6180d813f3e5bf994ee34f909f954e622c1c711b02b86ed8135c0b2b6/mergedoverlay 17G 13G 4.2G 76% /var/lib/docker/overlay2/71d01ad2b1415bebd28381f202e6cea925e5da109ea79f07a16188b6a5913404/mergedoverlay 17G 13G 4.2G 76% /var/lib/docker/overlay2/0ed0a69af127b12c1992216ada97ac0fe073fa194ee55c8d441974ef7f7b2275/merged[root@localhost ~]# dd if=/dev/sda1 of=/data/sda1.img bs=64M status=progress402653184字节(403 MB,384 MiB)已复制,56 s,7.2 MB/s469762048字节(470 MB,448 MiB)已复制,159 s,3.0 MB/s1073741824字节(1.1 GB,1.0 GiB)已复制,424 s,2.5 MB/s记录了16+0 的读入记录了16+0 的写出1073741824字节(1.1 GB,1.0 GiB)已复制,424.494 s,2.5 MB/s[root@localhost ~]# cd /data/[root@localhost data]# du -sh sda1.img1.1G sda1.img
例如将 ISO 写入 /dev/sdb:
dd if=ubuntu.iso of=/dev/sdb bs=4M status=progress oflag=sync完成后执行:
sync说明:
目标应为整个磁盘设备(如 /dev/sdb),不要写成 /dev/sdb1。
oflag=sync 和 sync 可确保数据完全写入。
生成 1GB 文件:
dd if=/dev/zero of=test.img bs=1M count=1024 status=progress参数解释:
| 参数 | 值 |
|---|---|
bs=1M | 每块 1MB |
count=1024 | 1024 块 |
总大小:
1MB × 1024 = 1GB命令演示:
[root@localhost data]# dd if=/dev/zero of=test.img bs=1M count=1024 status=progress993001472字节(993 MB,947 MiB)已复制,16 s,61.7 MB/s记录了1024+0 的读入记录了1024+0 的写出1073741824字节(1.1 GB,1.0 GiB)已复制,16.298 s,65.9 MB/s
dd if=test.img of=/dev/null bs=1G iflag=direct status=progress[root@localhost data]# dd if=test.img of=/dev/null bs=1G iflag=direct status=progress1073741824字节(1.1 GB,1.0 GiB)已复制,2 s,557 MB/s记录了1+0 的读入记录了1+0 的写出1073741824字节(1.1 GB,1.0 GiB)已复制,1.92899 s,557 MB/s
dd if=/dev/zero of=/dev/sdb bs=64M status=progress将整个磁盘写为 0。
快速清除前 100MB:
dd if=/dev/zero of=/dev/sdb bs=1M count=100conv 常见选项| 选项 | 作用 |
|---|---|
sync | 输入块不足时填充 0 |
noerror | 遇到读错误继续执行 |
notrunc | 不截断输出文件 |
fsync | 完成后刷新到磁盘 |
例如:
dd if=/dev/sda of=backup.img bs=64K conv=noerror,sync适用于坏盘恢复,尽可能继续读取。
bs)选择bs | 场景 |
|---|---|
| 512B | 扇区级操作 |
| 4K | 文件系统块 |
| 1M | 普通文件复制 |
| 4M | 写入 U 盘 |
| 64M | 大容量磁盘备份 |
| 1G | 性能测试 |
一般来说,适当增大 bs 可以减少系统调用次数,提高复制效率,但也会增加内存占用。
| 输入源 | 说明 |
|---|---|
/dev/zero | 连续的 0 |
/dev/null | 空设备 |
/dev/random | 真随机数(速度较慢) |
/dev/urandom | 伪随机数(速度较快) |
/dev/sda | 整块磁盘 |
/dev/sda1 | 分区 |