壓縮、解壓、打包

解壓、壓縮、打包

在Linux中,壓縮文件的擴展名大多有*.tar*.tar.gz*.tgz*.gz*.Z*.bz2*.xz等。Linux中有多個工具可以對文件進行壓縮,所以擴展名的作用主要是爲了識別該壓縮文件是通過哪個工具進行壓縮的。下面是擴展名的一些描述:

擴展名 描述
*.Z compress 程序壓縮的文件(已經不流行,基本用不到)
*.zip zip 程序壓縮的文件
*.gz gzip 程序壓縮的文件
*.bz2 bzip2 程序壓縮的文件
*.xz xz 程序壓縮的文件
*.tar tar 程序打包的數據,並沒有壓縮
*.tar.gz tar 程序打包的文件,並經過 gzip 的壓縮
*.tar.bz2 tar 程序打包的文件,並經過 bzip2 的壓縮
*.tar.xz tar 程序打包的文件,並經過 xz 的壓縮

下面主要介紹 gzipbzip2xztar這4個工具(指令)。

gzip(壓縮解壓)

簡介與語法

gzip可以說是應用最廣的壓縮指令,他可以解開compresszipgzip等軟件所壓縮的文件,其壓縮文件的檔名爲*.gz。下面是主要的語法:

# 壓縮文件
gzip [-cdtv#] 檔名
選項與參數:
-c:將壓縮的數據輸出到屏幕上,可透過數據流重導向來處理
-d:解壓
-t:用來檢驗一個壓縮文件的一致性,查看文件是否有錯誤
-v:查看壓縮比等信息
-#:#表示數字,代表壓縮等級,-1 最快,壓縮比最低,-9 最慢,壓縮比最高,預設爲 -6(壓縮比=源文件/壓縮文件)

# 查看壓縮文件
zcat 檔名.gz

案例操作

  • 找出/etc下(不含子目錄)容量最大的文件,並將它複製到/tmp,然後用gzip壓縮
[root@instance-d619ad0f ~]# ls -ldS /etc/*
-rw-r--r--.  1 root root   670293 Jun  7  2013 /etc/services
-rw-r--r--.  1 root root    27726 Jun  4 19:43 /etc/ld.so.cache
-rw-r--r--.  1 root root    12288 Jun  4 19:38 /etc/aliases.db
-rw-r--r--.  1 root root     8892 Jun 10  2014 /etc/nanorc
...
[root@instance-d619ad0f tmp]# cp /etc/services .
[root@instance-d619ad0f tmp]# gzip -v services
services:    79.7% -- replaced with services.gz

壓縮時,源文件本身會被壓縮,所以壓縮完成後,源文件就不存在了。

  • 讀取上面壓縮文件services.gz的文件內容
# 可以通過 zcat/zmore/zless 讀取壓縮文件的內容
[root@instance-d619vf0h tmp]# zcat services.gz
...
  • 解壓文件services.gz
[root@instance-d619ad0f tmp]# ls services*
services.gz
[root@instance-d619ad0f tmp]# gzip -d services.gz
[root@instance-d619ad0f tmp]# ls services*
services

解壓後,壓縮文件會被替換成源文件

  • 將文件services使用最佳的壓縮比壓縮,並保留源文件
[root@instance-d619ad0f tmp]# gzip -cv -9 services > services1.gz
services:    79.8%
[root@instance-d619ad0f tmp]# gzip -v services
services:    79.7% -- replaced with services.gz
[root@instance-d619ad0f tmp]# ls -l /etc/services services*
-rw-r--r--. 1 root root 670293 Jun  7  2013 /etc/services
-rw-r--r--  1 root root 135489 Aug 20 11:06 services1.gz
-rw-r--r--  1 root root 136088 Aug 20 11:03 services.gz
  • services.gz文件中,搜索關鍵詞http,並顯示關鍵詞所在的行號
[root@instance-d619ad0f tmp]# zgrep -n 'http' services.gz
14:#       http://www.iana.org/assignments/port-numbers
89:http            80/tcp          www www-http    # WorldWideWeb HTTP
...

bzip2(壓縮解壓)

簡介與語法

gzip取代了compress並提供了更好的壓縮比,而bzip2的壓縮比比gzip更高,且用法基本上與gzip相同。下面是主要的語法:

# 壓縮文件
bzip2 [-cdkvz#] 檔名
選項與參數:
-c:降壓所的過程產生的數據輸出到屏幕上
-d:解壓
-k:保留源文件
-v:顯示壓縮比等信息
-z:壓縮的參數(有默認值,可以不加)
-#:與 gzip 相同,設定壓縮比,-9最佳最慢,-1最差最快

# 查看壓縮文件
bzcat 檔名.bz2

案例操作

  • gzip範例中的/tmp/servicesbzip2壓縮
[root@instance-d619ad0f tmp]# bzip2 -v services
  services:  5.409:1,  1.479 bits/byte, 81.51% saved, 670293 in, 123932 out.
[root@instance-d619ad0f tmp]# ls -l services*
-rw-r--r-- 1 root root 135489 Aug 20 11:06 services1.gz
-rw-r--r-- 1 root root 123932 Aug 20 11:03 services.bz2
  • 讀取壓縮文件services.bz2的內容
[root@instance-d619ad0f tmp]# bzcat services.bz2 
...
  • 解壓文件services.ba2
[root@instance-d619ad0f tmp]# bzip2 -d services.bz2
[root@instance-d619ad0f tmp]# ls services*
services  services1.gz
  • 用最佳壓縮比壓縮文件services,並保留源文件
# 方式一
[root@instance-d619ad0f tmp]# bzip2 -9 -kv services
  services:  5.409:1,  1.479 bits/byte, 81.51% saved, 670293 in, 123932 out.
# 方式二
[root@instance-d619ad0f tmp]# bzip2 -9 -cv services > services1.bz2
  services:  5.409:1,  1.479 bits/byte, 81.51% saved, 670293 in, 123932 out.

xz(壓縮解壓)

簡介與語法

xz是比bzip2壓縮比更好的一款工具,相對應的,壓縮相同文件所花費時間也是gzipbzip2xz這三者中最久的,其壓縮文件的檔名爲*.xz。下面是主要的語法:

# 壓縮文件
xz [-dtlkc#] 檔名
選項與參數:
-d:解壓
-t:測試壓縮文件的完整性,查看是否有錯誤
-l:列出壓縮文件的相關信息
-k:保留源文件
-c:將壓縮數據輸出到屏幕
-#:與 gzip 相同,設定壓縮比,-9最佳最慢,-1最差最快

# 查看壓縮文件
xcat 檔名.xz

案例操作

  • 使用xz壓縮將services
[root@instance-d619ad0f tmp]# xz -v services
services (1/1)
  100 %        97.3 KiB / 654.6 KiB = 0.149
[root@instance-d619ad0f tmp]# ls -l services*
-rw-r--r-- 1 root root 123932 Aug 20 15:40 services1.bz2
-rw-r--r-- 1 root root 135489 Aug 20 11:06 services1.gz
-rw-r--r-- 1 root root 123932 Aug 20 11:03 services.bz2
-rw-r--r-- 1 root root  99608 Aug 20 11:03 services.xz
  • 列出壓縮文件services.xz的信息,然後讀出該文件的內容
[root@instance-d619ad0f tmp]# xz -l services.xz 
Strms  Blocks   Compressed Uncompressed  Ratio  Check   Filename
    1       1     97.3 KiB    654.6 KiB  0.149  CRC64   services.xz
[root@instance-d619vf0h tmp]# xzcat services.xz
...
  • 解壓文件services.xz
[root@instance-d619ad0f tmp]# xz -d services.xz
[root@instance-d619ad0f tmp]# ls services*
services  services1.bz2  services1.gz  services.bz2
  • 壓縮文件services,並保留源文件
[root@instance-d619ad0f tmp]# xz -k services
[root@instance-d619ad0f tmp]# ls -l services*
-rw-r--r-- 1 root root 670293 Aug 20 11:03 services
-rw-r--r-- 1 root root 123932 Aug 20 15:40 services1.bz2
-rw-r--r-- 1 root root 135489 Aug 20 11:06 services1.gz
-rw-r--r-- 1 root root 123932 Aug 20 11:03 services.bz2
-rw-r--r-- 1 root root  99608 Aug 20 11:03 services.xz

tar(打包)

簡介與語法

雖然gzipbzip2xz都能夠對目錄進行壓縮,但他們對目錄的壓縮是指將目錄內的所有文件分別進行壓縮,並不是像Windows中的WinRAR一樣可以把好多數據壓縮成一個文件。所以Linux中壓縮時可以通過打包指令tar來打包壓縮文件。
tar可以將多個目錄或文件打包成一個大文件,同時還可以通過gzip/bzip2/xz的支持,將文件同時進行壓縮。下面是主要的語法:

# 打包與壓縮
tar [-z|-j|-J] [cv] [-f 代建立的新檔名] filename...

# 查看檔名
tar [-z|-j|-J] [tv] [-f 已存在的tar檔名]

# 解壓
tar [-z|-j|-J] [xv] [-f 已存在的tar檔名] [-C 目錄]

選項與參數
-c:建立打包文件,可搭配-v來查看過程中被打包的檔名
-t:查看打包文件的內容含有哪些檔名
-x:解打包或解壓縮,可以搭配 -C 解壓/解打包到特定目錄
(注意:-c、-t、-x不可同時出現在一串指令列中)

-z:通過 gzip 壓縮/解壓,檔名爲 *.tar.gz
-j:通過 bzip2 壓縮/解壓,檔名爲 *.tar.bz2
-J:通過 xz 壓縮/解壓,檔名爲 *.tar.xz
(注意:-z、-j、-J不可同時出現在一串指令列中)

-v:在壓縮/解壓過程中,將正在處理的文件名顯示到屏幕上
-f filename:-f 後要立即接要被處理的檔名,建議-f單獨寫一個選項(以防忘記)
-C 目錄:用於解壓,可以指定解壓到某個特定的目錄

-p:保留備份數據的原本權限與屬性,常用於備份重要的配置文件
-P:保留絕對路徑,允許備份數據含有根目錄
--exclude=FILE:在打包過程中,不要講文件 FILE 打包

案例操作

  • 分別使用-z-j-J來壓縮/etc,並查看所使用的時間
[root@instance-d619ad0f ~]# time tar -zpc -f etc.tar.gz /etc
tar: Removing leading `/' from member names

real    0m1.817s
user    0m1.348s
sys 0m0.068s
[root@instance-d619ad0f ~]# time tar -jpc -f etc.tar.bz2 /etc
tar: Removing leading `/' from member names

real    0m3.508s
user    0m3.290s
sys 0m0.059s
[root@instance-d619ad0f ~]# time tar -Jpc -f etc.tar.xz /etc
tar: Removing leading `/' from member names

real    0m16.277s
user    0m15.364s
sys 0m0.219s
[root@instance-d619ad0f ~]# ll etc*
-rw-r--r-- 1 root root  9301856 Aug 20 16:50 etc.tar.bz2
-rw-r--r-- 1 root root 10744695 Aug 20 16:50 etc.tar.gz
-rw-r--r-- 1 root root  6969436 Aug 20 16:51 etc.tar.xz

壓縮的時候出現了警告tar: Removing leading `/' from member names,主要是告訴使用者壓縮的時候移除了開頭的根目錄/。去掉根目錄主要是爲了安全。如果沒有拿掉根目錄,解壓後的檔名是絕對路徑,即壓縮之前文件放在哪就會解壓到哪。如果某個文件做過修改,這樣就會覆蓋文件恢復爲壓縮前的數據。

  • 查看tar文件的數據內容
[root@instance-d619ad0f ~]# tar -jtv -f etc.tar.bz2
...
drwxr-xr-x root/root         0 2018-05-30 19:38 etc/ppp/
-rwxr-xr-x root/root      1687 2018-01-03 00:29 etc/ppp/ipv6-down
-rwxr-xr-x root/root       386 2018-01-03 00:29 etc/ppp/ip-down
-rwxr-xr-x root/root      3182 2018-01-03 00:29 etc/ppp/ipv6-up
-rwxr-xr-x root/root      6426 2018-01-03 00:29 etc/ppp/ip-up.ipv6to4
-rwxr-xr-x root/root       430 2018-01-03 00:29 etc/ppp/ip-up
-rwxr-xr-x root/root      3214 2018-01-03 00:29 etc/ppp/ip-down.ipv6to4
...

etc/pppetc/ppp/ipv6-down這些前面並沒有根目錄/,這和前面的警告信息tar: Removing leading `/' from member names相對應。

  • 壓縮文件時保留根目錄,並查看備份文件的內容
[root@instance-d619ad0f ~]# tar -jpPc -f etc.and.root.tar.bz2 /etc
[root@instance-d619ad0f ~]# tar -jtvf etc.and.root.tar.bz2
...
drwxr-xr-x root/root         0 2018-05-30 19:38 /etc/ppp/
-rwxr-xr-x root/root      1687 2018-01-03 00:29 /etc/ppp/ipv6-down
-rwxr-xr-x root/root       386 2018-01-03 00:29 /etc/ppp/ip-down
-rwxr-xr-x root/root      3182 2018-01-03 00:29 /etc/ppp/ipv6-up
-rwxr-xr-x root/root      6426 2018-01-03 00:29 /etc/ppp/ip-up.ipv6to4
-rwxr-xr-x root/root       430 2018-01-03 00:29 /etc/ppp/ip-up
-rwxr-xr-x root/root      3214 2018-01-03 00:29 /etc/ppp/ip-down.ipv6to4
...
  • 解壓備份數據etc.tar.bz2
# 解壓到當前目錄
[root@instance-d619ad0f ~]# tar -jx -f etc.tar.bz2 
[root@instance-d619ad0f ~]# ls
etc  etc.and.root.tar.bz2  etc.tar.bz2  etc.tar.gz  etc.tar.xz

# 解壓到 /tmp 目錄
[root@instance-d619ad0f ~]# tar -jx -f etc.tar.bz2 -C /tmp
[root@instance-d619ad0f ~]# ls -ld etc /tmp/etc
drwxr-xr-x 87 root root 4096 Aug 13 13:47 etc
drwxr-xr-x 87 root root 4096 Aug 13 13:47 /tmp/etc
  • 解壓單一的文件
# 查找文件
[root@instance-d619ad0f ~]# tar -jtv -f etc.tar.bz2 | grep 'shadow'
---------- root/root       480 2018-07-06 16:14 etc/gshadow
---------- root/root       636 2018-06-30 17:38 etc/shadow-
---------- root/root       758 2018-08-13 13:47 etc/shadow
---------- root/root       470 2018-06-04 19:39 etc/gshadow-

# 解壓文件
[root@instance-d619ad0f ~]# tar -jxv -f etc.tar.bz2 etc/shadow
etc/shadow

# 查看文件
[root@instance-d619ad0f ~]# ll etc
total 4
---------- 1 root root 758 Aug 13 13:47 shadow
  • 打包某目錄,但不包含該目錄下的某個文件
# 打包壓縮 /etc /root 兩個目錄並存在 /root 目錄下,文檔名爲system.tar.bz2, 壓縮包內不包含 /root目錄下etc開頭的文檔以及壓縮包本身
[root@instance-d619ad0f ~]# tar -jc -f /root/system.tar.bz2 --exclude=/root/etc* \
> --exclude=/root/system.tar.bz2 /etc /root
tar: Removing leading `/' from member names
[root@instance-d619ad0f ~]# ll
total 44568
drwxr-xr-x 2 root root     4096 Aug 21 11:49 etc
-rw-r--r-- 1 root root  9301403 Aug 20 17:22 etc.and.root.tar.bz2
-rw-r--r-- 1 root root  9301856 Aug 20 16:50 etc.tar.bz2
-rw-r--r-- 1 root root 10744695 Aug 20 16:50 etc.tar.gz
-rw-r--r-- 1 root root  6969436 Aug 20 16:51 etc.tar.xz
-rw-r--r-- 1 root root  9306299 Aug 21 11:55 system.tar.bz2
  • 只打包比/etc/passwd新的文件
[root@instance-d619ad0f ~]# ll /etc/passwd
-rw-r--r-- 1 root root 1139 Jul  6 16:55 /etc/passwd

[root@instance-d619ad0f ~]# tar -jcv -f /root/etc.newer.then.passwd.tar.bz2 --newer-mtime='2018/07/06' /etc/*
...
/etc/yum/vars/
tar: /etc/yum/vars/infra: file is unchanged; not dumped
tar: /etc/yum/vars/contentdir: file is unchanged; not dumped
/etc/yum/pluginconf.d/
tar: /etc/yum/pluginconf.d/langpacks.conf: file is unchanged; not dumped
tar: /etc/yum/pluginconf.d/versionlock.list: file is unchanged; not dumped
tar: /etc/yum/pluginconf.d/fastestmirror.conf: file is unchanged; not dumped
tar: /etc/yum/pluginconf.d/versionlock.conf: file is unchanged; not dumped
tar: /etc/yum/version-groups.conf: file is unchanged; not dumped
/etc/yum/fssnap.d/
tar: /etc/yum.conf: file is unchanged; not dumped
/etc/yum.repos.d/
tar: /etc/yum.repos.d/epel-testing.repo: file is unchanged; not dumped
tar: /etc/yum.repos.d/CentOS-Debuginfo.repo: file is unchanged; not dumped
...

[root@instance-d619ad0f ~]# tar -jtv -f etc.newer.then.passwd.tar.bz2 | grep -v '/$'
-rw-r--r-- root/root       603 2018-07-06 16:14 etc/group
---------- root/root       480 2018-07-06 16:14 etc/gshadow
-rw-r--r-- root/root      1139 2018-07-06 16:55 etc/passwd
-rw-r--r-- root/root      1145 2018-07-06 16:26 etc/passwd-
---------- root/root       758 2018-08-13 13:47 etc/shadow
-rw-r--r-- root/root       328 2018-08-21 13:38 etc/sshd.deny.hosteye

總結

  • 目前比較流行的壓縮工具有gzipbzip2xz
  • 壓縮率比較:xz<bzip2<gzip(相同源文件,壓縮率越小,壓縮文件越小)
  • 壓縮時間:xz>bzip2>gzip
  • tar用於打包文件,支持用gzipbzip2xz壓縮打包文件
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章