Linux基础核心经典教程(016)——压缩和解压指令


版权声明

  • 本文原创作者:谷哥的小弟
  • 作者博客地址:http://blog.csdn.net/lfdfhl

概述

在本节教程中主要学习Linux中的压缩和解压指令。

gzip和gunzip

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

压缩文件,语法如下:

gzip 待压缩文件	

通过该命令将文件压缩成*.gz文件。请注意:使用 gzip 对文件进行压缩后,不再保留原文件。

解压缩文件,语法如下:

gunzip 待解压文件.gz

请注意:使用 gunzip 对文件进行解压缩后,不再保留原压缩文件。

示例如下:

  • 1、将/home下hello.txt文件压缩
  • 2、将 /home 下的hello.txt.gz 文件进行解压缩
[root@MyCentOS /]# cd /home
[root@MyCentOS home]# pwd
/home
[root@MyCentOS home]# gzip hello.txt
[root@MyCentOS home]# ls
hello.txt.gz  test1.txt  test2.txt
[root@MyCentOS home]# gunzip hello.txt.gz
[root@MyCentOS home]# ls
hello.txt  test1.txt  test2.txt
[root@MyCentOS home]# 

在这里插入图片描述

zip和unzip

zip 用于压缩文件, unzip 用于解压文件。

压缩文件,语法如下:

zip [选项] 压缩后的文件名.zip 待压缩内容

常用选项:

  • -r 递归压缩

解压文件,语法如下:

unzip [选项] 压缩文件名.zip 

常用选项:

  • -d 指定解压后文件存放目录

示例如下:

  • 1、将 /home下的所有文件进行压缩成mypackage.zip
  • 2、将 /home下的mypackge.zip解压到 /opt/temp目录下
[root@MyCentOS home]# pwd
/home
[root@MyCentOS home]# zip -r mypackage.zip /home/
  adding: home/ (stored 0%)
  adding: home/test1.txt (deflated 9%)
  adding: home/hello.txt (deflated 9%)
  adding: home/test2.txt (deflated 9%)
[root@MyCentOS home]# ls
hello.txt  mypackage.zip  test1.txt  test2.txt
[root@MyCentOS home]# unzip -d /opt/temp/ mypackage.zip
Archive:  mypackage.zip
   creating: /opt/temp/home/
  inflating: /opt/temp/home/test1.txt  
  inflating: /opt/temp/home/hello.txt  
  inflating: /opt/temp/home/test2.txt  
[root@MyCentOS home]# cd /opt/temp
[root@MyCentOS temp]# ls
home
[root@MyCentOS temp]# cd home
[root@MyCentOS home]# ls
hello.txt  test1.txt  test2.txt
[root@MyCentOS home]# 

在这里插入图片描述

tar

tar指令通常称为打包指令,该指令不但可压缩文件也可对压缩文件进行解压。

压缩文件,语法如下:

tar [选项] 压缩文件名.tar.gz 待打包文件或目录

解压文件,语法如下:

tar [选项] 压缩文件名.tar.gz 解压后的目录

请注意:再解压前先确保保存解压文件的目录已存在。

tar指令常用选项:

选项功能
-c产生.tar打包文件
-v显示详细信息
-f指定压缩后的文件名
-z打包同时压缩
-x解压.tar文件

压缩时常用选项:-zcvf
解压时常用选项:-zxvf

示例如下:

  • 1、将/home/test1.txt 和 /home/test2.txt 压缩成 test.tar.gz
  • 2、将/home文件夹压缩成myhome.tar.gz
  • 3、将test.tar.gz解压到当前目录
  • 4、将myhome.tar.gz 解压到/opt/ 目录下
[root@MyCentOS /]# cd /home
[root@MyCentOS home]# pwd
/home
[root@MyCentOS home]# tar -zcvf test.tar.gz test1.txt test2.txt
test1.txt
test2.txt
[root@MyCentOS home]# ls
hello.txt  mypackage.zip  test1.txt  test2.txt  test.tar.gz
[root@MyCentOS home]# tar -zcvf myhome.tar.gz /home/
tar: 从成员名中删除开头的“//home/
/home/test1.txt
/home/hello.txt
/home/test2.txt
/home/test.tar.gz
/home/mypackage.zip
[root@MyCentOS home]# ls
hello.txt  myhome.tar.gz  mypackage.zip  test1.txt  test2.txt  test.tar.gz
[root@MyCentOS home]# tar -zxvf test.tar.gz
test1.txt
test2.txt
[root@MyCentOS home]# ls
hello.txt  myhome.tar.gz  mypackage.zip  test1.txt  test2.txt  test.tar.gz
[root@MyCentOS home]# tar -zxvf myhome.tar.gz -C /opt/
home/
home/test1.txt
home/hello.txt
home/test2.txt
home/test.tar.gz
home/mypackage.zip
[root@MyCentOS home]# cd /opt
[root@MyCentOS opt]# ls
home  rh  temp
[root@MyCentOS opt]# cd home
[root@MyCentOS home]# ls
hello.txt  mypackage.zip  test1.txt  test2.txt  test.tar.gz
[root@MyCentOS home]# 
[root@MyCentOS home]# 
[root@MyCentOS home]# 
[root@MyCentOS home]# 
[root@MyCentOS home]# 
[root@MyCentOS home]# 

在这里插入图片描述
在这里插入图片描述

  • 6
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谷哥的小弟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值