一、gzip和gunzip指令
1、gzip:用于压缩文件
语法:gzip 文件 (功能:压缩文件,只能将文件压缩为*.gz文件)
2、gunzip:用于解压文件
语法:gunzip 文件.gz (功能:解压缩文件)
示例代码:
//创建hello.txt文件
[root@slqEdu01 home]# touch hello.txt
[root@slqEdu01 home]# ls
hello.txt slq test zwj
//压缩hello.txt
[root@slqEdu01 home]# gzip hello.txt
[root@slqEdu01 home]# ls
hello.txt.gz slq test zwj
//解压缩
[root@slqEdu01 home]# gunzip hello.txt.gz
[root@slqEdu01 home]# ls
hello.txt slq test zwj
二、zip和unzip指令
1、zip:用于压缩文件或目录
语法:zip [选项] XXX.zip 将要压缩的内容 (功能:压缩文件和目录的命令)
zip常用选项: -r:递归压缩,即压缩目录
2、unzip:用于解压文件或目录
语法:unzip [选项] XXX.zip (功能:解压缩文件)
unzip常用选项:-d 目录:指定解压后文件的存放目录
示例代码:
//将home目录中的全部内容压缩到myhome.zip中
[root@slqEdu01 home]# zip -r myhome.zip /home
[root@slqEdu01 home]# ls
hello.txt myhome.zip slq test zwj
//将myhome.zip解压到/opt/tmp目录下
[root@slqEdu01 home]# cd /opt
[root@slqEdu01 opt]# mkdir tmp
[root@slqEdu01 opt]# ls
rh STM32F10xxx参考手册(中文).pdf tmp VMwareTools-10.3.23-17030940.tar.gz vmware-tools-distrib
[root@slqEdu01 opt]# unzip -d tmp /home/myhome.zip
[root@slqEdu01 opt]# ls
rh STM32F10xxx参考手册(中文).pdf tmp VMwareTools-10.3.23-17030940.tar.gz vmware-tools-distrib
[root@slqEdu01 opt]# cd tmp
[root@slqEdu01 tmp]# ls
home
三、tar指令
tar指令既是打包指令,也是解包指令,可以通过选项来确定
语法:tar [选项] XXX.tar.gz 打包的内容 (功能:打包文件或目录,压缩后的文件格式为*.tar.gz)
选项:
-c 产生.tar打包文件
-v 显示详细信息
-f 指定压缩后的文件名
-z 打包同时压缩
-x 解包.tar文件
示例代码:
//压缩多个文件,将/home/pig.txt和/home/cat.txt压缩成 pc.tar.gz
[root@slqEdu01 home]# touch pig.txt
[root@slqEdu01 home]# touch cat.txt
[root@slqEdu01 home]# ls
cat.txt hello.txt myhome.zip pig.txt slq test zwj
[root@slqEdu01 home]# tar -zcvf pc.tar.gz /home/pig.txt /home/cat.txt
tar: 从成员名中删除开头的“/”
/home/pig.txt
/home/cat.txt
[root@slqEdu01 home]# ls
cat.txt hello.txt myhome.zip pc.tar.gz pig.txt slq test zwj
//将/home的文件夹压缩成myhome.tar.gz
[root@slqEdu01 home]# tar -zcvf myhome.tar.gz /home
[root@slqEdu01 home]# ls
cat.txt hello.txt myhome.tar.gz myhome.zip pc.tar.gz pig.txt slq test zwj
//将pc.tar.gz解压到当前目录
[root@slqEdu01 home]# ls
cat.txt hello.txt myhome.tar.gz myhome.zip pc.tar.gz pig.txt slq test zwj
[root@slqEdu01 home]# rm -f cat.txt
[root@slqEdu01 home]# rm -f pig.txt
[root@slqEdu01 home]# rm -f hello.txt
[root@slqEdu01 home]# ls
myhome.tar.gz myhome.zip pc.tar.gz slq test zwj
[root@slqEdu01 home]# tar -zxvf pc.tar.gz
home/pig.txt
home/cat.txt
[root@slqEdu01 home]# ls
home myhome.tar.gz myhome.zip pc.tar.gz slq test zwj
[root@slqEdu01 home]# cd home
[root@slqEdu01 home]# ls
cat.txt pig.txt
//将myhome.tar.gz解压到/opt/tmp2目录下
[root@slqEdu01 opt]# mkdir /opt/tmp2
[root@slqEdu01 opt]# ls
rh STM32F10xxx参考手册(中文).pdf tmp tmp2 VMwareTools-10.3.23-17030940.tar.gz vmware-tools-distrib
[root@slqEdu01 home]# tar -zxvf /home/myhome.tar.gz -C /opt/tmp2
[root@slqEdu01 home]# cd /opt/tmp2
[root@slqEdu01 tmp2]# ls
home
[root@slqEdu01 tmp2]# cd home
[root@slqEdu01 home]# ls
cat.txt hello.txt myhome.tar.gz myhome.zip pc.tar.gz pig.txt slq test zwj