Linux的压缩包管理

压缩包的主要好处有:

  • 节省磁盘空间占用率
  • 节省网络传输带宽消耗
  • 网络传输更加快捷

Linux系统常见的后缀名所对应的压缩工具:

  • .gz   gzip    #压缩工具压缩的文件
  • .bz2   bzip2   # 压缩工具压缩的文件
  • .tar    tar   # tar没有压缩功能,只是把一个目录合并成一个文件
  • .tar.gz      #先使用tar打包,然后使用gzip压缩归档
  • .tar.bz2    #先使用tar打包,然后使用bzip压缩归档
  • .tar.xz      #先使用tar打包,然后使用xz压缩归档

Linux下常用压缩文件以.tar.gz结尾

Linux下压缩文件必须带后缀

zip/unzip解压缩工具

zip是压缩工具,unzip是解压工具

-q:不显示指令执行过程;

-r:递归处理,将指定目录下的所有文件和子目录一并处理;

[root@lzg ~]# ls
oldboy1.txt  oldboy2.txt  oldboy3.txt
[root@lzg ~]# zip -q oldboy.zip oldboy*                    #压缩文件
[root@lzg ~]# ls
oldboy1.txt  oldboy2.txt  oldboy3.txt  oldboy.zip

[root@lzg ~]# ls
file_1  file_2  file_3
[root@lzg ~]# zip -q -r file.zip file_*                    #压缩目录
[root@lzg ~]# ls
file_1  file_2  file_3  file.zip

[root@lzg ~]# unzip oldboy.zip                             #解压缩
[root@lzg ~]# unzip file.zip                   #解压缩

tar压缩工具

tar是Linux下最常用的压缩与解压缩工具,支持文件和目录的压缩(原始含义归档,不带压缩功能)。

按照 tar 选项的行为,分为三类:

常用组合有:

  • tf  #查看所有tar类压缩包内容(.tar/.tar.gz/.tar.bz2/.tar.xz)
  • xf  #解压缩所有tar类压缩包(.tar/.tar.gz/.tar.bz2/.tar.xz)

tar命令选项

-A或--catenate:新增文件到已存在的备份文件;
-B:设置区块大小;
-c或--create:建立新的备份文件;
-C <目录>:这个选项用在解压缩,若要在特定目录解压缩,可以使用这个选项。
-X<文件名列表>:排除多个文件,写入需要排除的文件名名称
-d:记录文件的差别;
-x或--extract或--get:从备份文件中还原文件;
-t或--list:列出备份文件的内容;
-z或--gzip或--ungzip:通过gzip指令处理备份文件;
-Z或--compress或--uncompress:通过compress指令处理备份文件;
-f<备份文件>或--file=<备份文件>:指定备份文件;
-v或--verbose:显示指令执行过程;
-r:添加文件到已经压缩的文件;
-u:添加改变了和现有的文件到已经存在的压缩文件;
-j:支持bzip2解压文件;
-v:显示操作过程或者列出详细信息;
-l:文件系统边界设置;
-k:保留原有文件不覆盖;
-m:保留文件不被覆盖;
-w:确认压缩文件的正确性;
-p或--same-permissions:用原来的文件权限还原文件;
-P或--absolute-names:文件名使用绝对名称,不移除文件名称前的“/”号;
-N <日期格式> 或 --newer=<日期时间>:只将较指定日期更新的文件保存到备份文件里;
--exclude=<范本样式>:排除符合范本样式的文件或目录。
--hard-dereference:打包硬链接

tar命令示例

压缩文件或目录

[root@lzg ~]# ls
test1.txt  test2.txt  test3.txt
[root@lzg ~]# tar -cvf test.tar test*        #仅打包,不压缩
test1.txt
test2.txt
test3.txt
[root@lzg ~]# ls
test1.txt  test2.txt  test3.txt  test.tar
[root@lzg ~]# tar -czvf test.tar.gz test?.txt     #打包后,以gzip压缩
test1.txt
test2.txt
test3.txt
[root@lzg ~]# ls
test1.txt  test2.txt  test3.txt  test.tar  test.tar.gz
[root@lzg ~]# tar -cjvf test.tar.bz2 test?.txt          #打包后,以bzip2压缩
test1.txt
test2.txt
test3.txt
[root@lzg ~]# ls
test1.txt  test2.txt  test3.txt  test.tar  test.tar.bz2  test.tar.gz
[root@lzg ~]# tar -cJvf test.tar.xz test?.txt           #打包后,以xz压缩
test1.txt
test2.txt
test3.txt
[root@lzg ~]# ls
test1.txt  test2.txt  test3.txt  test.tar  test.tar.bz2  test.tar.gz  test.tar.xz

-p 保留原文件权限压缩:

[root@lzg ~]# ls
file1  file2  file3  
[root@lzg ~]# tar -czvpf file.tar.gz file? 

--exclude排除指定的单个文件进行压缩:

[root@lzg ~]# ls
test1.txt  test2.txt  test3.txt  test4.txt  test5.txt
[root@lzg ~]# tar -czvf test.tar.gz --exclude test3.txt test?.txt            #排除test3.txt文件
test1.txt
test2.txt
test4.txt
test5.txt
#或者
[root@lzg ~]# tar --exclude test3.txt -czvf test1.tar.gz test?.txt           #排除test3.txt文件
test1.txt
test2.txt
test4.txt
test5.txt
[root@lzg ~]# ls
test1.tar.gz  test1.txt  test2.txt  test3.txt  test4.txt  test5.txt  test.tar.gz

-X 排除多个文件:

[root@lzg ~]# ls
exclude.list  test1.txt  test2.txt  test3.txt  test4.txt  test5.txt
[root@lzg ~]# cat exclude.list      #在exclude.list里面输入需要排除的文件名称(一行一个文件)
test2.txt
test4.txt
[root@lzg ~]# tar -X exclude.list -czvf test.tar.gz test?.txt     #-X后面跟指定需要排除文件的文件列表
test1.txt
test3.txt
test5.txt
[root@lzg ~]# ls
exclude.list  test1.txt  test2.txt  test3.txt  test4.txt  test5.txt  test.tar.gz

查看压缩包里的内容

[root@lzg ~]# tar -tf file.tar.gz       #查看压缩包里的内容
file1/
file1/a.txt
file1/fiel_log/
file1/fiel_log/mm.txt
file2/
file2/b.txt
file3/
[root@lzg ~]# tar -tvf file.tar.gz     #以长格式的方式列出压缩包里的内容
drwxr-xr-x root/root         0 2019-11-20 10:08 file1/
-rw-r--r-- root/root        14 2019-11-20 10:07 file1/a.txt
drwxr-xr-x root/root         0 2019-11-20 10:09 file1/fiel_log/
-rw-r--r-- root/root        13 2019-11-20 10:09 file1/fiel_log/mm.txt
drwxr-xr-x root/root         0 2019-11-20 10:08 file2/
-rw-r--r-- root/root        12 2019-11-20 10:08 file2/b.txt
drwxr-xr-x root/root         0 2019-11-20 10:05 file3/

解压压缩包里的内容

[root@lzg ~]# ls
file.tar  file.tar.bz2  file.tar.gz
[root@lzg ~]# tar -xf file.tar.gz           #解压压缩包(不显示过程)
#或者
[root@lzg ~]# tar -xvf file.tar.gz          #解压压缩包(显示过程)
file1/
file1/a.txt
file1/fiel_log/
file1/fiel_log/mm.txt
file2/
file2/b.txt
file3/
[root@lzg ~]# ls
file1  file2  file3  file.tar  file.tar.bz2  file.tar.gz

只将压缩包里的部分文件解压出来,可以先查看压缩包里的文件名,再根据需要解压文件: 

[root@lzg ~]# ls
file.tar.gz
[root@lzg ~]# tar -tf file.tar.gz       #查看压缩包里的文件
file1/
file1/a.txt
file1/fiel_log/
file1/fiel_log/mm.txt
file2/
file2/b.txt
file3/
[root@lzg ~]# tar -xvf file.tar.gz file1/a.txt file2  #解压缩file1里面的a.txt和file2,通过操作过程可以看到file1里的文件只有a.txt解压出来了,file2里的所有文件都解压出来了
file1/a.txt
file2/
file2/b.txt
[root@lzg ~]# tree
.
├── file1
│   └── a.txt
├── file2
│   └── b.txt
└── file.tar.gz

2 directories, 3 files

-C 将压缩包里的文件解压到指定的目录下面: 

[root@lzg ~]# ls
file.tar.gz
[root@lzg ~]# tar -xvf file.tar.gz -C /tmp/
file1/
file1/a.txt
file1/fiel_log/
file1/fiel_log/mm.txt
file2/
file2/b.txt
file3/
[root@lzg ~]# ls /tmp/
file1  file2  file3
[root@lzg ~]# tree /tmp/
/tmp/
├── file1
│   ├── a.txt
│   └── fiel_log
│       └── mm.txt
├── file2
│   └── b.txt
└── file3

4 directories, 3 files

gzip/gunzip解压缩工具

压缩文件

gzip【需要压缩的文件】

注意:只能压缩文件,不能压缩目录;压缩完后不保留原文件

解压文件

gunzip【压缩文件名】

注意:解压后不保留原文件

bzip2/bunzip2解压缩工具

压缩文件

bzip2 [选项] 文件路径

常用选项:

  • -d:解压缩
  • -k:压缩后保留源文件
  • -v:显示详细信息

压缩示例:

[root@VM-0-12-centos ceshi]# ls
ch
[root@VM-0-12-centos ceshi]#
[root@VM-0-12-centos ceshi]# bzip2 ch
[root@VM-0-12-centos ceshi]#
[root@VM-0-12-centos ceshi]# ls
ch.bz2
[root@VM-0-12-centos ceshi]#

#(直接bzip后面接要压缩的文件,就会自动删除源文件,并生成一个源文件的.bz2压缩包)

解压示例:

[root@localhost ceshi]# ls
ch.bz2
[root@localhost ceshi]#
[root@localhost ceshi]# bzip2 -d ch.bz2
[root@localhost ceshi]#
[root@localhost ceshi]# ls
ch
[root@localhost ceshi]#

#(-d选项即为解压缩.bz2格式的压缩包)

解压文件

虽然bzip2 -d选项可以用来解压缩,但是其实解压缩.bz2格式的压缩包也是可以用单独的命令来解压缩的。

bunzip2 文件路径

示例:

[root@VM-0-12-centos ceshi]# ls
ch.bz2
[root@VM-0-12-centos ceshi]# bunzip2 ch.bz2
[root@VM-0-12-centos ceshi]#
[root@VM-0-12-centos ceshi]# ls
ch
[root@VM-0-12-centos ceshi]#

#(如上bunzip2后面接要解压缩的.bz2格式的压缩包即可解压)

 

posted @   残城碎梦  阅读(75)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2021-12-05 创建SpringBoot项目的3种方式
2021-12-05 分布式事务原理
点击右上角即可分享
微信分享提示