Linux下的簡單壓縮相關操作

來源:互聯網
上載者:User

標籤:相對   tmp   排除   access   .gz   color   nginx   參考   不包含   

一、需求:壓縮/data/tmp

操作:

 1 [[email protected] /]# tar zcf /tmp/data.tar.gz data/                           #壓縮/data到/tmp下並將壓縮包命名為data.tar.gz 2 [[email protected] /]# ls -lrt /tmp/                                            #查看/tmp下的檔案 3 total 0 4-rw-r--r--  1 root     root  490 May 10 15:51 data.tar.gz                    #成功

 二、需求:查看壓縮包data.tar.gz裡的內容

操作:

 1 [[email protected] /]# tar tf /tmp/data.tar.gz                         #查看壓縮包data.tar.gz裡的內容,下面為內容(包含目錄、檔案、以及子目錄下的檔案) 2 data/ 3 data/logs/                  4 data/logs/access.log-20170426 5 data/logs/access.log-20170430 6 data/logs/access.log-20170427 7 data/logs/access.log-20170505 8 data/logs/access.log-20170429 9 data/logs/access.log-2017042510 data/logs/access.log-2017042411 data/logs/access.log-2017050312 data/logs/access.log13 data/logs/access.log-2017010114 data/logs/access.log-2017050815 data/logs/access.log-2017042816 data/logs/access.log-2017050717 data/logs/access.log-2017050418 data/logs/access.log-2017042219 data/logs/access.log-2017050120 data/logs/access.log-2017042321 data/logs/access.log-2017050222 data/logs/access.log-2017050623 data/hello.txt24 data/nginx.conf

 三、需求:解壓壓縮包data.tar.gz到其所在目錄

操作:

1 [[email protected] /]# tar xf /tmp/data.tar.gz

 四、需求:解壓壓縮包data.tar.gz到/root下

操作:

1 [[email protected] /]# tar xf /tmp/data.tar.gz -C /root

 五、需求:壓縮/data/tmp下,但不包含(排除)其中的hello.txt這個檔案

操作:

1 [[email protected] /]# tar zcf /tmp/data_pc.tar.gz data/ --exclude=hello.txt                      #使用--exclude參數排除hello.txt

 六、需求:壓縮/data/tmp下,但不包含(排除)其中的hello.txt;nginx.conf這兩個檔案(排除多個檔案)

操作:

1 [[email protected] /]# cat>>paichu.txt<<EOF                            #將hello.txt;nginx.conf兩個檔案名稱寫入到paichu.txt裡2 > hello.txt3 > nginx.conf4 > EOF5 [[email protected] /]# tar zcf /tmp/data_pc2.tar.gz data/ --exclude-from=paichu.txt      #使用--ecclude-from來排除paichu.txt裡的檔案

 

總結:

  使用tar命令來壓縮與解壓縮,如下:

  1. 壓縮檔:tar zcf [壓縮包路徑] [待壓縮目錄/檔案相對路徑]
  2. 查看壓縮檔:tar tf [壓縮包路徑]
  3. 解壓壓縮檔:tar xf [壓縮包路徑]
    1. 解壓到指定路徑:tar xf [壓縮包路徑] -C [解壓路徑]    
  4. 壓縮某個目錄但排除其中的某個檔案:tar zcf  [壓縮包路徑] [待壓縮目錄相對路徑] --exclude=[排除檔案的檔案名稱的相對路徑]
  5. 壓縮某個目錄但排除其中的多個檔案:
    1. 建立一個文字檔paichu.txt,並將欲排除檔案名稱寫入其中。
    2. 然後執行:tar zcf  [壓縮包路徑] [待壓縮目錄相對路徑] --exclude-from=[paichu.txt]

 

 ----------------------------------------------------分割線------------------------------------------------------

參考:

  1. 李泳宜老師授課
  2. http://man.linuxde.net/tar

Linux下的簡單壓縮相關操作

聯繫我們

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