Linux系統壓縮和解壓縮工具gzip、bzip2、xz以及tar打包工具總結

在Linux系統使用過程中,對于一些不是常用的文件,利用cpu的時間資源對文件進行壓縮可以節省一定的磁盤空間,對系統中某個目錄經常會有備份的需求,可以使用Linux系統中的tar打包工具實現,文件的壓縮對于大量的網絡文件傳輸可以節約帶寬,同時也能降低服務器的負載,我們經常需要從網站下載軟件包到本地主機,這些包基本都是打包壓縮的,要想使用安裝,需要我們解壓縮他們,掌握Linux系統中的壓縮解壓縮以及打包工具的使用是系統運維工程師基礎必備技能;

1)壓縮工具

2)打包工具

1.壓縮工具:

     (1)壓縮原理:

     這里簡單敘述一下,我們知道文本文件中有大量的重復字符串,單詞和文字對于單獨重復的字符串和單詞,我們可以使用一個標記符號代表,這樣只存儲一個標記字符;這只是一個大致的原理,當然不同的壓縮工具使用的算法也就不同,同時后面新出的壓縮算法更加優異合理,對CPU的資源占用也不是很高,實現快速壓縮;同時有個問題需要注意,壓縮和解壓縮都需要消耗大量的CPU時間資源,尤其是對大文件的操作,更需要考慮系統的負載狀況,所以對于系統中的文件壓縮要充分考慮清楚,有沒有必要浪費CPU的時間資源來換取節約磁盤空間,同時還有一點需要注意,對于二進制格式的數據流文件壓縮效果不是很好比如說MP3格式的音樂等,它們受到自身數據格式影響;

    (2)compress/uncompress:

    這是一種早期的Linux系統中的壓縮和解壓縮工具,壓縮比不是很好,所以很少在使用;壓縮后的文以.Z    結尾;

 

    (3)gzip/gunzip/zcat:

   

     壓縮:gzip

     語法:

       gzip [option] /path/to/file

    常見選項:

       -#:指定壓縮比,默認6,范圍(1-9),數字越大,壓縮比越高;

       -d:相當于命令gunzip,解壓文件;

       -c:默認情況下壓縮完文件后會把原文件刪除,如果想保留可以使用如下格式:

      gzip -c /path/to/sourcefile > /path/to/sourcefile.gz

      -r:可以遞歸對目錄下的所有文件進行壓縮;

    解壓縮:gunzip

    語法:

     gunzip [option] /path/to/file.gz

    常見選項:

         -r:可以遞歸對目錄下的所有壓縮文件進行解壓縮,默認刪除原壓縮文件;

    查看壓縮:zcat 正常情況下不能對壓縮文件進行查看,打開也是一堆亂碼;實際工作原理是:不解壓文                   件到磁盤,只是臨時解壓在內存中,提供壓縮文件查看, 

        語法:

     zcat /path/to/file

        *注意:使用查看壓縮文件命令時,尤其是大文件,解壓到內存中,是相當占內存


[root@xia7 dir]# ls -hl 
total 1.4M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
[root@xia7 dir]# gzip messages 
[root@xia7 dir]# ls -hl
total 192K
-rw------- 1 root root 190K Aug 16 17:40 messages.gz
[root@xia7 dir]# gzip -d messages.gz 
[root@xia7 dir]# ls -lh
total 1.4M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
[root@xia7 dir]# gzip -9 messages 
[root@xia7 dir]# ls -lh
total 188K
-rw------- 1 root root 187K Aug 16 17:40 messages.gz
[root@xia7 dir]# gunzip messages.gz 
[root@xia7 dir]# ls -lh
total 1.4M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
[root@xia7 dir]# gzip -c messages >messages.gz
[root@xia7 dir]# ls -lh
total 1.6M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
-rw-r--r-- 1 root root 190K Aug 18 10:30 messages.gz
[root@xia7 dir]# zcat messages.gz 
Aug 14 11:29:01 xia7 rsyslogd: [origin software="rsyslogd" swVersion="7.4.7" x-pid="819" x-info="http://www.rsyslog.com"] rsyslogd was HUPed
Aug 14 11:30:01 xia7 systemd: Started Session 7 of user root.
Aug 14 11:30:01 xia7 systemd: Starting Session 7 of user root.
Aug 14 11:40:01 xia7 systemd: Started Session 8 of user root.
Aug 14 11:40:01 xia7 systemd: Starting Session 8 of user root.
Aug 14 11:50:01 xia7 systemd: Started Session 9 of user root.

(4)bzip2/bunzip2/bzcat:

 

     壓縮:bzip2

     語法:

     bzip2 [option] /path/to/file

     常見選項:

       -#:指定壓縮比,默認6,范圍(1-9),數字越大,壓縮比越高;

       -d:相當于命令bunzip2,解壓文件;

       -k:壓縮完后保留原文件;

 

      解壓縮:bunzip2

      語法:

             bunzip2 [option] /path/to/file.bz2

      常見選項: 

            -k:解壓縮完成后不刪除原壓縮文件;


     查看壓縮:bzcat

     語法:

            bzcat /path/to/file.bz2


     解壓縮:bunzip2 

     語法:

            bunzip [option] /path/to/file.bz2

    常見選項:

     -k:解壓縮完成后不刪除原壓縮文件;


[root@xia7 dir]# ls -lh
total 1.4M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
[root@xia7 dir]# bzip2 messages 
[root@xia7 dir]# ls -lh
total 84K
-rw------- 1 root root 83K Aug 16 17:40 messages.bz2
[root@xia7 dir]# bzip2 -d  messages.bz2 
[root@xia7 dir]# ls -lh 
total 1.4M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
[root@xia7 dir]# bzip2 -9 messages 
[root@xia7 dir]# ls -lh
total 84K
-rw------- 1 root root 83K Aug 16 17:40 messages.bz2
[root@xia7 dir]# bunzip2 messages.bz2 
[root@xia7 dir]# ls -lh
total 1.4M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
[root@xia7 dir]# bzip2 -k messages 
[root@xia7 dir]# ls -lh
total 1.5M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
-rw------- 1 root root  83K Aug 16 17:40 messages.bz2
[root@xia7 dir]# bzcat messages.bz2 
Aug 14 11:29:01 xia7 rsyslogd: [origin software="rsyslogd" swVersion="7.4.7" x-pid="819" x-info="http://www.rsyslog.com"] rsyslogd was HUPed
Aug 14 11:30:01 xia7 systemd: Started Session 7 of user root.
Aug 14 11:30:01 xia7 systemd: Starting Session 7 of user root.
Aug 14 11:40:01 xia7 systemd: Started Session 8 of user root.
Aug 14 11:40:01 xia7 systemd: Starting Session 8 of user root.
Aug 14 11:50:01 xia7 systemd: Started Session 9 of user root.

(5)xz/unxz/xzcat:

   

    壓縮:xz

    語法:

        xz [option] /path/to/file

    常見選項:

        -#:指定壓縮比,默認6,范圍(1-9),數字越大,壓縮比越高;

        -d:相當于命令unxz,解壓文件;

        -k:壓縮完后保留原文件;

     

        查看壓縮:xzcat:

        語法:

            xzcat /path/to/file.xz

        

        解壓縮:unxz

        語法:

            unxz [option] /path/to/file.xz

        常見選項:

            -k:解壓縮完成后不刪除原壓縮文件;

[root@xia7 dir]# ll -lh
total 1.4M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
[root@xia7 dir]# xz messages 
[root@xia7 dir]# ll -lh
total 60K
-rw------- 1 root root 58K Aug 16 17:40 messages.xz
[root@xia7 dir]# xz -d messages.xz 
[root@xia7 dir]# ls -lh
total 1.4M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
[root@xia7 dir]# xz -9 messages 
[root@xia7 dir]# ls -lh
total 60K
-rw------- 1 root root 58K Aug 16 17:40 messages.xz
[root@xia7 dir]# unxz messages.xz 
[root@xia7 dir]# ls -lh
total 1.4M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
[root@xia7 dir]# xz -k messages 
[root@xia7 dir]# ls -lh
total 1.5M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
-rw------- 1 root root  58K Aug 16 17:40 messages.xz
[root@xia7 dir]# xzcat messages.xz 
Aug 14 11:29:01 xia7 rsyslogd: [origin software="rsyslogd" swVersion="7.4.7" x-pid="819" x-info="http://www.rsyslog.com"] rsyslogd was HUPed
Aug 14 11:30:01 xia7 systemd: Started Session 7 of user root.
Aug 14 11:30:01 xia7 systemd: Starting Session 7 of user root.
Aug 14 11:40:01 xia7 systemd: Started Session 8 of user root.
Aug 14 11:40:01 xia7 systemd: Starting Session 8 of user root.

  

(6)zip/unzip

     壓縮:zip

     語法:

     zip /path/to/file.zip /opt/to/file…

     zip壓縮工具可以把多個文件打包壓縮成一個文件;

        

        解壓縮:unzip

        語法:

            unzip /path/to/file.zip

[root@xia7 dir]# ll
total 1408
-rw-r--r-- 1 root root   13948 Aug 18 10:46 functions
-rw-r--r-- 1 root root      81 Aug 18 10:45 issue
-rw------- 1 root root 1415955 Aug 16 17:40 messages
-rw-r--r-- 1 root root    2016 Aug 18 10:45 passwd
[root@xia7 dir]# zip ceshi.zip ./*
  adding: functions (deflated 68%)
  adding: issue (deflated 2%)
  adding: messages (deflated 86%)
  adding: passwd (deflated 60%)
[root@xia7 dir]# ll -lh
total 1.6M
-rw-r--r-- 1 root root 196K Aug 18 10:48 ceshi.zip
-rw-r--r-- 1 root root  14K Aug 18 10:46 functions
-rw-r--r-- 1 root root   81 Aug 18 10:45 issue
-rw------- 1 root root 1.4M Aug 16 17:40 messages
-rw-r--r-- 1 root root 2.0K Aug 18 10:45 passwd
[root@xia7 dir]# rm -f functions issue messages passwd 
[root@xia7 dir]# unzip ceshi.zip 
Archive:  ceshi.zip
  inflating: functions               
  inflating: issue                   
  inflating: messages                
  inflating: passwd                  
[root@xia7 dir]# ls
ceshi.zip  functions  issue  messages  passwd
[root@xia7 dir]#

2.打包工具:

         前面講到的眾多壓縮工具一般只能對文件壓縮,不能對整個目錄打包壓縮歸檔,tar

工具就能很好的實現既能打包歸檔同時又能壓縮;

 

(1)tar工具:

      命令:tar

      語法:

          tar [option]…/path/to/file.tar /path/to/file…

          tar [option]…/path/to/file.tar

          tar [option]…/path/to/file.tar.[gz|bz2|xz] /path/to/file…

          tar [option]…/path/to/file.tar.[gz|bz2|xz] 

          tar [option]…/path/to/file.tar.[gz|bz2|xz] [-C /path/to/dir]

     常見選項:

         -c:打包歸檔成一個文件,相當于備份;

         -x:解包歸檔文件,默認當前目錄;

         -C:解包到指定目錄;

         -t:查看打包歸檔中的文件名;

         -f:跟打包文件參數,一般其他命令使用要跟-f結合,注意-f要放到選項最后;

         -z:使用gzip壓縮和解壓縮;

         -j:使用bzip2壓縮和解壓縮;

         -J:使用xz壓縮和解壓縮;

[root@xia7 dir]# ll
total 1408
-rw-r--r-- 1 root root   13948 Aug 18 10:46 functions
-rw-r--r-- 1 root root      81 Aug 18 10:45 issue
-rw------- 1 root root 1415955 Aug 16 17:40 messages
-rw-r--r-- 1 root root    2016 Aug 18 10:45 passwd
[root@xia7 dir]# tar cf ceshi.tar ./*
[root@xia7 dir]# ll -lh
total 2.8M
-rw-r--r-- 1 root root 1.4M Aug 18 11:14 ceshi.tar
-rw-r--r-- 1 root root  14K Aug 18 10:46 functions
-rw-r--r-- 1 root root   81 Aug 18 10:45 issue
-rw------- 1 root root 1.4M Aug 16 17:40 messages
-rw-r--r-- 1 root root 2.0K Aug 18 10:45 passwd
[root@xia7 dir]# tar zcf ceshi1.tar.gz functions issue messages 
[root@xia7 dir]# tar jcf ceshi2.tar.bz2 functions issue messages passwd 
[root@xia7 dir]# tar Jcf ceshi3.tar.xz functions issue messages passwd 
[root@xia7 dir]# ll -lh
total 3.1M
-rw-r--r-- 1 root root 194K Aug 18 11:15 ceshi1.tar.gz
-rw-r--r-- 1 root root  90K Aug 18 11:15 ceshi2.tar.bz2
-rw-r--r-- 1 root root  63K Aug 18 11:16 ceshi3.tar.xz
-rw-r--r-- 1 root root 1.4M Aug 18 11:14 ceshi.tar
-rw-r--r-- 1 root root  14K Aug 18 10:46 functions
-rw-r--r-- 1 root root   81 Aug 18 10:45 issue
-rw------- 1 root root 1.4M Aug 16 17:40 messages
-rw-r--r-- 1 root root 2.0K Aug 18 10:45 passwd
[root@xia7 dir]# tar tf ceshi2.tar.bz2 
functions
issue
messages
passwd
[root@xia7 dir]# tar tf ceshi3.tar.xz 
functions
issue
messages
passwd
[root@xia7 dir]# rm -f functions issue messages passwd 
[root@xia7 dir]# ll -lh
total 1.8M
-rw-r--r-- 1 root root 194K Aug 18 11:15 ceshi1.tar.gz
-rw-r--r-- 1 root root  90K Aug 18 11:15 ceshi2.tar.bz2
-rw-r--r-- 1 root root  63K Aug 18 11:16 ceshi3.tar.xz
-rw-r--r-- 1 root root 1.4M Aug 18 11:14 ceshi.tar
[root@xia7 dir]# tar xf ceshi3.tar.xz 
[root@xia7 dir]# ll -lh
total 3.1M
-rw-r--r-- 1 root root 194K Aug 18 11:15 ceshi1.tar.gz
-rw-r--r-- 1 root root  90K Aug 18 11:15 ceshi2.tar.bz2
-rw-r--r-- 1 root root  63K Aug 18 11:16 ceshi3.tar.xz
-rw-r--r-- 1 root root 1.4M Aug 18 11:14 ceshi.tar
-rw-r--r-- 1 root root  14K Aug 18 10:46 functions
-rw-r--r-- 1 root root   81 Aug 18 10:45 issue
-rw------- 1 root root 1.4M Aug 16 17:40 messages
-rw-r--r-- 1 root root 2.0K Aug 18 10:45 passwd
[root@xia7 dir]# mkdir /tmp/ceshitar
[root@xia7 dir]# tar xf ceshi2.tar.bz2 -C /tmp/ceshitar/
[root@xia7 dir]# ll /tmp/ceshitar/
total 1408
-rw-r--r-- 1 root root   13948 Aug 18 10:46 functions
-rw-r--r-- 1 root root      81 Aug 18 10:45 issue
-rw------- 1 root root 1415955 Aug 16 17:40 messages
-rw-r--r-- 1 root root    2016 Aug 18 10:45 passwd
[root@xia7 dir]#

原創文章,作者:xiashixiang,如若轉載,請注明出處:http://www.www58058.com/37223

(0)
xiashixiangxiashixiang
上一篇 2016-08-18 15:20
下一篇 2016-08-18 15:27

相關推薦

  • 磁盤管理、文件系統管理

    磁盤管理、文件系統管理 磁盤管理 磁盤設備 機械硬盤(非固態) I/O Ports —– I/O 設備地址 塊設備:Block 存取單位“塊”;(磁盤) 字符設備:char 存取單位“字符”;(鍵盤) 設備文件:關聯一個設備驅動程序,進而能夠跟與之對應的硬件設備進行通信; – 設備號碼: 主設備號 major number…

    Linux干貨 2016-07-22
  • Linux系統啟動過程及其修復過程簡析

    Linux組成 Linux: kernel+rootfs     kernel: 進程管理、內存管理、網絡管理、驅動程序、文件系統、安全功能     rootfs:程序和glibc     庫:函數集合, function, 調用接口(頭文…

    Linux干貨 2016-09-19
  • linux iptables替代工具nftables

    1 使用 FirewallD 構建動態防火墻 1.1 “守護進程” 1.2 靜態防火墻(system-config-firewall/lokkit) 1.3 使用 iptables 和 ip6tables 的靜態防火墻規則 1.4 什么是區域? 1.4.1 預定義的服務 1.4.2 端口和…

    Linux干貨 2015-03-11
  • sed 流編輯器使用詳解

    一、Stream EDitor, 行編輯器   sed是一種流編輯器,它一次處理一行內容。處理時,把當前處理的行存儲在臨時緩沖區中,稱為“模式空間”(pattern space),接著用sed命令處理緩沖區中的內容,處理完成后,把緩沖區的內容送往屏幕。接著處理下一行,這樣不斷重復,直到文件末尾。文件內容并沒有改變,除非你使用重定向存儲輸出…

    2016-08-10
  • 馬哥教育網絡班22期第3周課程作業

    1、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。 [root@MyCloudServer ~]# who | cut -d " " -f1 |sort -u root 2、取出最后登錄到當前系統的用…

    Linux干貨 2016-09-19
  • LINUX的初探

    一般而言,計算機的組成遵循著“馮諾依曼體系結構”。這種結構的體現實質上是一種存儲程序的原理,把程序本身當作數據來對待,程序和該程序處理的數據用同樣的方式來存儲。這種結構的要點是:計算機的數制采用二進制;計算機應該按照程序順序執行。

    2018-03-04
欧美性久久久久