本文使用

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

为什么要压缩

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

常见压缩格式

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

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

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

压缩工具

gzip压缩工具

gzip不能压缩目录!!

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

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

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

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

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

bz2压缩工具

压缩率比gzip更高

未安装使用yum install -y bzip2

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

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

xz压缩工具

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

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

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

  • 基本使用
  1. # 压缩
  2. [root@localhost tmp]# xz 1.txt
  3. [root@localhost tmp]# ls -lh
  4. 总用量 7.0M
  5. -rw-r--r--. 1 root root 7.0M 11 3 19:22 1.txt.xz
  6. # 解压缩 xz -d等价于unxz
  7. [root@localhost tmp]# xz -d 1.txt.xz
  8. [root@localhost tmp]# ls -lh
  9. 总用量 25M
  10. -rw-r--r--. 1 root root 25M 11 3 19:22 1.txt
  • 压缩文件同时保留原文件
  1. [root@localhost tmp]# xz -c 1.txt > 1.txt.xz
  2. [root@localhost tmp]# ls -lh
  3. 总用量 32M
  4. -rw-r--r--. 1 root root 25M 11 3 19:22 1.txt
  5. -rw-r--r--. 1 root root 7.0M 11 3 21:02 1.txt.xz
  • 解压同时保留压缩文件
  1. [root@localhost tmp]# xz -d -c 1.txt.xz > ./2.txt
  2. [root@localhost tmp]# ls -lh
  3. 总用量 57M
  4. -rw-r--r--. 1 root root 25M 11 3 19:22 1.txt
  5. -rw-r--r--. 1 root root 7.0M 11 3 21:02 1.txt.xz
  6. -rw-r--r--. 1 root root 25M 11 3 21:02 2.txt
  • 查看压缩文件内的文件内容(不解压前提下)
  1. [root@localhost tmp]# xzcat 1.txt.xz | wc -l
  2. 176350
  • xz同样支持指导压缩级别的压缩
  1. [root@localhost tmp]# xz -1 2.txt
  2. [root@localhost tmp]# ls -lh
  3. 总用量 40M
  4. -rw-r--r--. 1 root root 25M 11 3 19:22 1.txt
  5. -rw-r--r--. 1 root root 7.0M 11 3 21:02 1.txt.xz
  6. -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. Spring Taco Cloud——Controller的创建(含SpringMVC执行过程&SpringBoot&Spring三者解释及关联)

    在记录这次控制器编写前,对于Spring的感觉就是经常提这样代码好简洁,这样好方便,这个是用来干嘛的诸如之类的话. What is Spring ?这是我想问自己的,一直认为是简化代码利于工程的开源框 ...

  2. .NET Core项目部署到Linux(Centos7)(七)启动和停止.NET Core项目

    目录 1.前言 2.环境和软件的准备 3.创建.NET Core API项目 4.VMware Workstation虚拟机及Centos 7安装 5.Centos 7安装.NET Core环境 6. ...

  3. MyBatis(十):Mybatis缓存的重要内容

    本文是按照狂神说的教学视频学习的笔记,强力推荐,教学深入浅出一遍就懂!b站搜索狂神说或点击下面链接 https://space.bilibili.com/95256449?spm_id_from=33 ...

  4. java web数据库的增删改查详细

    本次课上实验是完成数据库的增删改查. 包括增加用户信息.删除用户信息.多条件查找用户信息.修改用户信息(主要是复选框单选框等的相关操作.) 下面下看一下各个界面的样子. 总页面:显示全部页面:增加页面 ...

  5. flask-文件上传的使用

    flask-文件上传 在flask中使用request.files.get来获取文件对象 对获取到的文件对象可以使用save(filepath)方法来保存文件 上传的文件在保存前需要对文件名做一个过滤 ...

  6. 001-iOS开发前奏-C语言笔记

    001-iOS开发前奏-C语言笔记 学习目标 1.[了解]操作系统 2.[了解]应用软件 3.[了解]操作系统的分类和市场占有份额 4.[了解]iOS操作系统 5.[了解]应用软件开发的分类 6.[了 ...

  7. 【Tool】IDEA配置Maven依赖管理

    IDEA配置Maven 打开IDEA,在项目界面打开[File] — [Settings] 找到构建工具,下面第一个就是Maven 主选项更换我们自己的主目录和设置目录与本地仓库 勾选[打印异常捕获信 ...

  8. Ajax 简述与基础语法

    目录 Ajax 1. 原生 JS 实现 Ajax 2. 使用 Ajax 实现异步通信 a. Ajax 的基础语法 b. 用 Ajax 传递数据 i. 传递字符串数据 ii. 传递 JSON 数据 3. ...

  9. sql 系统表协助集合

    一.判断字段是否存在: select * from syscolumns where id=object_id('表') and name='字段'

  10. Cocos2d-x在win7下的android交叉编译环境

    cocos2d-x在win7下的Android交叉编译环境 2014年4月14日 cocos2d-x环境配置 前面把Visual Studio+Python开发环境配好了,但还没有讲如何在Androi ...