linux基礎知識之打包壓縮命令

來源:互聯網
上載者:User

目前Linux下常見的壓縮檔副檔名:

*.Z          compress程式壓縮的檔案(最早期使用),壓縮後取代源檔案;
*.zip        一個誇平台的檔案打包程式,適用於Linux、windows和Mac OS系統;
*.bz2       bzip2程式壓縮的檔案;
*.gz         gzip程式壓縮的檔案;
*.tar         tar程式打包的檔案,並沒有壓縮;
*.tar.gz    tar程式打包的檔案,並經過gzip程式的壓縮;
*.tar.bz2  tar程式打包的檔案,並經過bzip2程式壓縮。

在實際中,compress壓縮命令已經不在是預設的壓縮軟體了。目前比較常見的壓縮命令是gzip和bzip2。但是這些只是壓縮命令而已,只針對單一檔案。tar程式則可以將眾多檔案“打包”為一個檔案,甚至對目錄也可以這樣做。但是tar不具有壓縮的功能。後來將tar和gzip等結合起來,使用起來非常方便。

tar命令詳解

tar [-cxtzjvfpPN] 檔案與目錄

參數:

-c :  建立壓縮檔的參數(create)。

-x :  解壓縮檔案的參數。

-t :  查看tarfile裡的檔案。

       注意,-c,-x,-t三個只能有一個,不能同時存在。

-z :  是否同時具有gzip的屬性?即是否需要用gzip壓縮。

-j :  是否同時具有bzip2的屬性?即是否需要用bzip2壓縮。

-v :  壓縮的過程中顯示檔案。

-f :  要壓縮儲存的檔案名稱。注意,當加入該參數時,該參數必須放在其他參數的後邊。

       tar -zcfv practice.tar.gz practice(錯誤)

       tar -zcvf practice.tar.gz practice(正確)

-p :  使用原檔案的原來屬性,不會根據使用者變化。

-P :  可以使用絕對路徑來壓縮。

-N :  比後面接的日期(yyyy/mm/dd)還要新的才會被打包進去。

--exclude FILE :  在壓縮的過程中,不要講FILE檔案打包。

常見執行個體

1、將目前的目錄的子目錄practice目錄下的檔案全部打包成practice.tar.gz(gzip壓縮)

      tar -cvf practice.tar.gz practice      (只打包不壓縮)

      tar -zcvf practice.tar.gz practice    (打包並以gzip壓縮)

2、將目前的目錄的子目錄practice目錄下的檔案全部打包成practice.tar.bz2(bzip2壓縮)

      tar -jcvf practice.tar.bz2 practice    (打包並以bzip2壓縮)

3、查看當前壓縮檔中有哪些檔案被壓縮。

     tar -ztvf practice.tar.gz

4、將指定檔案壓縮到practice.tar.gz中。

     tar -zcvf practice.tar.gz file1 file2 file3 dir1 dir2 dir3

5、將tar.gz檔案解壓到指定目錄下(/home/anatherdir/)。

     tar -zxvf practice.tar.gz -C /home/anatherdir/

6、將tar.gz壓縮檔中的某個檔案(file1)或者/和某個目錄(dir)解壓

     tar -zxvf practice.tar.gz file1 dir/

7、將practice目錄下晚於2011/03/14的檔案壓縮。

     tar -N ‘2011/03/14’ -zcvf practice.tar.gz practice

     結果如下,不知道咋回事。

                practice/
                practice/cfile.tar.gz
                tar: practice/pipes:檔案未改變;未輸出
                practice/a.c
                practice/pipes.c
                practice/hello.c
                practice/oushu
                practice/dir1/
                tar: practice/a:檔案未改變;未輸出
                tar: practice/hello:檔案未改變;未輸出
                practice/oushu.c

8、壓縮practice子目錄,但是不壓縮其中的某個檔案file1.txt

      tar --exclude practice/file1.txt -zcvf practice.tar.gz practice

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.