压缩和解压缩
发表于:2025-08-04 | 分类: Linux

一、生成测试文件

1、生成测试文件命令dd
基本用法
1
dd if=<输入源> of=<输出目标> bs=<块大小> count=<块数量> [status=progress] #[status=progress]可以查看进度显示

示例:

1
2
3
4
5
6
7
dd if=/dev/zero of=/tmp/testfile bs=1M count=100

# 参数说明
if=/dev/zero # 输入源(Linux零设备) if = input file
• of=/tmp/testfile # 输出文件路径 of = output file
• bs=1M # 块大小(1MB)
• count=100 # 块数量
设备文件 输出内容 典型用途
/dev/zero 无限连续的0x00字节 创建空白文件,初始化存储空间
/dev/null 黑洞设备(丢弃所有写入) 屏蔽程序输出
2、查看文件
1
2
3
4
5
6
7
8
9
10
file 文件名 #可以查看文件类型 会告诉你是什么文件 还是目录
[root@farrah 桌面]# file test
test: ASCII text

[root@xnha tmp]# hexdump testfile # 使用十六进制的方法查看文件
0000000 0000 0000 0000 0000 0000 0000 0000 0000
*
6400000

#出现类型为data时表示file文件命令无法识别该文件的具体格式。

二、压缩和解压缩

1、压缩命令gzip

gzip是Linux中用于文件压缩的常用工具,压缩以后会生成.gz后缀文件,原文件默认删除。

[!NOTE]

注意:gzip仅仅用来压缩文件 要想压缩目录需要使用tar命令打包成一个文件后再对文件进行压缩

命令格式
1
gzip [选项] 文件名
选项说明
参数 说明
-k 保留原文件
-d 解压文件
-1 ~ -9 压缩级别:1最快压缩率最低,-9最慢压缩率最高(默认-6)
-v 显示压缩率等详细信息
-c 输出到标准输出(不修改文件)
-t 测试压缩文件的完整性
-r 递归处理目录
-l 查看压缩文件信息(压缩前后大小,压缩率)
使用方法
  • 基础压缩

删除原文件的压缩

1
2
3
4
5
[root@farrah 桌面]# cd /tmp 
[root@farrah tmp]# touch testfile #先新建一个testfile文件测试
[root@farrah tmp]# gzip testfile #压缩testfile文件并删除原文件
[root@farrah tmp]# ll #找到该文件,可看见如下信息
-rw-r--r--. 1 root root 29 8月 4 10:14 testfile.gz

不删除原文件的压缩

1
2
3
4
[root@farrah tmp]# gzip -k testfile #-k的作用为不删除原文件进行压缩
[root@farrah tmp]# ll /tmp |grep testfile 筛选出testfile查看文件
-rw-r--r--. 1 root root 0 8月 4 10:17 testfile
-rw-r--r--. 1 root root 29 8月 4 10:17 testfile.gz

2.解压命令gunzip

命令格式
1
gunzip [选项] 文件名.gz #与gzip -d 可同等替换
#选项说明与压缩命令gzip基本一致
使用方法
  • 解压操作

不保留原压缩文件的解压

1
2
3
[root@farrah tmp]# gunzip testfile.gz #解压不保留原解压文件
[root@farrah tmp]# ll /tmp | grep testfile
-rw-r--r--. 1 root root 0 8月 4 10:17 testfile

保留原压缩文件的解压

1
2
3
4
5
6
7
8
9
10
[root@farrah tmp]# gunzip -k testfile.gz  #gunzip同样使用-k即可保留原文件
[root@farrah tmp]# ll /tmp | grep testfile
-rw-r--r--. 1 root root 0 8月 4 10:17 testfile
-rw-r--r--. 1 root root 29 8月 4 10:17 testfile.gz

#使用gzip也同样可以实现解压操作 选项为:-d
[root@farrah tmp]# gzip -d -k testfile.gz
[root@farrah tmp]# ll /tmp | grep testfile
-rw-r--r--. 1 root root 0 8月 4 10:17 testfile
-rw-r--r--. 1 root root 29 8月 4 10:17 testfile.gz

3.gzip查看压缩内容

1
2
zcat file.gz #用来查看压缩的文件内容
gzip -l backup.gz #显示压缩率

4.压缩命令bzip2

bzip2 是 Linux 中基于 Burrows-Wheeler 变换 的高效压缩工具,相比 gzip 压缩率更高,但速度较

慢。压缩后生成 .bz2 后缀文件,默认删除原始文件

命令格式
1
bzip2 [选项] 文件名 # 压缩文件
选项说明
参数 说明
-k 保留原文件
-d 解压文件
-1 ~ -9 压缩级别:1最快压缩率最低,-9最慢压缩率最高(默认-6)
-v 显示压缩率等详细信息
-c 输出到标准输出(不修改文件)
-t 测试压缩文件的完整性
-f 强制覆盖已存在的输出文件
-s 降低内存占用(牺牲速度)
-z 显式指定压缩模式(默认行为)
使用方法
  • 压缩文件
1
2
3
4
#不保留原文件进行压缩
[root@farrah tmp]# bzip2 testfile
[root@farrah tmp]# ll /tmp |grep testfile
-rw-r--r--. 1 root root 14 8月 4 10:17 testfile.bz2
1
2
3
4
5
#保留原文件进行压缩
[root@farrah tmp]# bzip2 -k testfile
[root@farrah tmp]# ll /tmp |grep testfile
-rw-r--r--. 1 root root 0 8月 4 10:17 testfile
-rw-r--r--. 1 root root 14 8月 4 10:17 testfile.bz2

5.解压命令bunzip2

命令格式
1
bunzip2 [选项] 文件名.bz2 # 解压(等价于 bzip2 -d)
#选项说明与压缩命令gzip基本一致
使用方法

不保留原文件进行解压

1
2
3
[root@farrah tmp]# bunzip2 testfile.bz2 
[root@farrah tmp]# ll /tmp |grep testfile
-rw-r--r--. 1 root root 0 8月 4 10:17 testfile

保留原文件进行解压

1
2
3
4
5
6
7
8
9
10
11
#使用bunzip2命令解压
[root@farrah tmp]# bunzip2 -k testfile.bz2
[root@farrah tmp]# ll /tmp | grep testfile
-rw-r--r--. 1 root root 0 8月 4 10:17 testfile
-rw-r--r--. 1 root root 14 8月 4 10:17 testfile.bz2

#使用bzip2命令解压
[root@farrah tmp]# bzip2 -kd testfile.bz2
[root@farrah tmp]# ll /tmp | grep testfile
-rw-r--r--. 1 root root 0 8月 4 10:17 testfile
-rw-r--r--. 1 root root 14 8月 4 10:17 testfile.bz2

6.bzip2查看压缩内容

1
2
3
4
5
bzcat file.bz2 # 不解压查看内容
bzip2 -l archive.bz2 # 显示压缩信息:
# Compression ratio: 4.20:1
# compressed size: 102400 bytes
# uncompressed size: 430080 bytes

三、打包文件

1、打包命令tar

tar (Tape Archive)是 Linux 系统中用于 打包文件目录 的归档工具,支持保留文件权限、时间戳和

目录结构(不对文件/目录本身造成任何影响,产生新文件)。常与压缩工具( gzip 、 bzip2 、 xz )

结合使用,形成 .tar.gz 、 .tar.bz2 、 .tar.xz 等压缩包

命令格式
1
tar [主选项] [辅助选项]  文件名或目录 #主选项必写,辅助选项可选
选项说明
  • 必选参数
参数 说明
-c(create) 创建新归档文件
-x (extracct) 解压归档文件
-t (list 查看归档内容列表
-r 追加文件到已有归档(要先解压在重新打包)
  • 辅助选项参数
参数 说明
-f 指定归档名(必须放在最后)
-v 显示操作详情
-z 使用gzip压缩(生成.tar.gz)
-j 使用bzip2压缩(生成tar.bz2)
-J 使用xz压缩(生成.tar.xz)
-C 解压到指定目录
–exclude 排除指定文件/目录
–wildcards 使用通配符文件
使用方法

打包压缩

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#打包并压缩test文件 -cvf 打包归档、 gzip压缩 分步操作
[root@farrah tmp]# tar -cvf test1.tar test
test/
test/dir1/
test/dir1/file1.txt
test/dir1/file2.txt
test/dir1/newfile.txt
test/dir2/
test/dir2/subdir/
test/dir2/subdir/dir1/
test/dir2/subdir/dir1/file1.txt
test/dir2/subdir/dir1/file2.txt
[root@farrah tmp]# gzip test1.tar

#一步操作 -czvf -z:可以直接压缩打包后的文件
[root@farrah tmp]# tar -czvf test1.tar.gz test
test/
test/dir1/
test/dir1/file1.txt
test/dir1/file2.txt
test/dir1/newfile.txt
test/dir2/
test/dir2/subdir/
test/dir2/subdir/dir1/
test/dir2/subdir/dir1/file1.txt
test/dir2/subdir/dir1/file2.txt

解压

1.解压.tar文件

1
2
3
4
5
[root@farrah tmp]# tar -xf testfile.tar
[root@farrah tmp]# ll /tmp | grep testfile
-rw-r--r--. 1 root root 0 8月 4 10:17 testfile
-rw-r--r--. 1 root root 14 8月 4 10:17 testfile.bz2
-rw-r--r--. 1 root root 10240 8月 4 12:26 testfile.tar

2.解压.tar.gz到指定目录

1
2
3
4
5
6
7
8
#-zxf -x:解压文件 -z:使用gzip解压 
[root@farrah tmp]# tar -cvzf testfile.tar.gz testfile
testfile/
testfile/dir1/
testfile/file
[root@farrah tmp]# tar -zxf testfile.tar.gz -C /tmp/test/
[root@farrah tmp]# ls /tmp/test
testfile

3.查看归档内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#-t 查看归档内容

#列出 .tar 文件内容
[root@xnha ~]# gunzip test321.tar.gz
[root@xnha ~]# tar -tf test321.tar

#列出.tar.gz中的文件
[root@farrah tmp]# tar -tf testfile.tar.gz
testfile/
testfile/dir1/
testfile/file[root@farrah tmp]# tar -tf testfile.tar.gz
testfile/
testfile/dir1/
testfile/file

#搜索归档类文件
[root@farrah tmp]# tar -ztf testfile.tar.gz | grep test
testfile/
testfile/dir1/
testfile/file

上一篇:
apache的搭建
下一篇:
OSPF实验基本配置步骤