前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux命令使用:gzip、bzip2与zip

Linux命令使用:gzip、bzip2与zip

作者头像
zhipingChen
发布2018-12-14 16:17:28
2K0
发布2018-12-14 16:17:28
举报
文章被收录于专栏:编程理解编程理解

文件的压缩/解压操作在日常使用中很常见,压缩后的文件可以节省存储空间,又方便进行网络传输。

下面列举一些常用的与文件压缩有关的命令:

1. gzip <file>

【1】压缩单个文件并删除源文件

代码语言:javascript
复制
tmpuser:test/ $ ls
a
tmpuser:test/ $ gzip a
tmpuser:test/ $ ls
a.gz

由该示例可知,gzip命令的使用方式很简单,命令后直接跟输入文件即可,gzip命令压缩后默认会覆盖源文件,生成以.gz为后缀的文件。

命令加-k参数,表示keep保留源文件

【2】压缩单个文件并保留源文件:

代码语言:javascript
复制
tmpuser:test/ $ ls
a
tmpuser:test/ $ gzip -k a
tmpuser:test/ $ ls
a  a.gz

【3】压缩目录

代码语言:javascript
复制
tmpuser:test/ $ ls directory
a  b
tmpuser:test/ $ gzip -r directory
tmpuser:test/ $ ls directory
a.gz  b.gz

命令中的-r参数表示递归压缩目录下每一个文件的作用,gzip命令只能压缩单个文件,即使压缩目录,也只是压缩目录下的每一个文件。

这里讲gzip只能压缩单个文件,并不是一次只能压缩一个文件,而是压缩的单位是单个文件,即并不能将多个文件压缩成为一个文件。

【4】压缩多个文件

代码语言:javascript
复制
tmpuser:test/ $ ls
a  b  directory
tmpuser:test/ $ ls directory
c  d
tmpuser:test/ $ gzip -r a b directory
tmpuser:test/ $ ls
a.gz  b.gz  directory
tmpuser:test/ $ ls directory
c.gz  d.gz

【5】解压缩单个文件

代码语言:javascript
复制
tmpuser:test/ $ ls
a.gz  b.gz  directory
tmpuser:test/ $ gzip -d a.gz
tmpuser:test/ $ ls
a  b.gz  directory
tmpuser:test/ $ gunzip b.gz
tmpuser:test/ $ ls
a  b  directory

解压缩有两种方式,可以使用gzip -d或者gunzip完成解压缩操作。

同理,可以使用-k参数保留源文件

【6】解压缩并保留源文件

代码语言:javascript
复制
tmpuser:test/ $ ls
a.gz  b  directory
tmpuser:test/ $ gzip -dk a.gz
tmpuser:test/ $ ls
a  a.gz  b  directory

【7】解压缩目录

代码语言:javascript
复制
tmpuser:test/ $ ls directory
c.gz  d.gz
tmpuser:test/ $ gzip -dr directory
tmpuser:test/ $ ls directory
c  d
2. bzip2 <file>

相对于gzipbzip2是一个压缩效率更高的命令,压缩后文件占据的空间更小,所以需要的压缩时间要比gzip更久,bzip2的使用方式与gzip基本相同。

【1】文件压缩与解压

代码语言:javascript
复制
tmpuser:test/ $ ls
a  b  directory
tmpuser:test/ $ bzip2 a
tmpuser:test/ $ ls
a.bz2  b  directory
tmpuser:test/ $ bzip2 -dk a.bz2
tmpuser:test/ $ ls
a  a.bz2  b  directory

由示例可知,bzip2命令的压缩和解压方式与gzip相同,且同样通过-k参数保留源文件。压缩后生成以.bz2为后缀的文件。

【2】目录压缩与解压

代码语言:javascript
复制
tmpuser:test/ $ ls directory
c  d
tmpuser:test/ $ bzip2 directory/*
tmpuser:test/ $ ls directory
c.bz2  d.bz2
tmpuser:test/ $ bzip2 -dk directory/*
tmpuser:test/ $ ls directory
c  c.bz2  d  d.bz2

bzip2命令对目录的压缩同样是压缩目录下每一个文件,不过bzip2命令并没有提供-r参数,所以无法递归的对目录下文件进行压缩与解压操作。

3. zip <file.zip> <file>

zip命令的压缩率要低于bzip2gzip,不过使用较为广泛,且兼容性较好。

【1】文件与目录压缩

代码语言:javascript
复制
tmpuser:test/ $ ls
a  b  directory
tmpuser:test/ $ ls directory
c  d
tmpuser:test/ $ zip -r test.zip a b directory
  adding: a (deflated 91%)
  adding: b (deflated 96%)
  adding: directory/ (stored 0%)
  adding: directory/c (stored 0%)
  adding: directory/d (stored 0%)
tmpuser:test/ $ ls
a  b  directory  test.zip

由示例可知,zip命令生成以.zip为后缀的压缩文件,使用-r参数完成对目录的递归压缩,且默认情况下不删除源文件。zip命令提供有-m参数,用于删除源文件,-m表示move移动源文件到压缩包中的意思。

【2】压缩文件查看

代码语言:javascript
复制
tmpuser:test/ $ unzip -l test.zip
Archive:  test.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       79  2018-11-21 23:28   a
      159  2018-11-21 23:28   b
        0  2018-11-22 00:16   directory/
        0  2018-11-21 23:27   directory/c
        0  2018-11-21 23:27   directory/d
---------                     -------
      238                     5 files

由示例可知,.zip格式压缩文件的解压使用unzip命令,使用-l参数查看压缩包内文件信息。

有些文件的左侧Length属性为空,是因为该文件为空文件,并没有添加具体内容

【3】解压缩文件

代码语言:javascript
复制
tmpuser:test/ $ ls
test.zip
tmpuser:test/ $ unzip test.zip
Archive:  test.zip
  inflating: a
  inflating: b
   creating: directory/
 extracting: directory/c
 extracting: directory/d
tmpuser:test/ $ ls
a  b  directory  test.zip
tmpuser:test/ $ ls directory
c  d

由示例可知,与压缩操作相同,解压缩操作同样不删除源文件,所以若目录下存在同名文件时,会出现是否更新文件的提示: 【4】解压缩文件并根据提示覆盖同名文件

代码语言:javascript
复制
tmpuser:test/ $ ls
a  b  directory  test.zip
tmpuser:test/ $ ls directory
c  d
tmpuser:test/ $ unzip test.zip
Archive:  test.zip
replace a? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
  inflating: a
  inflating: b
 extracting: directory/c
 extracting: directory/d

unzip提供有忽略提示,直接更新同名文件的参数。

【5】解压缩文件并覆盖同名文件

代码语言:javascript
复制
tmpuser:test/ $ ls
a  b  directory  test.zip
tmpuser:test/ $ ls directory
c  d
tmpuser:test/ $ unzip -o test.zip
Archive:  test.zip
  inflating: a
  inflating: b
 extracting: directory/c
 extracting: directory/d

由示例可知,使用-o参数忽略提示,直接更新同名文件,-o表示overwrite覆盖同名文件的意思。unzip同时提供-n参数,忽略提示并不更新同名文件,-n表示never覆盖同名文件的意思。

【6】解压缩文件到指定路径

代码语言:javascript
复制
tmpuser:test/ $ mkdir tmp
tmpuser:test/ $ ls
a  b  directory  test.zip  tmp
tmpuser:test/ $ unzip test.zip -d tmp
Archive:  test.zip
  inflating: tmp/a
  inflating: tmp/b
   creating: tmp/directory/
 extracting: tmp/directory/c
 extracting: tmp/directory/d
tmpuser:test/ $ ls tmp
a  b  directory

由示例可知,使用-d参数可以指定压缩包的解压路径,其中-d表示指定directory意思。

【7】压缩时排除指定文件

代码语言:javascript
复制
tmpuser:test/ $ ls directory
c  d
tmpuser:test/ $ zip -r test.zip directory -x directory/c
  adding: directory/ (stored 0%)
  adding: directory/d (stored 0%)
tmpuser:test/ $ unzip -l test.zip
Archive:  test.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2018-11-22 00:55   directory/
        0  2018-11-21 23:27   directory/d
---------                     -------
        0                     2 files

由示例可知,使用-x参数可以在压缩时排除目录下的指定文件,其中-x表示exclude意思。

【8】添加文件到压缩包中

代码语言:javascript
复制
tmpuser:test/ $ unzip -l test.zip
Archive:  test.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2018-11-22 00:55   directory/
        0  2018-11-21 23:27   directory/d
---------                     -------
        0                     2 files
tmpuser:test/ $ zip -u test.zip directory/c
  adding: directory/c (stored 0%)
tmpuser:test/ $ unzip -l test.zip
Archive:  test.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2018-11-22 00:55   directory/
        0  2018-11-21 23:27   directory/d
        0  2018-11-21 23:27   directory/c
---------                     -------
        0                     3 files

由示例可知,使用-u参数可以添加文件到压缩包中,其中-u表示update意思。

【9】从压缩包中删除文件

代码语言:javascript
复制
tmpuser:test/ $ unzip -l test.zip
Archive:  test.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2018-11-22 00:55   directory/
        0  2018-11-21 23:27   directory/d
        0  2018-11-21 23:27   directory/c
---------                     -------
        0                     3 files
tmpuser:test/ $ zip -d test.zip directory/c
deleting: directory/c
tmpuser:test/ $ unzip -l test.zip
Archive:  test.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2018-11-22 00:55   directory/
        0  2018-11-21 23:27   directory/d
---------                     -------
        0                     2 files

由示例可知,使用-d参数可以从压缩包中删除指定文件,其中-d表示delete意思。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.11.22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. gzip <file>
  • 2. bzip2 <file>
  • 3. zip <file.zip> <file>
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档