【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文件解压到指定文件内.

相关推荐
城南云小白10 分钟前
Linux网络服务只iptables防火墙工具
linux·服务器·网络
从心归零11 分钟前
sshj使用代理连接服务器
java·服务器·sshj
咩咩大主教11 分钟前
C++基于select和epoll的TCP服务器
linux·服务器·c语言·开发语言·c++·tcp/ip·io多路复用
羌俊恩16 分钟前
视频服务器:GB28181网络视频协议
服务器·网络·音视频
Zww089138 分钟前
docker部署个人网页导航
运维·docker·容器
Flying_Fish_roe38 分钟前
linux-网络管理-网络配置
linux·网络·php
运维小白。。41 分钟前
Nginx 反向代理
运维·服务器·nginx·http
PeterJXL41 分钟前
Docker-compose:管理多个容器
运维·docker·容器
FuLLovers41 分钟前
2024-09-13 冯诺依曼体系结构 OS管理 进程
linux·开发语言
海王正在撒网1 小时前
用 Docker 部署 Seafile 社区版
运维·docker·容器