Linux学习(七)压缩和解压类
前言
本文记录学习压缩和解压类,如有问题,请帮忙指出,谢谢。
提示:以下是本篇文章正文内容,下面案例可供参考
一、gzip/gunzip 压缩
基本语法
gzip 文件 (功能描述:压缩文件,只能将文件压缩为*.gz文件)
gunzip 文件.gz (功能描述:解压缩文件命令)
经验技巧
(1)只能压缩文件不能压缩目录
(2)不保留原来的文件
案例实操
gzip压缩
[root@hadoop1 ~]# ls
test.java
[root@hadoop1 ~]# gzip test.txt
[root@hadoop1 ~]# ls
test.txt.gz
gunzip解压缩文件
[root@hadoop1 ~]# gunzip test.txt.gz
[root@hadoop1 ~]# ls
test.txt
二、zip/unzip 压缩
基本语法
zip [选项] XXX.zip 将要压缩的内容 (功能描述:压缩文件和目录的命令)
unzip [选项] XXX.zip (功能描述:解压缩文件)
选项说明
zip
unzip
经验技巧
zip 压缩命令在window/linux都通用,可以压缩目录且保留源文件。
案例实操
压缩 1.txt 和2.txt,压缩后的名称为t.zip
[root@hadoop1 opt]# touch t2.txt
[root@hadoop1 opt]# zip t.zip test.txt t2.txt
adding: test.txt (stored 0%)
adding: t2.txt (stored 0%)
[root@hadoop1 opt]# ls
test.txt t2.txt t.zip
解压 t.zip
[root@hadoop101 ~]# unzip t.zip
Archive: t.zip
extracting: test.txt
extracting: t2.txt
[root@hadoop101 ~]# ls
test.txt t2.txt t.zip
解压t.zip到指定目录-d
[root@hadoop101 ~]# unzip t.zip -d /opt
[root@hadoop101 ~]# ls /opt/
三、tar 打包
基本语法
tar [选项] XXX.tar.gz 将要打包进去的内容 (功能描述:打包目录,压缩后的文件格式.tar.gz)
选项说明
案例实操
压缩多个文件
[root@hadoop101 opt]# tar -zcvf t.tar.gz test.txt t2.txt
test.txt
t2.txt
[root@hadoop101 opt]# ls
t.tar.gz test.txt t2.txt
压缩目录
[root@hadoop101 ~]# tar -zcvf tt.tar.gz tt/
tt/
tt/mingjie/
tt/dssz/
tt/dssz/test.txt
解压到当前目录
[root@hadoop101 ~]# tar -zxvf t.tar.gz
解压到指定目录
[root@hadoop101 ~]# tar -zxvf tt.tar.gz -C /opt
[root@hadoop101 ~]# ll /opt/
总结
在我工作中tar用的比较多,注意选项组合,分清压缩和解压。tar指定解压目录前要记得加-C。