一、gzip & gunzip
gzip 用于壓縮文件博杖;gunzip 用于解壓縮文件柠衅。
- 基本語法:
gzip 文件 : 壓縮文件,只能將文件壓縮為*.gz
gunzip 文件.gz: 解壓縮文件命令
備注: 壓縮或者解壓縮后述么,不會(huì)保留源文件蝌数。
[root@Hill wangqiang]# ls
cal.txt dog link2root test.txt
[root@Hill wangqiang]# gzip test.txt
[root@Hill wangqiang]# ls
cal.txt dog link2root test.txt.gz
[root@Hill wangqiang]# gunzip test.txt.gz
[root@Hill wangqiang]# ls
cal.txt dog link2root test.txt
[root@Hill wangqiang]#
二、 zip & unzip | [常用]
zip 用于壓縮文件度秘;unzip 用于解壓顶伞。
基本用法:
zip [選項(xiàng)] xxx.zip 將要壓縮的內(nèi)容。(功能描述:壓縮文件和目錄的命令)
unzip [選項(xiàng)] xxx.zip。 (功能描述:解壓縮文件)zip 常用選項(xiàng)
-r :遞歸壓縮唆貌,即壓縮目錄unzip 常用選項(xiàng)
-d <目錄>:指定解壓縮后文件的位置
-eg:
## 1. 壓縮
[root@Hill wangqiang]# zip -r mypackage.zip /home/wangqiang/
adding: home/wangqiang/ (stored 0%)
adding: home/wangqiang/dog/ (stored 0%)
adding: home/wangqiang/link2root/ (stored 0%)
adding: home/wangqiang/link2root/.bash_profile (deflated 21%)
adding: home/wangqiang/link2root/.oracle_jre_usage/ (stored 0%)
adding: home/wangqiang/link2root/.oracle_jre_usage/593c6814e38be419.timestamp (stored 0%)
adding: home/wangqiang/link2root/.ssh/ (stored 0%)
adding: home/wangqiang/link2root/.ssh/known_hosts (deflated 10%)
adding: home/wangqiang/link2root/.bashrc (deflated 23%)
adding: home/wangqiang/link2root/.bash_logout (stored 0%)
adding: home/wangqiang/link2root/.bash_history (deflated 67%)
adding: home/wangqiang/test.txt (deflated 30%)
adding: home/wangqiang/.bash_profile (deflated 21%)
adding: home/wangqiang/.bashrc (deflated 23%)
adding: home/wangqiang/.bash_logout (stored 0%)
adding: home/wangqiang/cal.txt (deflated 30%)
adding: home/wangqiang/.bash_history (deflated 46%)
[root@Hill wangqiang]# ls
cal.txt dog link2root mypackage.zip test.txt
## 解壓縮到其他文件夾
[root@Hill wangqiang]# unzip -d /opt/tmp mypackage.zip
Archive: mypackage.zip
creating: /opt/tmp/home/wangqiang/
creating: /opt/tmp/home/wangqiang/dog/
creating: /opt/tmp/home/wangqiang/link2root/
inflating: /opt/tmp/home/wangqiang/link2root/.bash_profile
creating: /opt/tmp/home/wangqiang/link2root/.oracle_jre_usage/
extracting: /opt/tmp/home/wangqiang/link2root/.oracle_jre_usage/593c6814e38be419.timestamp
creating: /opt/tmp/home/wangqiang/link2root/.ssh/
inflating: /opt/tmp/home/wangqiang/link2root/.ssh/known_hosts
inflating: /opt/tmp/home/wangqiang/link2root/.bashrc
extracting: /opt/tmp/home/wangqiang/link2root/.bash_logout
inflating: /opt/tmp/home/wangqiang/link2root/.bash_history
inflating: /opt/tmp/home/wangqiang/test.txt
inflating: /opt/tmp/home/wangqiang/.bash_profile
inflating: /opt/tmp/home/wangqiang/.bashrc
extracting: /opt/tmp/home/wangqiang/.bash_logout
inflating: /opt/tmp/home/wangqiang/cal.txt
inflating: /opt/tmp/home/wangqiang/.bash_history
[root@Hill wangqiang]# ls /opt/tmp/
home
三滑潘、tar 指令
- tar指令是打包指令,最后打包后的文件是.tar.gz的文件
- 基本語法
tar [選項(xiàng)] xxx.tar.gz 打包的類容 (功能描述:打包目錄锨咙,壓縮后的格式為.tar.gz) - 常用選項(xiàng)說明:
- -c : 產(chǎn)生.tar打包文件
- -v :顯示詳細(xì)信息
- -f :指定壓縮后的文件名
- -z:打包同時(shí)壓縮
- -x:解包.tar文件
案例說明:
- 案例1:
壓縮多個(gè)文件众羡,將/home/a1.txt和 /home/a2.txt 壓縮成a.tar.gz
[root@Hill ~]# cd /home/
[root@Hill home]# ls
dog hill user0 wangqiang zwj
[root@Hill home]# touch a1.txt a2.txt
[root@Hill home]# ls
a1.txt a2.txt dog hill user0 wangqiang zwj
[root@Hill home]# tar -zcvf a.tar.gz a1.txt a2.txt
a1.txt
a2.txt
[root@Hill home]# ls
a1.txt a2.txt a.tar.gz dog hill user0 wangqiang zwj
[root@Hill home]#
- 案例2:
將/home的文件夾壓縮成myhome.tar.gz
[root@Hill home]# tar -zcvf myhome.tar.gz /home/
[root@Hill home]# ls
a1.txt a2.txt a.tar.gz dog hill myhome.tar.gz user0 wangqiang zwj
[root@Hill home]#
- 案例3:
將a.tar.gz 解壓到當(dāng)前文件夾
[root@Hill home]# rm a1.txt a2.txt
[root@Hill home]# ls
a.tar.gz dog hill myhome.tar.gz user0 wangqiang zwj
[root@Hill home]# tar -zxvf a.tar.gz
a1.txt
a2.txt
[root@Hill home]# ls
a1.txt a2.txt a.tar.gz dog hill myhome.tar.gz user0 wangqiang zwj
[root@Hill home]#
- 案例4:
將myhome.tar.gz解壓到opt/tmp目錄下
[root@Hill home]# ls /opt/tmp/
[root@Hill home]# clear
[root@Hill home]# ls
a1.txt a2.txt a.tar.gz dog hill myhome.tar.gz user0 wangqiang zwj
[root@Hill home]# tar -zxvf myhome.tar.gz -C /opt/tmp/
[root@Hill home]# ls /opt/tmp/
home
[root@Hill home]#
備注: 指定解壓到某個(gè)目錄時(shí),目錄必須存在蓖租。否則會(huì)報(bào)錯(cuò)
[root@Hill home]# tar -zxvf myhome.tar.gz -C /opt/temmmmmmmmmmmmm
tar: /opt/temmmmmmmmmmmmm:無法 open: 沒有那個(gè)文件或目錄
tar: Error is not recoverable: exiting now