尚硅谷大数据技术之Linux (新)第7章 常用基本命令

7.8 压缩和解压类

7.8.1 gzip/gunzip 压缩

1.基本语法

gzip 文件 (功能描述:压缩文件,只能将文件压缩为*.gz文件)

gunzip 文件.gz (功能描述:解压缩文件命令)

2.经验技巧

(1)只能压缩文件不能压缩目录

(2)不保留原来的文件

3.案例实操

(1)gzip压缩

[root@hadoop101 ~]# ls

test.java

[root@hadoop101 ~]# gzip houge.txt

[root@hadoop101 ~]# ls

houge.txt.gz

(2)gunzip解压缩文件

[root@hadoop101 ~]# gunzip houge.txt.gz

[root@hadoop101 ~]# ls

houge.txt

7.8.2 zip/unzip 压缩

1.基本语法

zip  [选项] XXX.zip  将要压缩的内容 (功能描述:压缩文件和目录的命令)

unzip [选项] XXX.zip (功能描述:解压缩文件)

2.选项说明

表1-29

zip选项

功能

-r

压缩目录

表1-30

unzip选项

功能

-d<目录>

指定解压后文件的存放目录

3.经验技巧

zip 压缩命令在window/linux都通用,可以压缩目录且保留源文件。

4.案例实操

(1)压缩 1.txt 和2.txt,压缩后的名称为mypackage.zip

[root@hadoop101 opt]# touch bailongma.txt

[root@hadoop101 ~]# zip houma.zip houge.txt bailongma.txt

  adding: houge.txt (stored 0%)

  adding: bailongma.txt (stored 0%)

[root@hadoop101 opt]# ls

houge.txt bailongma.txt houma.zip

(2)解压 mypackage.zip

[root@hadoop101 ~]# unzip houma.zip

Archive:  houma.zip

 extracting: houge.txt               

 extracting: bailongma.txt       

[root@hadoop101 ~]# ls

houge.txt bailongma.txt houma.zip

(3)解压mypackage.zip到指定目录-d

[root@hadoop101 ~]# unzip houma.zip -d /opt

[root@hadoop101 ~]# ls /opt/

7.8.3 tar 打包

1.基本语法

tar  [选项]  XXX.tar.gz  将要打包进去的内容 (功能描述:打包目录,压缩后的文件格式.tar.gz)

2.选项说明

表1-31

选项

功能

-c

产生.tar打包文件

-v

显示详细信息

-f

指定压缩后的文件名

-z

打包同时压缩

-x

解包.tar文件

3.案例实操

(1)压缩多个文件

[root@hadoop101 opt]# tar -zcvf houma.tar.gz houge.txt bailongma.txt

houge.txt

bailongma.txt

[root@hadoop101 opt]# ls

houma.tar.gz houge.txt bailongma.txt

(2)压缩目录

[root@hadoop101 ~]# tar -zcvf xiyou.tar.gz xiyou/

xiyou/

xiyou/mingjie/

xiyou/dssz/

xiyou/dssz/houge.txt

(3)解压到当前目录

[root@hadoop101 ~]# tar -zxvf houma.tar.gz

(4)解压到指定目录

[root@hadoop101 ~]# tar -zxvf xiyou.tar.gz -C /opt

[root@hadoop101 ~]# ll /opt/