首页 >> 知识 >> Linux篇Linux的压缩与解压

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.cfgdrwxr-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.txt1.txt2.txt3.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.tardrwxr-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.txt1.txt2.txt3.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.gzdrwxr-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.gzdrwxr-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/hexuan1.txt2.txt3.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.txtdrwxr-xr-x. 2 hexuan hexuan 6 8月 23 21:46 Desktopdrwxr-xr-x. 2 hexuan hexuan 6 8月 23 21:46 Documentsdrwxr-xr-x. 2 hexuan hexuan 6 8月 23 21:46 Downloads-rw-rw-r--. 1 hexuan hexuan 52 8月 24 19:43 hello.txtdrwxrwxr-x. 3 hexuan hexuan 21 8月 23 13:16 itheimadrwxr-xr-x. 2 hexuan hexuan 6 8月 23 21:46 Musicdrwxr-xr-x. 2 hexuan hexuan 6 8月 23 21:46 Picturesdrwxr-xr-x. 2 hexuan hexuan 6 8月 23 21:46 Publicdrwxr-xr-x. 2 hexuan hexuan 6 8月 23 21:46 Templatesdrwxr-xr-x. 2 hexuan hexuan 6 8月 23 21:46 Videoslrwxrwxrwx. 1 hexuan hexuan 8 8月 25 20:30 yum -> /etc/yumdrwxr-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文件解压到指定文件内.

网站地图