首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >6.2 gzip压缩工具

6.2 gzip压缩工具

作者头像
运维小白
发布2018-02-06 10:49:56
1K0
发布2018-02-06 10:49:56
举报
文章被收录于专栏:运维小白运维小白运维小白

gzip目录概要

  • gzip 1.txt 压缩1.txt文件
  • gzip -d 1.txt 或者 uzip 1.txt.gz 解压1.txt文件的两种方法
  • gzip -# 1.txt //范围1-9,默认为6
  • 不能压缩目录
  • zcat 1.txt.gz
  • gzip -c 1.txt > /root/1.txt.gz 压缩文件,并指定目录
  • gunzip -c /root/1.txt.gz > /tmp/1.txt.new 解压文件,并指定

gzip压缩文件

  • gzip 1.txt 压缩1.txt文件
  1. 首先切换到/tmp目录下,并新建一个目录d6z [root@localhost ~]# cd /tmp/ [root@localhost tmp]# ls 11.sh han.111 23.sh fstab [root@localhost tmp]# mkdir d6z
  2. 切换到/d6z目录下,并查找/etc目录下所有以conf结尾的文件 [root@localhost tmp]# cd d6z [root@localhost d6z]# find /etc/ -type f -name "*conf" 查找所有文件中,名字以conf结尾的文件 /etc/resolv.conf /etc/pki/ca-trust/ca-legacy.conf /etc/yum/pluginconf.d/fastestmirror.conf /etc/yum/pluginconf.d/langpacks.conf /etc/yum/protected.d/systemd.conf /etc/yum/version-groups.conf /etc/rdma/mlx4.conf /etc/rdma/rdma.conf
  3. 然后这文件查找出的文件内容追加到1.txt中,这个符号 {} 表示列出来的文件
    • 多追加几次文件内容到1.txt中

    [root@localhost d6z]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \; [root@localhost d6z]#

  4. 查看文件和大小 [root@localhost d6z]# ls 1.txt [root@localhost d6z]# du -sh 1.txt 3.2M 1.txt

这里多次追加会看到文件,du -sh 1.txt查看的文件数值不同,但在多次查看,文件大小会恢复正常。(跳转数值较大比,是因为这个文件本身存在很多空隙,最后在压缩并解压后,会发现大小会有不同)

  1. 压缩文件1.txt [root@localhost d6z]# gzip 1.txt [root@localhost d6z]# ls 1.txt.gz 会看到源文件消失了,变成了.gz的压缩文件 1.查看压缩文件大小 [root@localhost d6z]# du -sh 1.txt.gz 332K 1.txt.gz

gzip解压文件(两种方法)

  • gzip -d 1.txt.gz 把1.txt文件解压出来
[root[@localhost](https://my.oschina.net/u/570656) d6z]# gzip -d 1.txt.gz
[root[@localhost](https://my.oschina.net/u/570656) d6z]# ls
1.txt
[root[@localhost](https://my.oschina.net/u/570656) d6z]# du -sh 1.txt
1.3M	1.txt
  • gunzip 1.txt.gz 解压1.txt文件
[root@localhost d6z]# gunzip 1.txt.gz
[root@localhost d6z]# ls
1.txt
[root@localhost d6z]# du -sh 1.txt
1.3M	1.txt

gzip指定压缩的级别

  • gzip压缩的级别范围有1-9,默认是 6 级别,也可以指定压缩级别
    • 9级别是压缩的最严谨,所耗费的CPU资源也最大(压缩的文件也是最小的)
    • 压缩到一定级别后,到达极限后,会压缩不了

file命令,查看压缩后的文件

  • 压缩后的文件变成了二进制文件,不能直接使用cat查看
  • file命令,查看压缩的文件
    • 格式 file 1.txt.gz
[root@localhost d6z]# file 1.txt.gz
1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Thu Nov  9 14:23:33 2017, max compression

这里会看到这是一个gzip的压缩数据,名称是1.txt,基于Unix平台,最后一次的更改时间,压缩的级别

zcat命令,查看压缩文件的内容

  • zcat命令,查看压缩文件的内容
    • 格式 zcat 1.txt.gz
      • 这是先解压,后cat查看的

gzip压缩文件,并指定目录

  • gzip -c 1.txt > /tmp/1.txt.gz 压缩文件,并重定向目录和名称
[root@localhost d6z]# gzip -c 1.txt > /tmp/1.txt.gz
[root@localhost d6z]# ls /tmp/1.txt.gz
/tmp/1.txt.gz

gzip解压文件,并指定目录

  • gunzip -c /tmp/1.txt.gz > /tmp/6dz/2.txt
  • gzip -d -c /tmp/1.txt.gz > /tmp/6dz/2.txt
[root@localhost d6z]# gunzip -c /tmp/1.txt.gz >/tmp/d6z/2.txt
[root@localhost d6z]# ls
1.txt  2.txt
[root@localhost d6z]# gzip -c -d /tmp/1.txt.gz >/tmp/d6z/2.txt
[root@localhost d6z]# ls
1.txt  2.txt

1.txt和2.txt这两个文件大小相同(du -sh 1.txt 2.txt),行数形同(wc -l 1.txt 2.txt)

gzip不能压缩目录

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • gzip目录概要
  • gzip压缩文件
  • gzip解压文件(两种方法)
  • gzip指定压缩的级别
  • file命令,查看压缩后的文件
  • zcat命令,查看压缩文件的内容
  • gzip压缩文件,并指定目录
  • gzip解压文件,并指定目录
  • gzip不能压缩目录
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档