012 压缩及解压缩命令

一、gzip & gunzip

gzip 用于压缩文件;gunzip 用于解压缩文件。

  • 基本语法:
    gzip 文件 : 压缩文件,只能将文件压缩为*.gz
    gunzip 文件.gz: 解压缩文件命令
    备注: 压缩或者解压缩后,不会保留源文件。
[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 [选项] xxx.zip 将要压缩的内容。(功能描述:压缩文件和目录的命令)
    unzip [选项] xxx.zip。 (功能描述:解压缩文件)

  • zip 常用选项
    -r :递归压缩,即压缩目录

  • unzip 常用选项
    -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 [选项] xxx.tar.gz 打包的类容 (功能描述:打包目录,压缩后的格式为.tar.gz)
  • 常用选项说明:
  1. -c : 产生.tar打包文件
  2. -v :显示详细信息
  3. -f :指定压缩后的文件名
  4. -z:打包同时压缩
  5. -x:解包.tar文件

案例说明:

  • 案例1:
    压缩多个文件,将/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 解压到当前文件夹
[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]#

备注: 指定解压到某个目录时,目录必须存在。否则会报错

[root@Hill home]# tar -zxvf myhome.tar.gz -C /opt/temmmmmmmmmmmmm
tar: /opt/temmmmmmmmmmmmm:无法 open: 没有那个文件或目录
tar: Error is not recoverable: exiting now

你可能感兴趣的:(012 压缩及解压缩命令)