Linux中tar命令使用詳解

來源:互聯網
上載者:User

tar

用來將多個檔案或目錄打包成一個大檔案

1.將/tmp 打包並使用bzip2壓縮

[root@localhost ~]# tar -cjvf ./tmp.tar.bz2 /tmp

tar: Removing leading `/' from member names

/tmp/

/tmp/man.config1.gz

/tmp/man.config

/tmp/.ICE-unix/

/tmp/man.config9.gz

/tmp/man.config.bz2

-c 建立打包檔案

-j 使用 bzip2 對打包後的檔案進行壓縮

-v 在打包,解包過程中,顯示正在處理的檔案或目錄

-f 後面接打包後新產生的打包檔案的檔案名稱

打包後也可以使用 gzip 來進行壓縮 用 -z 參數替換上例中的 -j 參數就可以了 當然打包壓縮後的檔案名稱尾碼要更改成 *.tar.gz 因為 bzip2 壓縮效果比 gzip 更好,所以我一般是用 -j 參數

tar 在打包時 一般會把絕對路徑中的 "/" 去除 也就是輸出的第一句 "tar: Removing leading `/' from member names" 提示. 這樣做的好處是 可以防止你解一個tar包時,直接覆蓋正在使用的檔案. 比如你從別的主機打包了一個/etc 目錄的tar包到本機, 如果 "/" 沒有去除的話, 一解包, 因為是絕對路徑會直接覆蓋原生 /etc 目錄 ,那就悲劇了. 當然你也可以使用 -P 參數保留這個 "/" 不過你解包時就一定要多加小心了

2.查看剛才打包的檔案中有哪些檔案

[root@localhost ~]# tar -tjvf tmp.tar.bz2

drwxrwxrwt root/root         0 2013-08-17 14:16 tmp/

-rw-r--r-- root/root      2332 2013-08-17 13:14 tmp/man.config1.gz

-rw-r--r-- root/root      4940 2013-08-17 12:37 tmp/man.config

drwxrwxrwt root/root         0 2013-08-17 12:36 tmp/.ICE-unix/

-rw-r--r-- root/root      2184 2013-08-17 13:15 tmp/man.config9.gz

-rw-r--r-- root/root      2195 2013-08-17 12:37 tmp/man.config.bz2

-t 查看打包檔案

3.將打包檔案解壓到/tmp/tar目錄下

[root@localhost ~]# mkdir /tmp/tar

[root@localhost ~]# tar -xjvf tmp.tar.bz2 -C /tmp/tar

tmp/

tmp/man.config1.gz

tmp/man.config

tmp/.ICE-unix/

tmp/man.config9.gz

tmp/man.config.bz2

[root@localhost ~]# ll /tmp/tar

total 4

drwxrwxrwt. 3 root root 4096 Aug 17 14:16 tmp

-x 解包打包檔案

-C 將打包檔案解壓到指定目錄下, 如果不加這個參數, 預設是會解壓到當前工作目錄下

到此 tar 的基本用法就介紹完了,下面我們介紹幾個稍稍進階一點的用法

聯繫我們

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