壓縮(zip),壓縮zip

來源:互聯網
上載者:User

壓縮(zip),壓縮zip

  預設情況這些壓縮公用程式在壓縮後會刪除源檔案(zip除外);而且預設只壓縮檔,而不壓縮目錄(連結到打包程式)。

    1. gzip

    2. bzip2

    3. zip

    4. GNU tar

     

    • 1.gzip

    1.1.壓縮

      gzip 壓縮公用程式壓縮一個普通副檔名為“.gz”。壓縮後刪除了原檔案。

    $ gzip user.sh

      可以一次壓縮多個檔案。壓縮時每個檔案獨立壓縮。

    $ gzip *

     

    1.2.解壓

      解壓縮;同樣刪除原檔案。

    $ gunzip user.sh.gz

      其實,解壓縮使用的命令是個指令碼。

    #!/bin/shPATH=${GZIP_BINDIR-'/bin'}:$PATHexec gzip -d "$@"

     

    1.3.測試壓縮檔完整性

    $ gzip -t hellogzip: hello: unexpected end of file

     

    1.4.查看壓縮檔內容

    $ zcat hello.gz

     

    • 2.bzip2/xz

    2.1.壓縮

      壓縮後副檔名“.bz2”,同樣會刪除原檔案。

    $ bzip2 *

      使用星號時,會壓縮多個檔案,每個檔案獨立壓縮。

     

    2.2.解壓

    $ bunzip2 *$ bzip2 -d *

     

    2.3.保留原檔案

    $ bzip2 -k hello

      不支援使用星號

     

    2.4.查看壓縮檔內容

    $ bzcat hello.bz2

     

    • 3.zip

      壓縮時指定壓縮包名稱為“foo”,最後產生壓縮包名為“foo.zip”。追加成員,無須特定選項。
      壓縮時,運行在unix環境下,支援正在運算式匹配:?、*、[]。分別代表匹配單個字元、匹配任意數量字元(包括空)、其中之一匹配。

    3.1.壓縮目錄

      可以直接壓縮目錄

    $ zip mash.zip mash/

     

      這樣確實可以直接壓縮目錄,但是如果目錄“mash”下全是目錄檔案,那麼壓縮包裡就是空的。因為之壓縮了目錄(確實是壓縮了)。

      壓縮目錄 "zip-dir" 下的所有檔案(包含其中子目錄及子目錄中檔案),解壓後還有目錄 “zip-dir”。(選項帶“-r”、末尾帶星號“*”)

    $ zip -r zip-dir.zip zip-dir/*

     

    3.2.壓縮檔

    壓縮目前的目錄下的所有檔案和目錄(不包含目前的目錄子目錄中的檔案)。

    $ zip zip-dir *

     

    3.3.指定壓縮包位置

    指定到其它目錄下。

    $ zip /test/zip-dir *

     

    3.4.壓縮後刪除原始檔案

    建立壓縮檔“all.zip”的同時刪除原始檔案“all”。

    $ cd zip-dir$ mkdir all$ zip -rm all all

     

    追加目前的目錄下的檔案到壓縮檔;並刪除原始檔案。

    $ zip -rm all *

     

    3.5.為壓縮包設定密碼

      給壓縮包加密,使用選項“-e”。

    $ zip -r -e bin.zip bin/*Enter password: Verify password:   adding: bin/zcw_bak4release-3.2.sh (deflated 57%)  adding: bin/zcw_mkdir4bak-2.2.sh (deflated 52%)  adding: bin/zcw_replace4release-2.2.sh (deflated 58%)  adding: bin/zcw_Virtualfile-1.3.sh (deflated 56%)

     

      可以直接在命令列指定加密密碼,使用大寫“-P”:

    # zip -r -e -P hello bin.zip bin/*

     

    3.6.顯示壓縮了多少檔案

    在壓縮完成後顯示壓縮了多少檔案。

    $ zip -dc bin.zip bin/*  0/  4 updating: bin/zcw_bak4release-3.2.sh (deflated 57%)  1/  3 updating: bin/zcw_mkdir4bak-2.2.sh (deflated 52%)  2/  2 updating: bin/zcw_replace4release-2.2.sh (deflated 58%)  3/  1 updating: bin/zcw_Virtualfile-1.3.sh (deflated 56%)

     

    3.7.壓縮後去掉目錄

      僅壓縮指定目錄下的檔案而不包含其中子目錄,也不包含目前的目錄(zip-dir)。(解壓後只有檔案不見目錄)

    $ zip -j zip-dir zip-dir/*

     

      執行個體:擷取tomcat的日誌(壓縮後路徑較長,使用不便):

    [root@right mag]# zip -j catalina.zip /home/work/tomcat4file/logs/catalina.out   adding: catalina.out (deflated 93%)

     

    3.8.改變壓縮輸入方式(壓縮檔來自管道)

    $ find /etc/sysconfig/network-scripts -name "ifcfg*" -print | zip ifcfg -@

     

    3.9.追加檔案到壓縮包

      追加檔案到壓縮包,啥都不要直接操作。

    [root@right mag]# zip back 1.txt  adding: 1.txt (stored 0%)[root@right mag]# zip back 2.conf  adding: 2.conf (stored 0%)[root@right mag]# zip back 3.xml 4.html 5.sql  adding: 3.xml (stored 0%)  adding: 4.html (stored 0%)  adding: 5.sql (stored 0%)[root@right mag]# unzip -l backArchive:  back.zip  Length      Date    Time    Name---------  ---------- -----   ----        0  01-11-2014 15:55   1.txt        0  01-11-2014 15:55   2.conf        0  01-11-2014 15:55   3.xml        0  01-11-2014 15:55   4.html        0  01-11-2014 15:55   5.sql---------                     -------        0                     5 files

     

    3.10.壓縮指定檔案

      通過選項“-i”、“--include”擷取指定格式的檔案:

    zip -r foo . -i \*.c

      只壓縮某些日誌,執行個體:

    [root@right mag]# zip -r lz.zip /home -i \*.out  adding: home/work/tomcat4file/logs/catalina.out (deflated 93%)[root@right mag]# unzip -l lz.zip Archive:  lz.zip  Length      Date    Time    Name---------  ---------- -----   ----132020334  01-11-2014 15:09   home/work/tomcat4file/logs/catalina.out---------                     -------132020334                     1 file

     

      研發常常會找營運人員要檔案:

    [root@iZ28srao5ssZ mag]# zip -rj config.zip /home/work -i "*config.properties"  adding: config.properties (deflated 63%)

     

    3.11.壓縮時跳過指定檔案

      指定選項“-x”、“--exclude”。
      跳過手機客戶程式:

    # zip -r 160.zip /home/work/release/* -x \*.apk

     

    3.12.刪除壓縮檔中某個指定檔案

      指定名稱名,使用選項“-d”刪除。

    [root@iZ28srao5ssZ mag]# zip -d config.zip home/work*deleting: home/work/release/caiBao/WEB-INF/classes/config.properties[work@app47 .donatello]$ zip -d 10_zcw_release-3.0.1.zip _re*deleting: _replace4release-3.2.sh

     

    3.13.檢查檔案

      選項“-T”

     

    # zip -T cat-47.zip test of cat-47.zip OK

     

     

     

    • unzip

    3.1.測試壓縮檔

      檢查壓縮後有沒有錯誤產生

    $ unzip -t binArchive:  bin.zip    testing: bin/                     OK    testing: bin/zcw_bak4release-3.2.sh   OK    testing: bin/zcw_Virtualfile-1.3.sh   OKNo errors detected in compressed data of bin.zip.$ unzip -tq binNo errors detected in compressed data of bin.zip.

     

    3.2.列出壓縮包中的檔案

    $ unzip -l zip-dir.zip

      詳細列出壓縮包中的檔案資訊

    $ unzip -v zip-dir.zip

     

    3.3.解壓所有檔案  

      提取zcw.zip中所有檔案

    $ unzip zcwArchive:  zcw.zip  inflating: zcw_replace4release-2.3.sh    inflating: zcw_Virtualfile-1.3.sh

     

      解壓時,去掉目錄結構。

    $ unzip -j bin

     

      解壓指定的某個檔案

    $ unzip bin \*zcw_V*.shArchive:  bin.zip  inflating: bin/zcw_Virtualfile-1.3.sh

     

    • FAQ: 解壓後的位置

    $ zip base.repo.zip /etc/yum.repos.d/CentOS-Base.repo$ unzip -l base.repo.zipArchive:  base.repo.zip  Length      Date    Time    Name---------  ---------- -----   ----     1926  12-28-2014 14:22   etc/yum.repos.d/CentOS-Base.repo---------                     -------     1926                     1 file

     

    即:

    $ cd /data/Test$ unzip /root/base.repo.zip$ ls -R etc/
    $ ls /data/Test/etc/yum.repos.d/CentOS-Base.repo/data/Test/etc/yum.repos.d/CentOS-Base.repo

     

    3.4.指定解壓目錄

      預設解壓所有檔案、子目錄到目前的目錄下,使用選項“-d”可以指定目錄。

    # unzip caiBao.zip -d /home/work/payment_front_pro/release/

     

    • 4. GNU tar

     

    主要操作模式

      -cf  歸檔打包

      -tf  查看歸檔包(效果類似於“ls”的顯示樣式,but不是以空白分割,而是換行分割)

      -tvf  查看歸檔包(效果類似於“ls -l”的顯示樣式)

      -xf  歸檔解包

     

    通用選項

      -C  指定解壓目錄

      -v  詳細列出已處理的檔案

    $ tar -cf demo.tar demo/$ tar -tf demo.tar…… $ tar -xf nari.tar

     

    4.1.壓縮

    $ tar -zcvf 2022.tar.gz 2022

      壓縮時帶上時間資訊

    $ tar -zcf etc_$(date +%F).tar.gz /etc$ tar -jcf root_$(date +%Y.%m.%d-%H%M%S).tar.bz2 /root$ tar -jcf root_$(date +%y.%m.%d-%H%M%S).tar.bz2 /root

     

    4.2.解壓縮

    $ tar -zxvf 2022.tar.gz$ tar -zxvf 2022.tgz$ tar -jxvf 2022.tar.bz2$ tar -Jxvf 2022.tar.Z$ tar -xf 2022.tar.gz

     

      預設還原到原始打包的路徑下;可以使用選項“-C”指定解壓後的目錄。

    $ tar -zxf /opt/etc.tar.gz$ tar -zxf /opt/etc.tar.gz -C /root

     

    4.3.查看壓縮包檔案結構

    查看壓縮檔列表

    $ tar -tf etc.tar.gz

    詳細查看壓縮檔列表

    $ tar -tvf etc.tar.gz

     

     

    4.4.打包時跳過某些檔案

    選項“-X”、“--exclude”

    $ tar --exclude=/data/tomcat_cb/logs/* -zcvf  tom.tgz /data/tomcat_cb/

     

    相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.