歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> Linux中tar命令使用詳解

Linux中tar命令使用詳解

日期:2017/3/3 16:14:22   编辑:關於Linux

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 的基本用法就介紹完了,下面我們介紹幾個稍稍高級一點的用法

4.還是剛才那個tmp.tar.bz2包 我只想把其中的 "tmp/man.config.bz2" 解壓到當前目錄,怎麼做?

[root@localhost ~]# ll

total 56

-rw-------. 1 root root 1350 Jul 8 22:03 anaconda-ks.cfg

-rw-r--r--. 1 root root 24606 Jul 8 22:03 install.log

-rw-r--r--. 1 root root 7345 Jul 8 22:01 install.log.syslog

-rw-r--r--. 1 root root 9947 Aug 17 18:10 tmp.tar.bz2

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

tmp/man.config.bz2

[root@localhost ~]# ll ./tmp

total 4

-rw-r--r--. 1 root root 2195 Aug 17 12:37 man.config.bz2

可以看到只有 man.config.bz2 被解壓出來了

5.打包時不包含某些文件的做法

[root@localhost ~]# ll /tmp

total 24

-rw-r--r--. 1 root root 4940 Aug 17 12:37 man.config

-rw-r--r--. 1 root root 2332 Aug 17 13:14 man.config1.gz

-rw-r--r--. 1 root root 2184 Aug 17 13:15 man.config9.gz

-rw-r--r--. 1 root root 2195 Aug 17 12:37 man.config.bz2

drwxr-xr-x. 3 root root 4096 Aug 17 18:47 tar

-rw-r--r--. 1 root root 0 Aug 17 19:13 tartest.out

本例中 不想包含 tar 這個文件夾, 可以這麼做

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

tar: Removing leading `/' from member names

/tmp/

/tmp/man.config1.gz

/tmp/man.config

/tmp/tartest.out

/tmp/.ICE-unix/

/tmp/man.config9.gz

/tmp/man.config.bz2

[root@localhost ~]# tar -tjvf tmp.tar.bz2 |grep "/$"

drwxrwxrwt root/root 0 2013-08-17 19:13 tmp/

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

可以看到打包後的文件中確實沒有 tar 這個目錄

 

6.這次我們要將 /tmp 目錄下 8月17日 18:00 以後生成的文件打包,可以這樣做

[root@localhost ~]# ll /tmp

total 24

-rw-r--r--. 1 root root 4940 Aug 17 12:37 man.config

-rw-r--r--. 1 root root 2332 Aug 17 13:14 man.config1.gz

-rw-r--r--. 1 root root 2184 Aug 17 13:15 man.config9.gz

-rw-r--r--. 1 root root 2195 Aug 17 12:37 man.config.bz2

drwxr-xr-x. 3 root root 4096 Aug 17 18:47 tar

-rw-r--r--. 1 root root 0 Aug 17 19:13 tartest.out

[root@localhost ~]# tar -cjvf tmp.tar.bz2 --newer-mtime="2013-08-17 18:00:00" /tmp

tar: Removing leading `/' from member names

/tmp/

tar: /tmp/man.config1.gz: file is unchanged; not dumped

tar: /tmp/man.config: file is unchanged; not dumped

/tmp/tartest.out

/tmp/.ICE-unix/

tar: /tmp/man.config9.gz: file is unchanged; not dumped

/tmp/tar/

/tmp/tar/tmp/

tar: /tmp/tar/tmp/man.config1.gz: file is unchanged; not dumped

tar: /tmp/tar/tmp/man.config: file is unchanged; not dumped

/tmp/tar/tmp/.ICE-unix/

tar: /tmp/tar/tmp/man.config9.gz: file is unchanged; not dumped

tar: /tmp/tar/tmp/man.config.bz2: file is unchanged; not dumped

tar: /tmp/man.config.bz2: file is unchanged; not dumped

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

drwxrwxrwt root/root 0 2013-08-17 19:42 tmp/

-rw-r--r-- root/root 0 2013-08-17 19:13 tmp/tartest.out

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

drwxr-xr-x root/root 0 2013-08-17 18:47 tmp/tar/

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

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

這個功能在備份時比較有用,可以做差異備份,如果用tar來做系統備份的話,為了保證備份資料保持原來的權限與屬性,可以加入 -p 參數

Copyright © Linux教程網 All Rights Reserved