Linux壓縮和打包

一、壓縮

1、什麼是壓縮?

  壓縮是利用算法將文件有損或無損地處理,以達到保留最多文件信息,而令文件體積變小
  那麼是如何實現壓縮的呢?計算機處理的信息是以二進制的形式表示的,且使用bytes單位計量,但實際上計算機中最小的單位是bits,大家都知道1byte=8bits,那麼在記錄一個數字1的時候是如何存儲的呢?" 00000001 "實際上前面7位都是空的,但要滿足我們操作系統的存取方式,必須以8爲爲單位存儲,所以造成有一些空間並未填滿。壓縮就是將這些沒有使用到的空間丟出來,讓文件的佔用空間變小。

  常見壓縮解壓縮命令:

壓 縮 解壓縮 查看壓縮文件內容 壓縮文件後綴名
zip uzip zcat / zless .zip
gzip gunzip zcat / zless .gz
bzip2 bunzip2 bzcat / bzless .bz2
xz unxz xzcat / xzless .xz

2、常見壓縮命令

① zip命令

  • zip簡介
      zip是個使用廣泛的壓縮程序,文件經它壓縮後會另外產生具有".zip"擴展名的壓縮文件。
  • 命令語法
      zip [選項]… 壓縮包名 源文件或源目錄
  • 命令常用選項
選 項 解 釋
-r 遞歸處理,將指定目錄下的所有文件和子目錄一併壓縮
-m 將文件壓縮之後,刪除原始文件,即把文件移到壓縮文件中
-d 從壓縮文件內刪除指定文件
-v 顯示壓縮過程或版本信息
-q 不顯示壓縮過程
-u 更新較新的文件到壓縮文件中
-壓縮級別 壓縮級別是從 1~9 的數值,數值越小壓縮速度更快,數值越大壓縮效果更好
  • 實例

  Ⅰ、同時壓縮多個文件;

[root@localhost tmp]# zip test.zip boot.log maillog
  adding: boot.log (deflated 64%)
  adding: maillog (deflated 77%)
[root@localhost tmp]# ls -lh    //可以看出zip在壓縮多個文件的同時會將它們一併打包
total 12K
-rw-r--r--. 1 root root 1.3K Mar 30 19:46 boot.log
-rw-------. 1 root root  788 Mar 30 19:46 maillog
`-rw-r--r--. 1 root root  953 Mar 30 20:03 test.zip`

  Ⅱ、向壓縮文件test.zip中添加/var/log/secure文件;

[root@localhost tmp]# zip test.zip /var/log/secure
  adding: var/log/secure (deflated 86%)
[root@localhost tmp]# ls -lh
total 12K
-rw-r--r--. 1 root root 1.3K Mar 30 19:46 boot.log
-rw-------. 1 root root  788 Mar 30 19:46 maillog
`-rw-r--r--. 1 root root 1.6K Mar 30 20:14 test.zip`

  Ⅲ、從壓縮文件test.zip中添刪除/var/log/secure文件;

[root@localhost tmp]# zip -d test.zip /var/log/secure
deleting: var/log/secure
[root@localhost tmp]# ls -lh
total 12K
-rw-r--r--. 1 root root 1.3K Mar 30 19:46 boot.log
-rw-------. 1 root root  788 Mar 30 19:46 maillog
`-rw-r--r--. 1 root root  953 Mar 30 20:17 test.zip`

② unzip命令

  • unzip簡介
      unzip命令可以查看和解壓縮".zip"文件。
  • 命令語法
      unzip [選項]… 壓縮文件
  • 命令常用選項
選 項 解 釋
-d 目錄名 將壓縮文件解壓到指定目錄下
-n 解壓時並不覆蓋已經存在的文件
-o 解壓時覆蓋已經存在的文件,無需確認。
-l 列出壓縮文件內所包含的文件
-v 列出壓縮文件的詳細信息,包括壓縮文件中包含的文件大小、文件名以及壓縮比等
-t 檢測壓縮文件完整性
  • 實例

  Ⅰ、查看壓縮文件內所包含的文件;

[root@localhost tmp]# unzip -l test.zip    //-l選項
Archive:  test.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
     1283  03-30-2020 19:46   boot.log
      788  03-30-2020 19:46   maillog
---------                     -------
     2071                     2 files

[root@localhost tmp]# unzip -v test.zip    //-v選項
Archive:  test.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
    1283  Defl:N      462  64% 03-30-2020 19:46 4738bcbd  boot.log
     788  Defl:N      183  77% 03-30-2020 19:46 56a6c26f  maillog
--------          -------  ---                            -------
    2071              645  69%                            2 files
    

  Ⅱ、解壓壓縮文件至指定目錄;

[root@localhost tmp]# unzip -d testdir/ test.zip
Archive:  test.zip
  inflating: testdir/boot.log
  inflating: testdir/maillog
[root@localhost tmp]# cd testdir/
[root@localhost testdir]# ls
boot.log  maillog

③ gzip命令

  • gzip簡介
      gzip是Linux系統中經常使用的一個對文件進行壓縮和解壓的命令,只能用來壓縮文件(如果指定目錄,則會壓縮該目錄下的所有文件),文件經它壓縮後會產生具有".gz"擴展名的壓縮文件。
  • 命令語法
      gzip [選項]… 源文件
  • 命令常用選項
選 項 解 釋
-r 遞歸處理,指定目錄下的所有文件和子目錄分別壓縮
-c 將壓縮數據輸出到標準輸出,並保留源文件
-l 列出壓縮文件的相關信息(壓縮文件大小、未壓縮文件大小、壓縮比率、未壓縮文件名稱)
-v 顯示指令執行過程
-d 對".gz"文件進行解壓縮
-壓縮級別 壓縮級別是從 1~9 的數值,數值越小壓縮速度更快,數值越大壓縮效果更好,默認爲 6
  • 實例

  Ⅰ、壓縮文件;

[root@localhost tmp]# touch testfile    //創建測試文件
[root@localhost tmp]# ls
testfile
[root@localhost tmp]# gzip testfile    //使用gzip壓縮
[root@localhost tmp]# ls    //可以看出gzip在生成壓縮文件的同時會刪除源文件
testfile.gz

  Ⅱ、將壓縮數據輸出到標準輸出;

[root@localhost tmp]# touch testfile    //創建測試文件
[root@localhost tmp]# echo "123456" > testfile    //向文件中輸入數據
[root@localhost tmp]# gzip -c testfile    //將壓縮數據輸出到標準輸出
▒h▒^testfile342615▒▒%

  Ⅲ、壓縮並保留源文件;

[root@localhost tmp]# touch testfile    //創建測試文件
[root@localhost tmp]# gzip -c testfile > testfile.gz    //將壓縮數據重定向到壓縮文件中,這樣就可以實現在壓縮的同時不刪除源文件
[root@localhost tmp]# ls
testfile  testfile.gz

  Ⅳ、壓縮目錄;

[root@localhost tmp]# mkdir testdir    //創建測試目錄
[root@localhost tmp]# touch testdir/file1 testdir/file2 testdir/file3 testdir/file4    //創建測試文件
[root@localhost tmp]# ls
`testdir`
[root@localhost tmp]# ls testdir/
`file1  file2  file3  file4`
[root@localhost tmp]# gzip -r testdir/    //遞歸壓縮目錄
[root@localhost tmp]# ls    //可以看出,目錄依然存在且並未變成壓縮文件
`testdir`
[root@localhost tmp]# ls testdir/    //將目錄下的所有文件分別壓縮,且並未作打包處理
`file1.gz  file2.gz  file3.gz  file4.gz`

④ gunzip命令

  • gunzip簡介
      gunzip命令用來解壓縮".gz"文件。
  • 命令語法
      gunzip [選項]… 文件
  • 命令常用選項
選 項 解 釋
-r 遞歸處理,將指定目錄下的所有文件和子目錄分別解壓縮
-c 將解壓縮數據輸出到標準輸出,並保留源壓縮文件
-f 強制解壓縮文件
-l 列出壓縮文件內容
-v 顯示指令執行過程
-t 檢測壓縮文件完整性
  • 實例

  解壓縮文件;

[root@localhost tmp]# touch testfile    //創建測試文件
[root@localhost tmp]# gzip testfile    //壓縮
[root@localhost tmp]# ls
testfile.gz
[root@localhost tmp]# gunzip testfile    //解壓縮
[root@localhost tmp]# ls
testfile

⑤ bzip2命令

  • bzip2簡介
      bzip2與gzip類似,都只能用來壓縮文件,且壓縮任務完成後不會保留源文件,但".bz2"格式的壓縮比率相對較高,壓縮速度相對較慢,文件經它壓縮後會另外具有".bz2"擴展名的壓縮文件。
  • 命令語法
      bzip2 [選項]… 源文件
  • 命令常用選項
選 項 解 釋
-c 將壓縮數據輸出到標準輸出,並保留源文件
-f 執行壓縮或解壓縮時,若輸出文件與現有文件同名時,默認不會覆蓋現有文件,使用 -f 則會強制覆蓋現有文件
-k 執行壓縮或解壓縮任務後,保留源文件
-v 顯示指令執行過程
-d 對".bz2"文件進行解壓縮
-壓縮級別 壓縮級別是從 1~9 的數值,數值越小壓縮速度更快,數值越大壓縮效果更好 ,默認爲 6
-t 檢測壓縮文件完整性
  • 實例

  Ⅰ、壓縮並保留源文件;

`//方法一`
[root@localhost tmp]# touch testfile    //創建測試文件
[root@localhost tmp]# bzip2 -k testfile    //壓縮
[root@localhost tmp]# ls
testfile  testfile.bz2
`//方法二`
[root@localhost tmp]# bzip2 -c testfile > testfile1.bz2    //將壓縮數據重定向到壓縮文件中,這樣就可以實現在壓縮的同時不刪除源文件
[root@localhost tmp]# ls
testfile  testfile1.bz2  testfile.bz2

  Ⅱ、將壓縮數據輸出到標準輸出;

[root@localhost tmp]# bzip2 -c testfile
bzip2: I won't write compressed data to a terminal.    //什麼意思呢?我不會將壓縮數據寫入終端。
bzip2: For help, type: `bzip2 --help'.    //也許選項-c的意義就是使用重定向執行壓縮並保留源文件

⑥ bunzip2命令

  • bunzip2簡介
      bunzip2命令用來解壓縮".bz2"文件。
  • 命令語法
      bunzip2 [選項]… 壓縮文件
  • 命令常用選項
選 項 解 釋
-c 將解壓縮數據輸出到標準輸出,並保留源壓縮文件
-f 執行解壓縮時,若輸出文件與現有文件同名時,默認不會覆蓋現有文件,使用 -f 則會強制覆蓋現有文件
-k 執行解壓縮任務後,保留源壓縮文件
-v 顯示指令執行過程
-L 顯示版本信息
  • 實例

  Ⅰ、解壓縮文件;

[root@localhost tmp]# touch testfile    //創建測試文件
[root@localhost tmp]# bzip2 testfile    //壓縮
[root@localhost tmp]# ls
testfile.bz2
[root@localhost tmp]# bunzip2 testfile.bz2    //解壓縮
[root@localhost tmp]# ls
testfile

  Ⅱ、將解壓縮數據輸出到標準輸出;

[root@localhost tmp]# touch testfile    //創建測試文件
[root@localhost tmp]# echo "123456" > testfile    //向文件中輸入數據
[root@localhost tmp]# bzip2 testfile    //壓縮
[root@localhost tmp]# bunzip2 -c testfile.bz2
123456

⑦ xz命令

  • xz簡介
      xz是一個很少見到的,但絕大多數Linux默認自帶的一個壓縮工具,與gzip和bzip2同樣,都支持多文件壓縮,但三者而言xz的壓縮比率是最高的。
  • 命令語法
      xz [選項]… 源文件
  • 命令常用選項
選 項 解 釋
-c 將壓縮數據輸出到標準輸出,並保留源文件
-f 執行壓縮或解壓縮時,若輸出文件與現有文件同名時,默認不會覆蓋現有文件,使用 -f 則會強制覆蓋現有文件
-k 執行壓縮或解壓縮任務後,保留源文件
-v 顯示指令執行過程
-d 對".xz"文件進行解壓縮
-壓縮級別 壓縮級別是從 1~9 的數值,數值越小壓縮速度更快,數值越大壓縮效果更好,默認爲 6
-t 檢測壓縮文件完整性
  • 實例

  壓縮文件;

[root@localhost tmp]# touch testfile    //創建測試文件
[root@localhost tmp]# xz testfile    //壓縮
[root@localhost tmp]# ls
testfile.xz

⑧ unxz命令

  • unxz簡介
      unxz命令用來解壓縮".xz"文件。
  • 命令語法
      unxz [選項]… 壓縮文件
  • 命令常用選項
選 項 解 釋
-c 將解壓縮數據輸出到標準輸出,並保留源壓縮文件
-k 執行解壓縮任務後,保留源壓縮文件
-v 顯示指令執行過程
  • 實例

  解壓縮文件;

[root@localhost tmp]# touch testfile    //創建測試文件
[root@localhost tmp]# xz testfile    //壓縮
[root@localhost tmp]# ls
testfile.xz
[root@localhost tmp]# unxz testfile.xz    //解壓縮
[root@localhost tmp]# ls
testfile

二、打包(歸檔)

1、什麼是打包?

  打包(歸檔)是將多個文件和目錄集中存儲在一個文件中。歸檔文件沒有經過壓縮,因此,它佔用的空間是其中所有文件和目錄的總和。
  壓縮是爲了節省存儲空間,那麼打包的目的是什麼呢?那是因爲多數壓縮程序只能針對一個文件進行壓縮,如果需要壓縮一大堆文件時,就得先將這一大堆文件打成一個包,然後再進行壓縮。

2、tar命令

  • 命令解釋
      tar 命令可以把一大堆的文件和目錄打包成一個文件,同時也可以進行解打包。使用 tar 命令歸檔的包通常稱爲 tar 包(tar 包文件都是以“.tar”結尾的)。
  • 命令語法
      tar [選項…] [文件]…
  • 命令常用選項

  ① 打包解包選項

選 項 解 釋
-c 將多個文件或目錄進行打包操作
-x 對 tar 包做解打包操作
-A 追加 tar 文件到 tar 包
-r 追加文件至 tar 包結尾
-u 更新原 tar 包中的文件
-t 查看 tar 包中有哪些文件或目錄
-v 顯示打包文件過程
-o 文件解打包後到標準輸出
-N [date / file] 目錄 打包指定目錄中比date時間或者比file時間更新的文件
-f 包名 指定包的文件名,這是選項必須使用的並且位置必須在最後一個,後跟包名

  ② 壓縮 / 解壓縮選項

選 項 解 釋
-z 壓縮或解壓縮 “.tar.gz” 格式
-j 壓縮或解壓縮 ".tar.bz2"格式
-J 壓縮或解壓縮 ".tar.xz"格式
  • 實例

  Ⅰ、打包文件和目錄;

[root@localhost tmp]# touch testfile{1,2,3,4}    //創建測試文件
[root@localhost tmp]# mkdir testdir    //創建測試目錄
[root@localhost tmp]# tar -cvf test.tar testfile{1,2,3,4} testdir/    //打包爲test.tar文件
testfile1
testfile2
testfile3
testfile4
testdir/
[root@localhost tmp]# ls
testdir  testfile1  testfile2  testfile3  testfile4  test.tar

  Ⅱ、打包並使用gzip命令壓縮文件和目錄;

[root@localhost tmp]# touch testfile{1,2,3,4}    //創建測試文件
[root@localhost tmp]# mkdir testdir    //創建測試目錄
[root@localhost tmp]# tar -zcvf test.tar.gz testfile{1,2,3,4} testdir/    //打包並壓縮
testfile1
testfile2
testfile3
testfile4
testdir/
[root@localhost tmp]# ls -lh    //壓縮後的包只有177byte大小
total 20K
drwxr-xr-x. 2 root root 4.0K Apr  1 07:03 testdir
-rw-r--r--. 1 root root    0 Apr  1 07:03 testfile1
-rw-r--r--. 1 root root    0 Apr  1 07:03 testfile2
-rw-r--r--. 1 root root    0 Apr  1 07:03 testfile3
-rw-r--r--. 1 root root    0 Apr  1 07:03 testfile4
-rw-r--r--. 1 root root  10K Apr  1 07:12 test.tar
-rw-r--r--. 1 root root  177 Apr  1 07:11 test.tar.gz

  Ⅲ、解壓縮解打包test.tar.gz文件;

[root@localhost tmp]# rm -rf testfile* testdir    //刪除測試文件
[root@localhost tmp]# ls
test.tar  test.tar.gz
[root@localhost tmp]# tar -zxvf test.tar.gz    //解壓縮解打包,只需將選項-c換成-x即可
testfile1
testfile2
testfile3
testfile4
testdir/
[root@localhost tmp]# ls
testdir  testfile1  testfile2  testfile3  testfile4  test.tar  test.tar.gz

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章