6.2 gzip壓縮工具

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查看的文件數值不一樣,但在屢次查看,文件大小會恢復正常。(跳轉數值較大比,是由於這個文件自己存在不少空隙,最後在壓縮並解壓後,會發現大小會有不一樣).net

  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平臺,最後一次的更改時間,壓縮的級別code

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)ip

gzip不能壓縮目錄

相關文章
相關標籤/搜索