【Linux篇】Linux的压缩与解压

1. 压缩格式

市面上有非常多的压缩格式:

  • zip格式:Linux,win,MacOS,常用
  • 7zip:win常用
  • rar:win常用
  • tar:Linux,MacOS常用
  • gzip:Linux,MacOS常用

如何在Linux系统中操作tar,gzip,zip这三种压缩格式,完成文件的压缩与解压。

2. tar命令

Linux和Mac系统常用的有2种压缩格式,后缀名分别是:

  • .tar,称之为tarball,归档文件,即简单的将文件组装到一个.tar文件内,并没有太多的文件体积的减少,仅仅是简单的封装。
  • .gz,也常见为.tar.gz,gzip格式的压缩文件,即使用gzip压缩算法将文件压缩到一个文件内,可以极大的减少压缩后的体积。

针对这两种格式,使用tar命令均可以进行压缩和解压缩的操作。

语法:tar -c -v -x -f -z-C 参数1 参数2 ...

  • -c,创建压缩文件,用于压缩格式。
  • -v,显示压缩,解压过程,用于查看进度。
  • -x,解压模式
  • -f,要创建的文件,或要解压的文件,-f选项必须在所有选项中位置位于最后一个。
  • -z,gzip格式,不使用-z就是普通的tarball格式。
  • -C,选择解压的目的地,用于解压格式。

2.1 tar命令压缩

tar常用的组合为:

  • tar -cvf test.tar 1.txt 2.txt 3.txt

将1.txt 2.txt 3.txt三个文件压缩到test.tar中。

复制代码
[root@hexuan ~]# touch 1.txt
[root@hexuan ~]# touch 2.txt
[root@hexuan ~]# touch 3.txt
[root@hexuan ~]# ll
总用量 8
-rw-r--r--. 1 root root    0 8月  27 16:00 1.txt
-rw-r--r--. 1 root root    0 8月  27 16:01 2.txt
-rw-r--r--. 1 root root    0 8月  27 16:01 3.txt
-rw-------. 1 root root 1749 6月  14 13:21 anaconda-ks.cfg
-rw-r--r--. 1 root root 1797 6月  14 13:22 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 6月  14 13:23 公共
drwxr-xr-x. 2 root root    6 6月  14 13:23 模板
drwxr-xr-x. 2 root root    6 6月  14 13:23 视频
drwxr-xr-x. 3 root root   24 6月  14 14:01 图片
drwxr-xr-x. 2 root root    6 6月  14 13:23 文档
drwxr-xr-x. 2 root root    6 6月  14 13:23 下载
drwxr-xr-x. 2 root root    6 6月  14 13:23 音乐
drwxr-xr-x. 2 root root    6 6月  14 13:23 桌面
[root@hexuan ~]# tar -cvf test.tar 1.txt 2.txt 3.txt
1.txt
2.txt
3.txt
[root@hexuan ~]# ll
总用量 20
-rw-r--r--. 1 root root     0 8月  27 16:00 1.txt
-rw-r--r--. 1 root root     0 8月  27 16:01 2.txt
-rw-r--r--. 1 root root     0 8月  27 16:01 3.txt
-rw-------. 1 root root  1749 6月  14 13:21 anaconda-ks.cfg
-rw-r--r--. 1 root root  1797 6月  14 13:22 initial-setup-ks.cfg
-rw-r--r--. 1 root root 10240 8月  27 16:01 test.tar
drwxr-xr-x. 2 root root     6 6月  14 13:23 公共
drwxr-xr-x. 2 root root     6 6月  14 13:23 模板
drwxr-xr-x. 2 root root     6 6月  14 13:23 视频
drwxr-xr-x. 3 root root    24 6月  14 14:01 图片
drwxr-xr-x. 2 root root     6 6月  14 13:23 文档
drwxr-xr-x. 2 root root     6 6月  14 13:23 下载
drwxr-xr-x. 2 root root     6 6月  14 13:23 音乐
drwxr-xr-x. 2 root root     6 6月  14 13:23 桌面
  • tar -zcvf test.tar.gz 1.txt 2.txt 3.txt

将1.txt 2.txt 3.txt三个文件压缩到test.tar.gz中,使用gzip格式。

注意:

  • -z选项如果使用,一般位于选项的最后一个位置。

  • -f选项,必须位于选项的最后一个。

    [root@hexuan ~]# tar -zcvf test.tar.gz 1.txt 2.txt 3.txt
    1.txt
    2.txt
    3.txt
    [root@hexuan ~]# ll
    总用量 24
    -rw-r--r--. 1 root root 0 8月 27 16:00 1.txt
    -rw-r--r--. 1 root root 0 8月 27 16:01 2.txt
    -rw-r--r--. 1 root root 0 8月 27 16:01 3.txt
    -rw-------. 1 root root 1749 6月 14 13:21 anaconda-ks.cfg
    -rw-r--r--. 1 root root 1797 6月 14 13:22 initial-setup-ks.cfg
    -rw-r--r--. 1 root root 10240 8月 27 16:01 test.tar
    -rw-r--r--. 1 root root 128 8月 27 16:02 test.tar.gz
    drwxr-xr-x. 2 root root 6 6月 14 13:23 公共
    drwxr-xr-x. 2 root root 6 6月 14 13:23 模板
    drwxr-xr-x. 2 root root 6 6月 14 13:23 视频
    drwxr-xr-x. 3 root root 24 6月 14 14:01 图片
    drwxr-xr-x. 2 root root 6 6月 14 13:23 文档
    drwxr-xr-x. 2 root root 6 6月 14 13:23 下载
    drwxr-xr-x. 2 root root 6 6月 14 13:23 音乐
    drwxr-xr-x. 2 root root 6 6月 14 13:23

2.2 tar命令压缩

常用的tar解压组合有:

  • tar -xvf test.tar

解压test.tar,将文件解压至当前目录。

  • tar -xvf test.tar -C /home/hexuan

解压test.tar,将文件解压至指定目录

  • tar -zxvf test.tar.gz -C /home/hexuan

以gzip格式解压文件,将文件压缩到指定目录。

注意:

  • -f选项,必须在选项组合体的最后一位。

  • -z选项,建议在开头位置。

  • -C选项单独使用,和解压所需的其他参数分开。

    [root@hexuan ~]# rm -rf *.txt
    [root@hexuan ~]# ll
    总用量 24
    -rw-------. 1 root root 1749 6月 14 13:21 anaconda-ks.cfg
    -rw-r--r--. 1 root root 1797 6月 14 13:22 initial-setup-ks.cfg
    -rw-r--r--. 1 root root 10240 8月 27 16:01 test.tar
    -rw-r--r--. 1 root root 128 8月 27 16:02 test.tar.gz
    drwxr-xr-x. 2 root root 6 6月 14 13:23 公共
    drwxr-xr-x. 2 root root 6 6月 14 13:23 模板
    drwxr-xr-x. 2 root root 6 6月 14 13:23 视频
    drwxr-xr-x. 3 root root 24 6月 14 14:01 图片
    drwxr-xr-x. 2 root root 6 6月 14 13:23 文档
    drwxr-xr-x. 2 root root 6 6月 14 13:23 下载
    drwxr-xr-x. 2 root root 6 6月 14 13:23 音乐
    drwxr-xr-x. 2 root root 6 6月 14 13:23 桌面
    [root@hexuan ~]# tar -xvf test.tar -C /home/hexuan
    1.txt
    2.txt
    3.txt
    [root@hexuan ~]# su - hexuan
    上一次登录:日 8月 25 21:02:06 CST 2024pts/0 上
    [hexuan@hexuan ~]$ ll
    总用量 4
    -rw-r--r--. 1 root root 0 8月 27 16:00 1.txt
    -rw-r--r--. 1 root root 0 8月 27 16:01 2.txt
    -rw-r--r--. 1 root root 0 8月 27 16:01 3.txt
    drwxr-xr-x. 2 hexuan hexuan 6 8月 23 21:46 Desktop
    drwxr-xr-x. 2 hexuan hexuan 6 8月 23 21:46 Documents
    drwxr-xr-x. 2 hexuan hexuan 6 8月 23 21:46 Downloads
    -rw-rw-r--. 1 hexuan hexuan 52 8月 24 19:43 hello.txt
    drwxrwxr-x. 3 hexuan hexuan 21 8月 23 13:16 itheima
    drwxr-xr-x. 2 hexuan hexuan 6 8月 23 21:46 Music
    drwxr-xr-x. 2 hexuan hexuan 6 8月 23 21:46 Pictures
    drwxr-xr-x. 2 hexuan hexuan 6 8月 23 21:46 Public
    drwxr-xr-x. 2 hexuan hexuan 6 8月 23 21:46 Templates
    drwxr-xr-x. 2 hexuan hexuan 6 8月 23 21:46 Videos
    lrwxrwxrwx. 1 hexuan hexuan 8 8月 25 20:30 yum -> /etc/yum
    drwxr-xr-x. 3 hexuan hexuan 263 8月 22 22:16 图片
    drwxr-xr-x. 2 hexuan hexuan 6 8月 23 21:46 桌面

3. zip命令

3.1 zip命令压缩

可以使用zip命令,压缩文件为zip压缩包。

语法:zip -r 参数1 参数2 ...

  • -r,被压缩的包含文件夹时,需要使用-r选项,和rm,cp命令的-r效果一致。

示例:

zip test.zip a.txt b.txt c.txt

将a.txt b.txt c.txt压缩到test.zip文件内。

3.2 zip命令解压

使用unzip命令,可以方便的解压zip压缩包。

语法:unzip -d 参数

  • -d,指定要解压去的位置,同tar的-C选项。
  • 参数,被解压的zip压缩包文件。

示例:

unzip test.zip -d /root

将test.zip文件解压到指定文件内.

相关推荐
静止了 所有的花开7 分钟前
Ubuntu 26.04 静态 IP 配置不生效解决方法
linux·ubuntu·静态ip
承渊政道1 小时前
KES专业技能包发布:覆盖数据库开发、迁移与运维全流程
运维·数据库·gitee·数据库开发·金仓数据库
笨鸟先飞,勤能补拙1 小时前
Web 服务器与计算机网络全景原理
运维·服务器·前端·网络·人工智能·计算机网络·web安全
caimouse1 小时前
ReactOS 图形系统分析(3):显示驱动 DDI framebuf.dll
linux·运维·网络
跨境小彭1 小时前
Temu运营优化方案:活动库存自动化管控,解决断流、超卖与权重下跌问题
大数据·运维·人工智能·自动化·跨境电商·temu·temu电商运营
漫谈数据智理2 小时前
IDS-RAM 如何支撑数据空间走向可
大数据·运维·微服务
佳児素花痴╮2 小时前
网络问题与基础理论
运维·服务器·网络
tanglinS2 小时前
工业陶瓷在半导体设备里都用哪里?氮化铝基板与氮化硅结构件的静电无尘角色
大数据·运维·人工智能·自动化·材质
highreport2 小时前
HighReport报表工具定时调度和邮件定时发送
运维·服务器·数据库
TonyLee0172 小时前
Claude Code + DeepSeek:服务器安装与使用笔记
linux·服务器·deepseek·claude code