栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Linux 解压缩

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux 解压缩

1. tar

tar 命令是Linux 下使用较多的归档命令。tar 命令归档(打包)的文件以 .tar 结尾。

常用的组合命令:

  • 打包:tar -cvf 包名 文件1 文件2
  • 解打包:tar -xvf 包名
  • 查看包:tar -tvf 包名
  • 解压 .tar.gzip 包:tar -zxvf 包名
1.1 tar 打包

语法格式:tar [选项] 文件或目录

功能描述:将文件或目录打包成 .tar 文件

选项参数:

选项描述
-c对多个文件或目录进行打包操作
-v显示打包文件过程
-f 包名指定包名

案例演示:

[root@joel ~]# ls
test        test1.txt       test2.txt

# 将test文件夹打包为test.tar
[root@joel ~]# tar -cvf test.tar test
test/
[root@joel ~]# ls
test        test.tar       test1.txt       test2.txt
# 将 test1.txt test2.txt 打包进 Tests.tar
[root@joel ~]# tar -cvf Tests.tar test1.txt test2.txt 
test        test.tar       test1.txt       test2.txt      Tests.tar

⚠️ 注意:选项 -f 后就是包名,所以选项的顺序上只能将 f 放最后!

1.2 tar 解打包

语法格式:tar [选项] tar包

功能描述:向文件或目录打包

选项参数:

选项描述
-x对 tar 包做解打包操作
-v显示解打包的具体过程
-f 包名指定解打包后的文件名
-C 目录指定解打包位置

案例演示:

[root@joel ~]# ls
test.tar

# 将test.tar解打包到当前目录
[root@joel ~]# tar -xvf test.tar
test/
[root@joel ~]# ls
test  test.tar
# 将test.tar解打包到demo目录
[root@joel ~]# tar -xvf test.tar -C demo/
test/
[root@joel ~]# ls demo
test

⚠️ 注意:-xvf 选项会将tar包解到当前目录,并且不需要指定文件名

1.2 tar 查看包

语法格式:tar -t tar包

功能描述:查看 tar 包中有哪些文件或目录,需要搭配 v、f 选项

案例演示:

# 显示 Tests.tar 中的内容
[root@joel ~]# tar -tf Tests.tar
test1.txt
test2.txt

# 详细显示 Tests.tar 中的内容
[root@joel ~]# tar -tvf Tests.tar
-rw-r--r-- root/root        20 2021-11-11 22:35 test1.txt
-rw-r--r-- root/root         8 2021-11-11 19:33 test2.txt
2. zip

在 Win 上,我们看到更多的是 .zip 压缩文件,Linux 也支持该格式的压缩文件,并提供了配套的解压缩命令。

2.1 zip 压缩

语法格式:zip [选项] 压缩包名 文件或目录

功能描述:将文件或目录压缩为 .zip 文件

选项参数:

选项描述
-r递归压缩目录
-m将文件压缩之后,删除原始文件
-v显示详细的压缩过程信息
-q在压缩的时候不显示命令的执行过程
-压缩级别压缩级别是从 1~9 的数字,-1 代表压缩速度更快,-9 代表压缩效果更好。
-u向压缩文件中添加新文件

案例演示:

# 压缩文件 test 为  test.zip
[root@joel ~]# ls
test.txt  
[root@joel ~]# zip test.zip test
  adding: test/ (stored 0%)
[root@joel ~]# ls
test.txt  test.zip
# 压缩目录 test 为 test.zip ,并通过选项 V 显示详情
[root@joel ~]# ls test
t1.txt  t2.txt  t3.txt
[root@joel ~]# zip -cv test.zip test
  adding: test/	(in=0) (out=0) (stored 0%)
Enter comment for test/: # 按回车

total bytes=0, compressed=0 -> 0% savings
2.2 unzip 解压缩

语法格式:unzip [选项] 压缩包名

功能描述:将 .zip 文件解压

选项参数:

选项描述
-d 目录名将压缩文件解压到指定目录下
-n解压时并不覆盖已经存在的文件
-o解压时覆盖已经存在的文件,并且无需用户确认
-v查看压缩文件的详细信息
-t测试压缩文件有无损坏,但不解压
-x 文件列表解压文件,但不包含文件列表中指定的文件

案例演示:

[root@joel ~]# ls
test.zip

# 将 test.zip 解压至当前目录
[root@joel ~]# unzip test.zip
Archive:  test.zip
   creating: test/
[root@joel ~]# ls
test  test.zip
# 将 test.zip 解压至当前指定目录
[root@joel ~]# unzip -d /test test.zip
Archive:  test.zip
   creating: /test/test/

⚠️ 注意:确保支持 unzip 命令,若没有可以利用 yum install -y unzip zip 下载上。

3. gzip

以后缀为 .gz 的压缩包也是常见的,指的是 gzip 压缩文件 ,gzip 是常用的对文件压缩解压缩的命令,比如 jdk 的某压缩包名为 jdk-8u291-linux-x64.tar.gz 就需要使用该类型的命令来解压。

3.1 gzip 压缩

语法格式:gzip [选项] 文件

功能描述:将 .gz 文件压缩,只能压缩文件,不能压缩目录

选项参数:

选项描述
-c将压缩数据输出到标准输出中,并保留源文件
-d对压缩文件进行解压缩
-r递归压缩指定目录
-v显示相应的文件名和压缩比
-l显示压缩文件的详细信息
-数字用于指定压缩等级,-1~9。默认 -6

案例演示:

[root@joel ~]# ls
test.txt

# 压缩 test.txt 文件,默认将源文件删除
[root@joel ~]# gzip test.txt
[root@joel ~]# ls
test.txt.gz
3.3 ungzip 解压缩

语法格式:gunzip [选项] 文件

功能描述:将 .gz 文件解压

选项参数:

选项描述
-r递归解压目录
-c把解压缩后的文件输出到标准输出设备
-f强制解压缩文件
-l列出压缩文件内容
-v显示命令执行过程
-t测试压缩文件是否正常,但不解压

案例演示:

[root@joel ~]# ls
test.txt.gz

# 解压 test.txt.gz 文件,默认将源文件删除
[root@joel ~]# gunzip test.txt.gz 
[root@joel ~]# ls
test.txt
4. bzip2

bzip2 压缩包可能见的就少了,该命令只能对文件压缩解压缩,后缀为 .bz2 。Linux 中如果不能使用,yum install bzip2 下载就是了。

4.1 bzip2 压缩

语法格式:bzip2 [选项] 文件

功能描述:将文件压缩为 .bz2 压缩包

选项参数:

选项描述
-d将文件压缩
-k压缩后,保留原文件(默认不保留)
-f强制覆盖现有文件(默认不覆盖)
-t测试压缩包文件的完整性
-v压缩或解压缩文件时,显示详细信息。
-数字用于指定压缩等级,-1~9。默认 -6

案例演示:

[root@joel ~]# ls
test.txt

# 压缩 test.txt 文件,默认将源文件删除
[root@joel ~]# bzip2 test.txt
[root@joel ~]# ls
test.txt.bz2

4.2 bunzip2 解压缩

语法格式:bunzip2 [选项] 文件

功能描述:将 .bz2 压缩包解压

选项参数:

选项描述
-k解压缩后保留原文件(默认删除)
-f覆盖现有文件(默认不覆盖)
-v显示命令执行过程
-L列出压缩文件内容

案例演示:

[root@joel ~]# ls
test.txt.bz2

# 解压 test.txt.bz2 文件,默认将源文件删除
[root@joel ~]# bunzip2 test.txt.bz2 
[root@joel ~]# ls
test.txt
5. 写在最后

使用如果使用tar打包,又使用gzip将其压缩,我们解压时使用 tar -zxvf 组合命令就OK了,z 就代表了 .gz 文件格式。

 


❤️ END ❤️
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/511238.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号