本文使用

  • 为了要压缩
  • 常见压缩格式
  • 压缩工具
    • gzip压缩工具
    • bz2压缩工具
    • xz压缩工具

为什么要压缩

为什么要压缩?文件经过压缩后,其大小会缩小,可以节约服务器带宽资源、内存资源,节约运维成本!

常见压缩格式

Linux平台的常见压缩文件格式有以下几种

  • .zip
  • .gz
  • .bz2
  • .xz
  • .tar.gz
  • .tar.bz2
  • .tar.xz

虽然在Linux中文件后缀并不重要,但是为了便于区分和管理,压缩文件还是最好加上上述的一些后缀名。

压缩工具

gzip压缩工具

gzip不能压缩目录!!

  • 基本使用:压缩/解压缩
# 创建一个大文件
[root@localhost tmp]# find /etc/ -type f -exec cat {} >> 1.txt \;
[root@localhost tmp]# ls -lh
总用量 25M
-rw-r--r--. 1 root root 25M 11月 3 18:53 1.txt #压缩文件:gzip file
[root@localhost tmp]# gzip 1.txt
[root@localhost tmp]# ls -lh
总用量 9.3M
-rw-r--r--. 1 root root 9.3M 11月 3 18:53 1.txt.gz #解压缩文件: gzip -d file.gz
# gzip -d 等价于 gunzip
[root@localhost tmp]# gzip -d 1.txt.gz
[root@localhost tmp]# ls -lh
总用量 25M
-rw-r--r--. 1 root root 25M 11月 3 18:53 1.txt
  • 按指定压缩级别进行压缩
#以1级别进行压缩,默认压缩级别为9;
# 压缩级别1-9,压缩级别越高,占用CPU资源越高
# gzip -[1-9] file
[root@localhost tmp]# gzip -1 1.txt
[root@localhost tmp]# ls -lh
总用量 9.9M
-rw-r--r--. 1 root root 9.9M 11月 3 18:53 1.txt.gz
  • 在压缩的同时,保留原文件(默认不保留)
[root@localhost tmp]# gzip -c 1.txt > /test/tmp/1.txt.gz
[root@localhost tmp]# ls -lh
总用量 34M
-rw-r--r--. 1 root root 25M 11月 3 18:53 1.txt
-rw-r--r--. 1 root root 9.3M 11月 3 19:04 1.txt.gz
  • -c配合-d使用,将压缩文件解压缩的同时保留压缩包
# 注意最好使用绝对路径表示文件,清楚明白,当然相对路径表示也可以。
[[root@localhost tmp]# gzip -d -c /test/tmp/1.txt.gz > /test/tmp/2.txt [root@localhost tmp]# ls -l
总用量 34684
-rw-r--r--. 1 root root 9649736 11月 3 19:04 1.txt.gz
-rw-r--r--. 1 root root 25865478 11月 3 19:22 2.txt
  • zcat命令在不打开压缩包的情况下查看包内文件的内容
# 由于内容太多,这里只显示行数
[root@localhost tmp]# zcat 1.txt.gz | wc -l
176350

可以使用file命令查看压缩文件的具体信息!

[root@localhost tmp]# file 1.txt.gz
1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Fri Nov 3 18:53:55 2017, max speed

有些压缩文件被命名为不带压缩文件类型后缀的,这种文件无法解压,可以使用file来查看文件类型,然后修改后缀,更好区分!!

[root@centos7 test]# mv 1.txt.bz2 1.txt
[root@centos7 test]# file 1.txt
1.txt: bzip2 compressed data, block size = 900k
[root@centos7 test]# mv 1.txt 1.txt.bz2

bz2压缩工具

压缩率比gzip更高

未安装使用yum install -y bzip2

常用使用跟gzip命令类似,且同样不支持压缩目录

  • 基本用法
# 压缩前
[root@localhost tmp]# ls -lh
总用量 25M
-rw-r--r--. 1 root root 25M 11月 3 19:22 1.txt # bzip2压缩
[root@localhost tmp]# bzip2 1.txt
# 压缩后,比gzip压缩率高
[root@localhost tmp]# ls -lh
总用量 8.2M
-rw-r--r--. 1 root root 8.1M 11月 3 19:22 1.txt.bz2
# file命令查看文件格式
[root@localhost tmp]# file 1.txt.bz2
1.txt.bz2: bzip2 compressed data, block size = 900k # bzip2解压缩
[root@localhost tmp]# bzip2 -d 1.txt.bz2
[root@localhost tmp]# ls -lh
总用量 25M
-rw-r--r--. 1 root root 25M 11月 3 19:22 1.txt
  • -c参数在压缩同时保留原文件
[root@localhost tmp]# bzip2 -c 1.txt > 1.txt.bz2
[root@localhost tmp]# ls -lh
总用量 33M
-rw-r--r--. 1 root root 25M 11月 3 19:22 1.txt
-rw-r--r--. 1 root root 8.1M 11月 3 20:04 1.txt.bz2
  • -d -c参数:在解压同时保留原压缩文件
[root@localhost tmp]# bzip2 -d -c 1.txt.bz2 > 3.txt
[root@localhost tmp]# ls -l
总用量 58816
-rw-r--r--. 1 root root 25865478 11月 3 19:22 1.txt
-rw-r--r--. 1 root root 8491810 11月 3 20:04 1.txt.bz2
-rw-r--r--. 1 root root 25865478 11月 3 20:06 3.txt
  • 指定压缩级压缩(一般保存默认即可)
bzip2同样可以指定压缩级别:1-9,默认级别为6
[root@localhost tmp]# bzip2 -1 1.txt
[root@localhost tmp]# ls -lh
总用量 8.8M
-rw-r--r--. 1 root root 8.8M 11月 3 19:22 1.txt.bz2
  • 不解压情况下查看压缩文件内文件的内容
[root@localhost tmp]# bzcat 1.txt.bz2 | wc -l
176350

xz压缩工具

级别使用方法等同与上述的gzip和bzip2命令,同样也不能压缩目录!!

一般情况下,上述3种压缩方式:xz > bz2 > gzip

具体压缩要看文件内容,也不一定xz的压缩包最小

  • 基本使用
# 压缩
[root@localhost tmp]# xz 1.txt
[root@localhost tmp]# ls -lh
总用量 7.0M
-rw-r--r--. 1 root root 7.0M 11月 3 19:22 1.txt.xz # 解压缩 xz -d等价于unxz
[root@localhost tmp]# xz -d 1.txt.xz
[root@localhost tmp]# ls -lh
总用量 25M
-rw-r--r--. 1 root root 25M 11月 3 19:22 1.txt
  • 压缩文件同时保留原文件
[root@localhost tmp]# xz -c 1.txt > 1.txt.xz
[root@localhost tmp]# ls -lh
总用量 32M
-rw-r--r--. 1 root root 25M 11月 3 19:22 1.txt
-rw-r--r--. 1 root root 7.0M 11月 3 21:02 1.txt.xz
  • 解压同时保留压缩文件
[root@localhost tmp]# xz -d -c 1.txt.xz > ./2.txt
[root@localhost tmp]# ls -lh
总用量 57M
-rw-r--r--. 1 root root 25M 11月 3 19:22 1.txt
-rw-r--r--. 1 root root 7.0M 11月 3 21:02 1.txt.xz
-rw-r--r--. 1 root root 25M 11月 3 21:02 2.txt
  • 查看压缩文件内的文件内容(不解压前提下)
[root@localhost tmp]# xzcat 1.txt.xz | wc -l
176350
  • xz同样支持指导压缩级别的压缩
[root@localhost tmp]# xz -1 2.txt
[root@localhost tmp]# ls -lh
总用量 40M
-rw-r--r--. 1 root root 25M 11月 3 19:22 1.txt
-rw-r--r--. 1 root root 7.0M 11月 3 21:02 1.txt.xz
-rw-r--r--. 1 root root 7.5M 11月 3 21:02 2.txt.xz

转载于:https://my.oschina.net/LuCastiel/blog/1560663

压缩工具gzip、bzip2、xz的使用的更多相关文章

  1. gzip,bzip2,xz压缩工具

    gzip,bzip2,xz压缩工具====================== gzip压缩工具 示例:[root@aminglinux yasuo]# ls1.txt 2.txt 3.txt[roo ...

  2. centos 文档的压缩和打包 gzip,bzip2,xz,zip,unzip,tar,tgz 第九节课

    centos  文档的压缩和打包   gzip,bzip2,xz,zip,unzip,tar,tgz  第九节课 SAS盘可以支持热插拔,看机器 tar.zip.tar -czvf 不会动源文件,gz ...

  3. [CentOS7] gzip, bzip2, xz 压缩与解压缩

    声明:本文主要总结自:鸟哥的Linux私房菜-第八章.檔案與檔案系統的壓縮,打包與備份,如有侵权,请通知博主 gzip命令: 选项参数: -c :将压缩后的数据显示到屏幕上,可以用于重定向: -d : ...

  4. lesson - 8 课程笔记 tar / gzip /bzip2 / xz /

    作用:为linux的文件和目录创建档案,也可以在档案中改变文件,或者向档案中加入新的文件即用来压缩和解压文件.tar本身不具有压缩功能.他是调用压缩功能实现的  语法:tar[必要参数][选择参数][ ...

  5. 关于打包压缩几种格式(gzip,bzip2,xz)的试验对比

    要通过脚本进行备份,必然将会应用到压缩技术,这里简单针对几个常见的格式进行测验,从而得到一种合适的方式. 这里以一个应用目录做例子: [root@isj-test-5 mnt]$du -sh * 66 ...

  6. Linux之备份(tar)/解压与压缩(gzip,bzip2,xz)【待完善】

    [本博文,待完善] 以data原始文件为例,同tar备份,用xz压缩,实现备份->压缩整个过程的正向过程(生成.tar.xz)与其逆过程(先解压,后还原备份文件) 1.备份(tar) tar - ...

  7. Linux centosVMware 压缩打包介绍、gzip压缩工具、bzip2压缩工具、xz压缩工具。

    一.压缩打包介绍 Lnux下常见的压缩文件通常是.tar.gz模式,还有.tar..gz..bz2..zip..tar.bz2..tar.xz. .gz:表示由gzip压缩工具压缩的文件 .bz2:表 ...

  8. [拾 得] zip gzip bzip2 & tar 压缩/打包 四大金刚

    坚持知识分享,该文章由Alopex编著, 转载请注明源地址: http://www.cnblogs.com/alopex/    索引: 介绍压缩和打包 gzip bzip2 zip 的基本使用 gz ...

  9. 关于压缩软件gzip和xz的简单对照

    晚上因为处理磁盘报警的须要.进行了日志压缩,在此次压缩中分别使用了gzip和xz软件对文本进行了压缩.压缩的结果很令人诧异. 出于对xz好奇的原因是因为在下载内核源码时常常能够看到.xz格式的文件包. ...

随机推荐

  1. openlayers-统计图显示(中国区域高亮)

    openlayers版本: v3.19.1-dist 统计图效果:         案例下载地址:https://gitee.com/kawhileonardfans/openlayers-examp ...

  2. python 递归、匿名函数、

    1.递归:就是函数自己调用自己.(注:递归最多循环999) 2.匿名函数(意义:减少内存占用) lambada 定义一个匿名函数,eg:lambad x,b:x+b  (:前面是入参eg:x,b,:后 ...

  3. String 对象-->substr() 方法

    1.定义和用法 substr() 方法可在字符串中抽取从 开始 下标开始的指定数目的字符. 语法: string.substr(start,length) 参数: start:提取开始下标 lengt ...

  4. "段落"组件:<p> —— 快应用组件库H-UI

     <import name="p" src="../Common/ui/h-ui/text/c_p"></import> <te ...

  5. tf.nn.softmax 分类

    tf.nn.softmax(logits,axis=None,name=None,dim=None) 参数: logits:一个非空的Tensor.必须是下列类型之一:half, float32,fl ...

  6. 如果我选择IT行业,会不会在几年,或者几年后被社会给淘汰??

    IT互联网各行业薪资占比,你能拿到多少?随着移动互联网时代的发展,IT行业的需求量也越来越大,而且每年都会新增,当然也会有淘汰. 人生如此之短,都不喜欢自己虚度光阴,也不希望自己所努力的东西成为历史, ...

  7. phoenix 索引实践

    准备工作 创建测试表 CREATE TABLE my_table ( rowkey VARCHAR NOT NULL PRIMARY KEY, v1 VARCHAR, v2 VARCHAR, v3 V ...

  8. GeoGebra的一些指令名字

    列举出老师上课提出的一些命令 比较不常见的命令 1.取得函数上一点的坐标值x(A).y(A).z(A) 2.复数指令real() imaginary() 复数中的虚数应该使用Alt+i打出 点的表示指 ...

  9. 实例讲解Springboot以Repository方式整合Redis

    1 简介 Redis是高性能的NoSQL数据库,经常作为缓存流行于各大互联网架构中.本文将介绍如何在Springboot中整合Spring Data Redis,使用Repository的方式操作. ...

  10. [V&N2020 公开赛]TimeTravel 复现

    大佬友链(狗头):https://www.cnblogs.com/p201821440039/ 参考博客: https://www.zhaoj.in/read-6407.html https://cj ...