首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >《深度揭秘 Linux 压缩打包全体系:tar、gzip、zip 与 rar 的使用精髓》

《深度揭秘 Linux 压缩打包全体系:tar、gzip、zip 与 rar 的使用精髓》

作者头像
XPcode7
发布2025-10-23 14:53:44
发布2025-10-23 14:53:44
5530
举报

《深度揭秘 Linux 压缩打包全体系:tar、gzip、zip 与 rar 的使用精髓》

导读:在 Linux 系统的日常使用与运维工作中,文件的压缩与打包是一项极为基础且关键的技能。无论是节省磁盘空间、方便文件传输,还是进行数据备份,都离不开对各种压缩工具的熟练运用。然而,面对 tar、gzip、zip 以及 rar 等多种不同格式的压缩工具,许多 Linux 用户常常感到困惑,不知如何根据实际需求选择最合适的工具,也难以充分发挥它们的强大功能。 本文将带你深入探索 Linux下的压缩打包世界,详细剖析 tar、gzip、zip 与 rar 这几种常见压缩工具的用法。从基础的压缩与解压缩操作,到归档、查看压缩包内容等进阶技巧,再到不同工具之间的差异对比,以及在实际场景中的最佳应用策略,我们都将一一为你呈现。无论你是初涉 Linux 领域的新手,还是经验丰富的系统管理员,都能从本文中获取到实用的知识与技能,迅速提升在 Linux环境下处理文件压缩与打包任务的能力。

2.13.1 tar------(.tar.gz结尾)
1-1 tar压缩和解压缩
代码语言:javascript
复制
# 压缩文件有时候我们也叫做归档文件。但是归档和压缩有一些区别,归档只是将多个文件捆绑成一个文件,并没有压缩,而压缩才是将大小压缩的更小。

#Linux最常用的压缩和解压指令是:
## tar:能够解压的文件格式是xx.tar.gz
     ## 压缩:tar -zcf 压缩包路径 目标1 目标2 目标3 ...
     ## 解压:tar -zxf 解压路径
        
        
## 例子1:压缩和解压文件
    [root@localhost ~]# ls
    ## 123.txt  4.txt   a.txt c.txt     code.txt 1.txt   anaconda-ks.cfg   b.txt   code.tar.gz   services
    
    ## 删除所有文件
    [root@localhost ~]# rm -f *
    [root@localhost ~]# ls
    
    ## 复制文件
    [root@localhost ~]# cp /etc/services .
    ## 压错
    [root@localhost ~]# tar -zcf code.tar.gz services  #把services 文件压缩成code.tar.gz
    [root@localhost ~]# ls -lh
    -rw-r--r--  1 root root 134K 3月  22 14:51 code.tar.gz
    -rw-r--r--  1 root root 655K 3月  22 11:22 services
        
    [root@localhost ~]# rm -f services # 删除原文件之后就再解压
    [root@localhost ~]# tar -zxf code.tar.gz   # 解压文件
    [root@localhost ~]# ls -lh
    -rw-r--r--  1 root root 134K 3月  22 14:51 code.tar.gz
    -rw-r--r--  1 root root 655K 3月  22 11:22 services  #看到和源文件一样的文件,包括文件属性也一样。
        
        
## 同时压缩好几个文件
    ## 把1.txt 和 aini 目录分别压缩成kk.tar.hz和jj.tar.gz
    tar -zcf  kk.tar.jz jj.tar.gz 1.txt aini  
    
    ## 把1.txt 和 code压缩到/tmp/oo.tar.gz
    tar -zcf /tmp/oo.tar.gz 1.txt code
1-2 tar归档
代码语言:javascript
复制
#归档,但是不压缩
    tar -cf
    [root@localhost ~]# cp /etc/services ./shike # 再拷贝一个services文件过来
    [root@localhost ~]# ls -lh
    -rw-r--r-- 1 root root 13M 3月  22 14:58 2.tar.gz
    -rw-r--r-- 1 root root 134K 3月  22 14:56 code.tar.gz
    -rw-r--r-- 1 root root 655K 3月  22 14:55 services
    -rw-r--r-- 1 root root 655K 3月  22 15:04 shike
        
    [root@localhost ~]# tar -cf 3.tar.gz services shike #归档,但是不压缩
    [root@localhost ~]# ls -lh

    -rw-r--r-- 1 root root 13M 3月  22 14:58 2.tar.gz
    -rw-r--r-- 1 root root 1.3M 3月  22 15:04 3.tar.gz  #看大小就知道没有压缩大小。
    -rw-r--r-- 1 root root 134K 3月  22 14:56 code.tar.gz
    -rw-r--r-- 1 root root 655K 3月  22 14:55 services
    -rw-r--r-- 1 root root 655K 3月  22 15:04 shike
1-3 查看压缩包内容
代码语言:javascript
复制
[root@localhost ~]# tar -tf 3.tar.gz 
services
shike

## tar这个指令的参数可以不加
## tar tf 3.tar.gz

## linux系统下解压文件的时候,不同格式的压缩包需要使用不同的命令来解压或者压缩。
2.13.2 gzip----(.gz结尾)
代码语言:javascript
复制
#打包和压缩
    gzip
    #压缩文件,会自动删除原文件,和tar不同,tar会留着原文件
    [root@localhost ~]# gzip services 
    [root@localhost ~]# ls -lh
    总用量 15M
    -rw-r--r-- 1 root root 133K 3月  22 14:55 services.gz
    -rw-r--r-- 1 root root 655K 3月  22 15:04 shike
        
# 解压,会自动删除原压缩包
    [root@localhost ~]# gzip -d services.gz 
    [root@localhost ~]# ls -lh
    总用量 16M
    -rw-r--r-- 1 root root 655K 3月  22 14:55 services
    -rw-r--r-- 1 root root 655K 3月  22 15:04 shike
        
#压缩多个文件,每一个文件产生一个单独的压缩包
    [root@localhost ~]# gzip services shike 
    [root@localhost ~]# ls -lh
    总用量 15M
    -rw-r--r-- 1 root root 133K 3月  22 14:55 services.gz
    -rw-r--r-- 1 root root 133K 3月  22 15:04 shike.gz
        
#解压缩
    [root@localhost ~]# gzip -d services.gz shike.gz 
    [root@localhost ~]# ls -lh
    总用量 16M
    -rw-r--r-- 1 root root 655K 3月  22 14:55 services
    -rw-r--r-- 1 root root 655K 3月  22 15:04 shike
        
## gzip其实感觉并不太好用,但是工作中我们可能会遇到gzip的压缩包。
2.13.3 zip (.zip结尾)
代码语言:javascript
复制
#压缩
    zip
    例子1:
    [root@localhost ~]# zip -r 1.zip services shike #会保留原文件
     adding: services (deflated 80%)
     adding: shike (deflated 80%)
    [root@localhost ~]# ls -lh
    总用量 16M
    -rw-r--r-- 1 root root 267K 3月  22 15:25 1.zip
    -rw-r--r-- 1 root root 655K 3月  22 14:55 services
    -rw-r--r-- 1 root root 655K 3月  22 15:04 shike
        
#解压
    unzip
    例子1: # 解压之前先把原文件删掉,以免冲突
    [root@localhost ~]# unzip 1.zip 
    [root@localhost ~]# ls -lh
    总用量 16M
    -rw-r--r-- 1 root root 267K 3月  22 15:25 1.zip
    -rw-r--r-- 1 root root 655K 3月  22 14:55 services
    -rw-r--r-- 1 root root 655K 3月  22 15:04 shike
2.13.4 rar解压
代码语言:javascript
复制
## windows上常见的rar格式的压缩包,在linux上其实比较难解压,需要我们安装专业的工具:
    #解压rar包
    #需要安装软件
        yum install epel-release -y
        yum install unar -y
    #再进行解压
        unar -o 解压路径 被解压文件路径
        
        ## 例如:
         unar -o /opt 456.rar
            
## 如下
[root@localhost ~]# ls
1.zip  2.tar.gz  3.tar.gz code.rar code.tar services shike

[root@localhost ~]# mkdir xx
[root@localhost ~]# unar -o ./xx code.rar ## 把code.rar 解压到./xx目录下
code.rar: RAR 5
 code.txt (49 B)... OK.
Successfully extracted to "./xx/code.txt".
[root@localhost ~]# ls xx/
code.txt

## 尽量不要给linux发送rar的压缩包。全世界通用的是zip格式的压缩包。好,关于压缩和解压我们就先说这么多。

总结:

希望这篇文章能够为你在探索 Linux系统的道路上提供有力的帮助,让你在操作 Linux 系统时更加得心应手。 我是旺仔SeC,是一名热衷于系统技术的博主,对 Linux系统有着深入的研究和丰富的实践经验。我致力于分享各种系统相关的知识和技巧,帮助大家解决在使用过程中遇到的各种问题。如果你觉得这篇文章对你有所帮助,欢迎点赞、收藏和分享哦 同时,也希望你能关注我的博客,我会持续为大家带来更多优质的系统技术文章,包括但不限于 Linux系统的深入探索、系统优化、故障排除以及各种有趣的实战案例。 如果你在学习和使用 Linux 系统的过程中遇到任何问题,或者对某些内容有疑问,欢迎在评论区留言,我会尽力为大家解答。让我们一起在系统技术的海洋中遨游,不断提升自己的技能水平,共同探索系统世界的奥秘 再次感谢大家的阅读,期待我们下次再见!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-01-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 《深度揭秘 Linux 压缩打包全体系:tar、gzip、zip 与 rar 的使用精髓》
    • 2.13.1 tar------(.tar.gz结尾)
    • 2.13.2 gzip----(.gz结尾)
    • 2.13.3 zip (.zip结尾)
    • 2.13.4 rar解压
    • 总结:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档