1. 程式人生 > >2018-05-25筆記(文件的壓縮和打包)

2018-05-25筆記(文件的壓縮和打包)

linux

第六章文件的壓縮和打包

6.1 壓縮打包介紹

Linux環境常見壓縮文件類型:.zip,.gz,.bz2,.xz,.tar.gz,.tar.bz2,.tar.xz等,linux系統中的後綴名其實要不要無所謂,但是對於壓縮文件來講必須要帶上。這是為了判斷壓縮文件是由哪種壓縮工具所壓縮,而後才能去正確的解壓縮這個文件。壓縮打包的目的是為了方便文件傳輸,節省磁盤空間,減少傳輸花費的時間,節省帶寬等
.gz gzip 壓縮工具壓縮的文件
.bz2 bzip2 壓縮工具壓縮的文件
.tar tar 打包程序打包的文件(tar並沒有壓縮功能,只是把一個目錄合並成一個文件)
.tar.gz 可以理解為先用tar打包,然後再gzip壓縮


.tar.bz2 同上,先用tar打包,然後再bzip2壓縮

6.2 gzip壓縮工具

gzip是GNUzip的縮寫,它是一個GNU自由軟件的文件壓縮程序,用於UNIX系統的文件壓縮。我們在Linux中經常會用到後綴為.gz的文件,它們就是gzip格式的,註意:gzip不能用來壓縮目錄
語法: gzip [-d#] filename 其中#為1-9的數字
選項:
-d:解壓縮(=gunzip)
-#:指定壓縮等級,此處#表示1~9數字,9壓縮最好,默認為6(壓縮等級越高,CPU消耗越高)
-c:指定壓縮或解壓後的路徑

[root@localhost ~]# dd if=/dev/zero of=/tmp/2.txt bs=1M count=10     #用dd命創建一個10M的文件2.txt
記錄了10+0 的讀入
記錄了10+0 的寫出
10485760字節(10 MB)已復制,0.0111434 秒,941 MB/秒
[root@localhost ~]# du -sh /tmp/2.txt
10M /tmp/2.txt
[root@localhost ~]# cd /tmp
[root@localhost tmp]# gzip 2.txt                                                           #直接用gzip壓縮,源文件消失,變成2.txt.gz
[root@localhost tmp]# du -sh 2.txt.gz                                                  #可以看到2.txt已經被壓縮成12K
12K 2.txt.gz
[root@localhost tmp]# file 2.txt.gz                                                       #用file查看文件的類型是gzip壓縮文件
2.txt.gz: gzip compressed data, was "2.txt", from Unix, last modified: Thu May 24 11:50:02 2018
[root@localhost tmp]# gzip -d 2.txt.gz                                                 #-d選項是解壓用的,壓縮文件消失,變成2.txt
[root@localhost tmp]# ll
總用量 10252
drwxr-xr-x  2 root    root        4096 5月  22 09:38 123
-rw-r--r--  1 tcpdump tcpdump       68 5月  22 13:52 123.log
-rw-r--r--  1 root    root    10485760 5月  24 11:50 2.txt

-c指定壓縮後的路徑,源文件不消失

[root@localhost tmp]# gzip -c 2.txt > /tmp/2.txt.gz
[root@localhost tmp]# ll                                             #這裏可以看到原來的2.txt還在
總用量 10264
drwxr-xr-x  2 root    root        4096 5月  22 09:38 123
-rw-r--r--  1 tcpdump tcpdump       68 5月  22 13:52 123.log
-rw-r--r--  1 root    root    10485760 5月  24 11:50 2.txt
-rw-r--r--  1 root    root       10214 5月  24 11:57 2.txt.gz

查看壓縮包內容zcat

[root@localhost tmp]# zcat 2.txt.gz      #可查看壓縮包的內部內容

解壓除了-d選項外,還可以使用gunzip,也可以加上-c指定解壓後的路徑

[root@localhost tmp]# gunzip 2.txt.gz             
gzip: 2.txt already exists; do you wish to overwrite (y or n)? y     #因為之前-c選項2.txt已經存在,會提示是否覆蓋
[root@localhost tmp]# ll
總用量 10252
drwxr-xr-x  2 root    root        4096 5月  22 09:38 123
-rw-r--r--  1 tcpdump tcpdump       68 5月  22 13:52 123.log
-rw-r--r--  1 root    root    10485760 5月  24 11:57 2.txt

6.3 bzip2 壓縮工具

bzip2 是一個基於Burrows-Wheeler 變換的無損壓縮軟件,壓縮效果比傳統的LZ77/LZ78壓縮算法來得好。它是一款免費軟件。可以自由分發免費使用。它廣泛存在於UNIX&LINUX的許多發行版本中。bzip2能夠進行高質量的數據壓縮。它利用先進的壓縮技術,能夠把普通的數據文件壓縮10%至15%,壓縮的速度和解壓的效率都非常高!支持大多數壓縮格式,包括tar、gzip 等等
註意: bzip2也不可以壓縮目錄
語法: bzip2 [options] [filename]
選項:
-d:解壓縮
-z:壓縮(=bzip2,所以可以不帶該參數直接使用)
-c:指定壓縮或解壓後的路徑
bzip2的使用方法同gzip

[root@localhost tmp]# bzip2 2.txt              #用bzip2壓縮2.txt,源文件消失,變成2.txt.bz2
[root@localhost tmp]# ll
總用量 16
drwxr-xr-x  2 root    root    4096 5月  22 09:38 123
-rw-r--r--  1 tcpdump tcpdump   68 5月  22 13:52 123.log
-rw-r--r--  1 root    root      49 5月  24 11:57 2.txt.bz2
[root@localhost tmp]# du -sh 2.txt.bz2              #可以看到壓縮等級比gzip要高,之前gzip壓縮後為12k,這裏壓縮完變成4K
4.0K    2.txt.bz2

解壓可以使用-d選項或者bunzip2,這裏使用bunzip2解壓

[root@localhost tmp]# bunzip2 2.txt.bz2              #解壓後,源文件消失,變成2.txt
[root@localhost tmp]# ll
總用量 10252
drwxr-xr-x  2 root    root        4096 5月  22 09:38 123
-rw-r--r--  1 tcpdump tcpdump       68 5月  22 13:52 123.log
-rw-r--r--  1 root    root    10485760 5月  24 11:57 2.txt

-c指定解壓縮後路徑,源文件不消失

[root@localhost tmp]# bzip2 -c 2.txt >/tmp/2.txt.bz2         
[root@localhost tmp]# ll                                               #可以看到原來的2.txt還在
總用量 10256
drwxr-xr-x  2 root    root        4096 5月  22 09:38 123
-rw-r--r--  1 tcpdump tcpdump       68 5月  22 13:52 123.log
-rw-r--r--  1 root    root    10485760 5月  24 11:57 2.txt
-rw-r--r--  1 root    root          49 5月  24 12:19 2.txt.bz2

同樣可以使用bzcat查看壓縮包的內容

[root@localhost tmp]# bzcat 2.txt.bz2             #查看壓縮包內容

6.4 xz壓縮工具

註意: xz同樣不可用於壓縮目錄
語法: xz [options] [filename]
選項:
-d:解壓縮

[root@localhost tmp]# xz 2.txt             #不跟選項直接壓縮,源文件消失
[root@localhost tmp]# ll
總用量 16
drwxr-xr-x  2 root    root    4096 5月  22 09:38 123
-rw-r--r--  1 tcpdump tcpdump   68 5月  22 13:52 123.log
-rw-r--r--  1 root    root    1660 5月  24 12:19 2.txt.xz
drwxrwxrwx. 2 root    root    4096 5月   1 02:49 nfstest
-rw-------. 1 root    root       0 4月  22 00:02 yum.log
-rw-------  1 root    root       0 5月  17 01:52 yum.log.bak
[root@localhost tmp]# du -sh 2.txt.xz 
4.0K    2.txt.xz

解壓可以使用-d或者unxz命令

[root@localhost tmp]# xz -d 2.txt.xz                #源文件消失,變成2.txt
[root@localhost tmp]# ll
總用量 10252
drwxr-xr-x  2 root    root        4096 5月  22 09:38 123
-rw-r--r--  1 tcpdump tcpdump       68 5月  22 13:52 123.log
-rw-r--r--  1 root    root    10485760 5月  24 12:19 2.txt

-c選項同gzip和bzip2用法一樣

6.5 zip壓縮工具

windows下最常見的壓縮格式,這裏也可以用在linux下,如果沒有這個命令,需要安裝zip包

[root@localhost tmp]# yum install -y zip

註意: zip既可以壓縮目錄文件也可以壓縮普通文件,源文件不消失
語法: zip [options] [filename.zip] [filename]
說明: zip後面先跟目標文件名,也就是自定義的壓縮包名,然後跟源文件名。
選項:
-r:壓縮目錄文件時使用,表示級聯壓縮,連通目錄內文件一同壓縮

[root@localhost tmp]# zip 2.txt.zip 2.txt                #這裏先指定壓縮後的文件名2.txt.zip再跟要壓縮的文件,源文件不消失
  adding: 2.txt (deflated 100%)
[root@localhost tmp]# ll
總用量 10264
drwxr-xr-x  2 root    root        4096 5月  22 09:38 123
-rw-r--r--  1 tcpdump tcpdump       68 5月  22 13:52 123.log
-rw-r--r--  1 root    root    10485760 5月  24 12:19 2.txt
-rw-r--r--  1 root    root       10350 5月  24 12:35 2.txt.zip

壓縮目錄-r,連通目錄內文件一同壓縮

[root@localhost tmp]# zip 123.zip 123
  adding: 123/ (stored 0%)
[root@localhost tmp]# ll
總用量 10268
drwxr-xr-x  2 root    root        4096 5月  22 09:38 123
-rw-r--r--  1 tcpdump tcpdump       68 5月  22 13:52 123.log
-rw-r--r--  1 root    root         158 5月  24 12:38 123.zip

解壓

[root@localhost tmp]# unzip 2.txt.zip      
Archive:  2.txt.zip
replace 2.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y                  #因為源文件不消失,2.txt已經存在,所以提示
  inflating: 2.txt                   
[root@localhost tmp]# ll
總用量 10268
drwxr-xr-x  2 root    root        4096 5月  22 09:38 123
-rw-r--r--  1 tcpdump tcpdump       68 5月  22 13:52 123.log
-rw-r--r--  1 root    root         158 5月  24 12:38 123.zip
-rw-r--r--  1 root    root    10485760 5月  24 12:19 2.txt
-rw-r--r--  1 root    root       10350 5月  24 12:35 2.txt.zip

-d選項可以指定解壓時路徑

[root@localhost tmp]# unzip 2.txt.zip -d /tmp/3.txt      #因為2.txt已經存在,這裏改成解壓為/tmp/3.txt
Archive:  2.txt.zip
  inflating: /tmp/3.txt/2.txt        
[root@localhost tmp]# ll
總用量 10272
drwxr-xr-x  2 root    root        4096 5月  22 09:38 123
-rw-r--r--  1 tcpdump tcpdump       68 5月  22 13:52 123.log
-rw-r--r--  1 root    root         158 5月  24 12:38 123.zip
-rw-r--r--  1 root    root    10485760 5月  24 12:19 2.txt
-rw-r--r--  1 root    root       10350 5月  24 12:35 2.txt.zip
drwxr-xr-x  2 root    root        4096 5月  24 12:43 3.txt

查看壓縮文件

[root@localhost tmp]# unzip -l 2.txt.zip 
Archive:  2.txt.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
 10485760  05-24-2018 12:19   2.txt
---------                     -------
 10485760                     1 file

6.6 tar打包

語法: tar [options] [filename]
選項:
-c:建立一個tar包或者壓縮文件包
-f:指定目標文件名,如果多個參數組合使用時,把-f放在最後面,必加選項
-z:同時用gzip壓縮
-j:同時用bzip2壓縮
-J:同時用xz壓縮
-t:查看包裏面的文件
-v:可視化
--exclude:後面跟文件名,表示打包除了該文件之外的內容
打包目錄或文件tar cvf

[root@localhost tmp]# touch 123/222.txt 123/333.txt
[root@localhost tmp]# ll 123
總用量 0
-rw-r--r-- 1 root root 0 5月  24 12:48 222.txt
-rw-r--r-- 1 root root 0 5月  24 12:48 333.txt
[root@localhost tmp]# tar cvf 123.tar 123             #先指定打包後文件名再跟打包的文件,這裏比較特殊,選項可以加-或者不加-,同樣
123/
123/333.txt
123/222.txt
[root@localhost tmp]# tar cvf 333.tar 123 2.txt     #同時打包多個
123/
123/333.txt
123/222.txt
2.txt

說明: 打包不會刪除源文件,當某.tar文件已經存在時,再次打包會直接覆蓋該文件,無任何提示。
查看包的內容
-t選項可以用來查看包的內容,-f指定查看的包

[root@localhost tmp]# tar -tvf 333.tar                  #可以看到這前的找包的123目錄和目錄下的文件,還有2.txt
drwxr-xr-x root/root         0 2018-05-24 12:48 123/
-rw-r--r-- root/root         0 2018-05-24 12:48 123/333.txt
-rw-r--r-- root/root         0 2018-05-24 12:48 123/222.txt
-rw-r--r-- root/root  10485760 2018-05-24 12:19 2.txt

解包tar xvf

[root@localhost tmp]# tar xvf 333.tar               #把333.tar解包,文件已經存在,會直接覆蓋,不提示
123/
123/333.txt
123/222.txt
2.txt

選擇性打包(--exclude)

[root@localhost tmp]# ll 123       #可以看到123目錄下有222.txt和333.txt 
總用量 0
-rw-r--r-- 1 root root 0 5月  24 12:48 222.txt
-rw-r--r-- 1 root root 0 5月  24 12:48 333.txt
[root@localhost tmp]# tar cvf 123.tar --exclude 333.txt 123           #這裏使用--exclude選項打包的時候把333.txt排除
123/
123/222.txt
[root@localhost tmp]# tar cvf 333.tar --exclude "*.txt" 123            #這裏排除*.txt,只打包目錄本身 
123/

6.7 打包並壓縮

語法: tar [options] [filename]
選項:
-z:同時用gzip壓縮
-j:同時用bzip2壓縮
-J:同時用xz壓縮
打包的同時用gzip壓縮 tar zcvf

[root@localhost tmp]# tar zcvf 3.tar.gz 123                           #這裏先指定打包壓縮後的文件名
123/
123/333.txt
123/222.txt
[root@localhost tmp]# ll
[root@localhost tmp]# ll
總用量 10260
drwxr-xr-x  2 root    root        4096 5月  24 12:48 123
-rw-r--r--  1 tcpdump tcpdump       68 5月  22 13:52 123.log
-rw-r--r--  1 root    root    10485760 5月  24 12:19 2.txt
-rw-r--r--  1 root    root         142 5月  24 13:08 3.tar.gz

打包的同時用bzip2壓縮 tar jcvf

[root@localhost tmp]# tar jcvf 3.tar.bz2 123
123/
123/333.txt
123/222.txt
[root@localhost tmp]# ll
總用量 10264
drwxr-xr-x  2 root    root        4096 5月  24 12:48 123
-rw-r--r--  1 tcpdump tcpdump       68 5月  22 13:52 123.log
-rw-r--r--  1 root    root    10485760 5月  24 12:19 2.txt
-rw-r--r--  1 root    root         152 5月  24 13:11 3.tar.bz2
-rw-r--r--  1 root    root         142 5月  24 13:08 3.tar.gz

打包的同時用xz壓縮 tar Jcvf,這裏是大寫J

[root@localhost tmp]# tar Jcvf 3.tar.xz 123
123/
123/333.txt
123/222.txt
[root@localhost tmp]# ll
總用量 10268
drwxr-xr-x  2 root    root        4096 5月  24 12:48 123
-rw-r--r--  1 tcpdump tcpdump       68 5月  22 13:52 123.log
-rw-r--r--  1 root    root    10485760 5月  24 12:19 2.txt
-rw-r--r--  1 root    root         152 5月  24 13:11 3.tar.bz2
-rw-r--r--  1 root    root         142 5月  24 13:08 3.tar.gz
-rw-r--r--  1 root    root         192 5月  24 13:12 3.tar.xz

解包

[root@localhost tmp]# tar zxvf 3.tar.gz                 #解包帶有zip格式的包
123/
123/333.txt
123/222.txt
[root@localhost tmp]# tar jxvf 3.tar.bz2               #解包帶有bzip2格式的包
123/
123/333.txt
123/222.txt
[root@localhost tmp]# tar Jxvf 3.tar.xz                #解包帶有xz格式的包
123/
123/333.txt
123/222.txt

打包發送到遠程主機並解包

[root@localhost tmp]# tar cvf - 123 |ssh 192.168.66.132 "cd /tmp/;tar xvf -"
123/
123/333.txt
123/222.txt
The authenticity of host ‘192.168.66.132 (192.168.66.132)‘ can‘t be established.
RSA key fingerprint is 2c:57:fc:93:19:b3:56:66:a7:7a:28:fa:47:5e:6e:8f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.66.132‘ (RSA) to the list of known hosts.
[email protected]‘s password: 
123/
123/333.txt
123/222.txt

2018-05-25筆記(文件的壓縮和打包)