【Linux】10.压缩和解压缩

gzip / gunzip

(1)基本语法

##将文件压缩为a.txt.gz 不保留原始文件a.txt
gzip a.txt
#解压a.txt.gz 得到文件a.txt 不保留压缩包a.txt.gz
gunzip a.txt.gz

(2)经验技巧

  • 只能压缩文件,不能压缩目录
  • 不保留原来的文件
  • 同时压缩多个文件会产生多个压缩包

(3)案例实操

压缩

[root@VM-24-12-centos home]# gzip a.txt b.txt
[root@VM-24-12-centos home]# ll
-rw-r--r-- 1 root       root         26 6月  10 16:51 a.txt.gz
-rw-r--r-- 1 root       root         26 6月  10 16:51 b.txt.gz

解压

[root@VM-24-12-centos home]# gunzip a.txt.gz b.txt.gz 
[root@VM-24-12-centos home]# ll
-rw-r--r-- 1 root       root          0 6月  10 16:51 a.txt
-rw-r--r-- 1 root       root          0 6月  10 16:51 b.txt

zip / unzip

(1)基本语法

zip [选项] xxx.zip 文件或路径
# -r:可选参数,用于递归内容,压缩目录时使用
unzip [选项] xxx.zip
# -d:可选参数,指定解压后文件的存放路径,不写则解压到当前路径

(2)经验技巧

  • zip压缩命令在window/linux通用
  • 会保留原始文件

(3)案例实操

压缩

[root@VM-24-12-centos home]# zip xxx.zip a.txt b.txt
  adding: a.txt (stored 0%)
  adding: b.txt (stored 0%)
[root@VM-24-12-centos home]# ll
-rw-r--r-- 1 root       root          0 6月  10 16:51 a.txt
-rw-r--r-- 1 root       root          0 6月  10 16:51 b.txt
-rw-r--r-- 1 root       root        298 6月  10 16:56 xxx.zip

解压

[root@VM-24-12-centos home]# unzip xxx.zip -d /home/aaa
Archive:  xxx.zip
 extracting: /home/aaa/a.txt         
 extracting: /home/aaa/b.txt         
[root@VM-24-12-centos home]# cd aaa
[root@VM-24-12-centos aaa]# ll
-rw-r--r-- 1 root root 0 6月  10 16:51 a.txt
-rw-r--r-- 1 root root 0 6月  10 16:51 b.txt

tar

(1)基本语法

tar [选项] xxx.tar.gz 文件或目录
# -c :打包
# -z :打包同时压缩
# -f :指定压缩后的文件名
# -x :解压
# -C :解压到指定目录,不指定解压目录时默认解压到当前文件夹
# -v :显示详细信息

(2)案例实操

压缩多个文件

[root@VM-24-12-centos home]# tar -czvf xxx.tar.gz a.txt b.txt
a.txt
b.txt
[root@VM-24-12-centos home]# ll
-rw-r--r-- 1 root       root          0 6月  10 17:24 a.txt
-rw-r--r-- 1 root       root          0 6月  10 17:24 b.txt
-rw-r--r-- 1 root       root        117 6月  10 17:26 xxx.tar.gz

压缩目录

[root@VM-24-12-centos home]# tar -czvf xxx.tar.gz aaa/
aaa/
aaa/b.txt
aaa/a.txt
[root@VM-24-12-centos home]# ll
drwxr-xr-x 2 root       root       4096 6月  10 16:59 aaa
-rw-r--r-- 1 root       root        146 6月  10 17:28 xxx.tar.gz

解压

[root@VM-24-12-centos home]# tar -zxvf xxx.tar.gz -C /home/bbb
aaa/
aaa/b.txt
aaa/a.txt
[root@VM-24-12-centos home]# ll
drwxr-xr-x 3 root       root       4096 6月  10 17:35 bbb
-rw-r--r-- 1 root       root        146 6月  10 17:28 xxx.tar.gz
posted @ 2022-06-10 20:46  YF721  阅读(42)  评论(0编辑  收藏  举报