Linux-壓縮

5.打包與壓縮

本章同步視頻:https://edu.51cto.com/sd/e4874

5.1 Linux 系統常見的壓縮指令

5.1.1常見後綴

n  *.Z         compress 程序壓縮的檔案;c#

n  *.zip       zip程序壓縮的檔案;bash

n  *.gzgzip程序壓縮的檔案;less

n  *.bz2       bzip2 程序壓縮的檔案;ide

n  *.xzxz程序壓縮的檔案;測試

n  *.tar       tar程序打包的數據,並無壓縮過;spa

n  *.tar.gz    tar 程序打包的檔案,其中而且通過gzip的壓縮orm

n  *.tar.bz2   tar 程序打包的檔案,其中而且通過 bzip2 的壓縮視頻

n  *.tar.xz    tar 程序打包的檔案,其中而且通過xz的壓縮遞歸

5.1.2  gzip, gunzip, zcat - compress or expand files

1.語法ip

[dmtsai@study ~]$ gzip [-cdtv#] 檔名

[dmtsai@study ~]$ zcat檔名.gz

選項與參數:

-c  :將壓縮的數據輸出到屏幕上,可透過數據流重導向來處理;

-d  :解壓縮的參數;

-t  :能夠用來檢驗一個壓縮文件的一致性~看看檔案有無錯誤;

-v  :能夠顯示出原檔案/壓縮文件案的壓縮比等信息;

-#  :# 爲數字的意思,表明壓縮等級,-1 最快,可是壓縮比最差、-9 最慢,可是壓縮比最好!預設是 -6

2.用法

(1)測試通常壓縮

[root@localhost dir]# ll

total 1020

-rw-r--r--. 1 root root 1042088 Mar 30 20:36 etc.conf

[root@localhost dir]# gzip etc.conf      #不帶任何選項的壓縮

[root@localhost dir]# ll

total 244

-rw-r--r--. 1 root root 247318 Mar 30 20:36 etc.conf.gz

(2)解壓縮

[root@localhost dir]# gzip -d etc.conf.gz     #解壓縮

[root@localhost dir]# ll

total 1020

-rw-r--r--. 1 root root 1042088 Mar 30 20:36 etc.conf

(3)顯示壓縮比

[root@localhost dir]# gzip -v etc.conf    #顯示壓縮比的壓縮

etc.conf:  76.3% -- replaced with etc.conf.gz

(4)保留源文件

[root@localhost dir]# gzip -c etc.conf >etc.conf.c   #保留源文件的壓縮

[root@localhost dir]# ll

total 1264

-rw-r--r--. 1 root root 1042088 Mar 30 20:36 etc.conf

-rw-r--r--. 1 root root  247318 Mar 30 20:47 etc.conf.c

(5)設定不一樣的壓縮比(壓縮比爲1-9)

[root@localhost dir]# gzip -9 -c etc.conf>etc.conf.9    #9級壓縮

[root@localhost dir]# gzip -1 -c etc.conf>etc.conf.1    #1級壓縮

[root@localhost dir]# ll    

total 1784

-rw-r--r--. 1 root root 1042088 Mar 30 20:36 etc.conf

-rw-r--r--. 1 root root  282311 Mar 30 20:48 etc.conf.1

-rw-r--r--. 1 root root  246914 Mar 30 20:48 etc.conf.9

-rw-r--r--. 1 root root  247318 Mar 30 20:47 etc.conf.c

(6)遞歸壓縮

[root@localhost dir]# mkdir dir     #建立一個子目錄

[root@localhost dir]# ll

total 1020

drwxr-xr-x. 2 root root       6 Mar 30 20:50 dir

-rw-r--r--. 1 root root 1042088 Mar 30 20:36 etc.conf

[root@localhost dir]# cp etc.conf dir/    #將etc.conf複製到子目錄

[root@localhost dir]# cd ..     #退回到父目錄

[root@localhost tmp]# gzip -c dir/* > dir.1     #壓縮外層dir目錄

gzip: dir/dir is a directory -- ignored     #顯示忽略子目錄內容

[root@localhost tmp]# gzip -c -r dir/>dir.2    #遞歸壓縮外層dir目錄

[root@localhost tmp]# ll

drwxrwxrwx. 3 root root      31 Mar 30 20:50 dir

-rw-r--r--. 1 root root  247318 Mar 30 20:52 dir.1

-rw-r--r--. 1 root root  494636 Mar 30 20:53 dir.2

#注意對比dir.1和dir.2的大小。

(7)查看壓縮內容

[root@localhost dir]# ll

-rw-r--r--. 1 root root 2003 Mar 30 21:05 passwd

[root@localhost dir]# gzip passwd

[root@localhost dir]# ll

-rw-r--r--. 1 root root 800 Mar 30 21:05 passwd.gz

[root@localhost dir]# zcat passwd.gz

[root@localhost dir]# zmore passwd.gz

[root@localhost dir]# zless passwd.gz

[root@localhost dir]# zgrep -n "calf" passwd.gz

39:calf:x:1000:1000:calf:/home/calf:/bin/bash

5.1.3 bzip2, bzcat/bzmore/bzless/bzgrep

1.語法

[dmtsai@study ~]$ bzip2 [-cdkzv#] 檔名

[dmtsai@study ~]$ bzcat檔名.bz2

選項與參數:

-c  :將壓縮的過程產生的數據輸出到屏幕上!

-d  :解壓縮的參數

-k  :保留源文件,而不會刪除原始的檔案喔!

-z  :壓縮的參數 (默認值,能夠不加)

-v  :能夠顯示出原檔案/壓縮文件案的壓縮比等信息;

-#  :與gzip一樣的,都是在計算壓縮比的參數, -9 最佳, -1 最快!

2.用法

(1)基本用法

[root@localhost dir]# ll

total 4

-rw-r--r--. 1 root root 2003 Mar 30 21:05 passwd

[root@localhost dir]# bzip2 passwd

[root@localhost dir]# ll

total 4

-rw-r--r--. 1 root root 837 Mar 30 21:05 passwd.bz2

(2)解壓縮

[root@localhost dir]# bzip2 -d passwd.bz2

[root@localhost dir]# ll

total 4

-rw-r--r--. 1 root root 2003 Mar 30 21:05 passwd

(3)顯示壓縮參數

[root@localhost dir]# bzip2 -v passwd

  passwd:   2.393:1,  3.343 bits/byte, 58.21% saved, 2003 in, 837 out.

(4)保留源文件

[root@localhost dir]# bzip2 -k passwd

[root@localhost dir]# ll

total 8

-rw-r--r--. 1 root root 2003 Mar 30 21:05 passwd

-rw-r--r--. 1 root root  837 Mar 30 21:05 passwd.bz2

(5)指定壓縮比

[root@localhost dir]# bzip2 -9 passwd

[root@localhost dir]# bzip2 -1 passwd

#注意:連續執行這兩條命令會報錯,bzip2: Output file passwd.bz2 already exists. 提示文件已經存在,須要解壓後再作第二步。或者執行下面兩條命令:

[root@localhost dir]# bzip2 -9 -c passwd>passwd.9.bz2

[root@localhost dir]# bzip2 -1 -c passwd>passwd.1.bz2

(6)查看壓縮內容

[root@localhost dir]# bzcat passwd.bz2

[root@localhost dir]# bzmore passwd.bz2

[root@localhost dir]# bzless passwd.bz2

[root@localhost dir]# bzgrep -n "calf" passwd.bz2

39:calf:x:1000:1000:calf:/home/calf:/bin/bash

5.1.4 xz,    xzcat,   - Compress or decompress   .xz  files

1.語法

[dmtsai@study ~]$ xz [-dtlkc#] 檔名

[dmtsai@study ~]$ xcat檔名.xz

選項與參數:

-d  :就是解壓縮啊!

-t  :測試壓縮文件的完整性,看有沒有錯誤

-l  :列出壓縮文件的相關信息

-k  :保留本來的檔案不刪除~

-c  :一樣的,就是將數據由屏幕上輸出的意思!

-#  :一樣的,也有較佳的壓縮比的意思!

2.用法

(1)通常用法

[root@localhost dir]# xz passwd

[root@localhost dir]# ll

total 4

-rw-r--r--. 1 root root 864 Mar 30 21:05 passwd.xz

#壓縮後源文件會消失

(2)解壓縮

[root@localhost dir]# xz -d passwd.xz

[root@localhost dir]# ll

total 4

-rw-r--r--. 1 root root 2003 Mar 30 21:05 passwd

(3)保留源文件

[root@localhost dir]# xz -k passwd

[root@localhost dir]# ll

total 8

-rw-r--r--. 1 root root 2003 Mar 30 21:05 passwd

-rw-r--r--. 1 root root  864 Mar 30 21:05 passwd.xz

(4)顯示壓縮參數

[root@localhost dir]# xz -l passwd.xz

Strms  Blocks   Compressed Uncompressed  Ratio  Check   Filename

    1       1        864 B      2,003 B  0.431  CRC64   passwd.xz

(5)指定壓縮比

[root@localhost dir]# xz -9 passwd

[root@localhost dir]# xz -1 passwd

#或者執行下面兩條命令

[root@localhost dir]# xz -9 -c passwd>passwd.9.xz

[root@localhost dir]# xz -1 -c passwd>passwd.1.xz

(6)查看壓縮內容

[root@localhost dir]# xzcat passwd.xz

[root@localhost dir]# xzmore passwd.xz

[root@localhost dir]# xzless passwd.xz

[root@localhost dir]# xzgrep -n "calf" passwd.xz

39:calf:x:1000:1000:calf:/home/calf:/bin/bash

本章同步視頻:https://edu.51cto.com/sd/e4874

相關文章
相關標籤/搜索