歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> 每天一個Linux命令(24)tar命令

每天一個Linux命令(24)tar命令

日期:2017/3/3 12:30:30   编辑:Linux技術

[b] tar命令可以為linux的文件和目錄創建檔案。[/b][b] (1)用法:[/b]

用法: tar [選項] [文件參數][b] (2)功能:[/b]

[b] 功能: 用來壓縮和解壓文件。tar本身不具有壓縮功能。它是調用壓縮功能實現的。[/b] 利用tar命令,可以把一大堆的文件和目錄全部打包成一個文件,這對於備份文件或將幾個文件組合成為一個文件以便於網絡傳輸是非常有用的。

要弄清兩個概念:打包和壓縮 打包是指將一大堆文件或目錄變成一個總的文件;壓縮則是將一個大的文件通過一些壓縮算法變成一個小文件。

為什麼要區分這兩個概念呢? 這源於Linux中很多壓縮程序只能針對一個文件進行壓縮,這樣當你想要壓縮一大堆文件時,你得先將這一大堆文件先打成一個包(tar命令),然後再用壓縮程序進行壓縮(gzip bzip2命令)。

[b] (3)選項參數:[/b] 1) -c --create             建立新的備份文件

2) -z                   支持gzip解壓文件 3) -j                   支持bzip2解壓文件

4) -v --verbose             顯示指令執行過程

5) -f<備份文件> --file=<備份文件>    指定備份文件

6) -t或--list               列出備份文件的內容7) -N<日期格式>--newer=<日期時間> 只將較指定日期更新的文件保存到備份文件裡

8) -x或--extract或--get       從備份文件中還原文件

9) -C <目錄>             這個選項用在解壓縮,若要在特定目錄解壓縮,可以使用這個選項

10) -P --absolute-names       文件名使用絕對名稱,不移除文件名稱前的“/”號[b] (4)實例:[/b]

1)[root@localhost Documents]# tar -cvf Dir.tar Dir           f選項參數是必不可少的,這裡以普通壓縮格式壓縮文件夾   

[root@localhost Documents]# tar -cvf Dir.tar Dir
Dir/
Dir/head_text
Dir/less1
Dir/less2
[root@localhost Documents]# ll
總用量 28
-rwx--xrwx. 1 root root    27 5月  19 04:21 core.log
dr-xr-xr-x. 2 root root    46 5月  19 23:29 Dir
-rw-r--r--. 1 root root 10240 5月  21 06:22 Dir.tar
-r--r--r--. 1 root root     0 5月  19 04:16 find
dr--r--r--. 2 root root    84 5月  19 04:57 findDir
-r--r--r--. 1 root root     0 5月  15 18:21 newlocate
-rw-r--r--. 1 root root    85 5月  19 04:25 t3.txt
--w-------. 1 root root   259 5月  12 21:53 tail_text
--w-------. 1 root root   216 5月  12 22:24 tempory
--w-------. 1 root root     0 5月  15 18:34 uText

2)[root@localhost Documents]# tar -czvf Dir.tar.gz Dir        以其他格式壓縮有Gzip和bzip2兩種格式

[root@localhost Documents]# tar -czvf Dir.tar.gz Dir
Dir/
Dir/head_text
Dir/less1
Dir/less2
[root@localhost Documents]# tar -cjvf Dir.tar.bz2 Dir
Dir/
Dir/head_text
Dir/less1
Dir/less2
[root@localhost Documents]# ll
總用量 40
-rwx--xrwx. 1 root root    27 5月  19 04:21 core.log
dr-xr-xr-x. 2 root root    46 5月  19 23:29 Dir
-rw-r--r--. 1 root root 10240 5月  21 06:22 Dir.tar
-rw-r--r--. 1 root root   646 5月  21 06:35 Dir.tar.bz2                 //公認的後綴名,以bzip2壓縮
-rw-r--r--. 1 root root   656 5月  21 06:30 Dir.tar.gz                  //公認的後綴名,以Gzip壓縮
-r--r--r--. 1 root root     0 5月  19 04:16 find
dr--r--r--. 2 root root    84 5月  19 04:57 findDir
-r--r--r--. 1 root root     0 5月  15 18:21 newlocate
-rw-r--r--. 1 root root    85 5月  19 04:25 t3.txt
--w-------. 1 root root   259 5月  12 21:53 tail_text
--w-------. 1 root root   216 5月  12 22:24 tempory
--w-------. 1 root root     0 5月  15 18:34 uText
-rw-r--r--. 1 root root   105 5月  21 06:35 vf

3)[root@localhost Documents]# tar -ztvf Dir.tar.gz          查閱上述tar包內有哪些文件,按什麼格式壓縮就要按照什麼格式解壓

[root@localhost Documents]# tar -ztvf Dir.tar.gz
dr-xr-xr-x root/root         0 2016-05-19 23:29 Dir/
-r-xr-xr-x root/root       664 2016-05-09 07:59 Dir/head_text
-r-xr-xr-x root/root        45 2016-05-09 08:15 Dir/less1
-r-xr-xr-x root/root        57 2016-05-09 08:16 Dir/less2
[root@localhost Documents]# tar -jtvf Dir.tar.bz2
dr-xr-xr-x root/root         0 2016-05-19 23:29 Dir/
-r-xr-xr-x root/root       664 2016-05-09 07:59 Dir/head_text
-r-xr-xr-x root/root        45 2016-05-09 08:15 Dir/less1
-r-xr-xr-x root/root        57 2016-05-09 08:16 Dir/less2

4)[root@localhost findDir]# tar -zxvf Dir.tar.gz Dir          解壓tar.gz壓縮包,到指定名的文件夾,必須是Dir,不能變

[root@localhost findDir]# tar -zxvf Dir.tar.gz Dir
Dir/
Dir/head_text
Dir/less1
Dir/less2
[root@localhost findDir]# ll
總用量 20
dr-xr-xr-x. 2 root root    46 5月  19 23:29 Dir
-rw-r--r--. 1 root root 10240 5月  21 06:22 Dir.tar
-rw-r--r--. 1 root root   646 5月  21 06:35 Dir.tar.bz2
-rw-r--r--. 1 root root   656 5月  21 06:30 Dir.tar.gz
-r--r--r--. 1 root root     0 5月  17 04:18 p1.pdf
-r--r--r--. 1 root root     0 5月  17 04:18 p2.pdf
-r--r--r--. 1 root root     0 5月  17 03:50 t1.txt
-r--r--r--. 1 root root     0 5月  17 04:02 T1.txt
-r--r--r--. 1 root root     0 5月  19 04:58 t2.txt
-r--r--r--. 1 root root     0 5月  17 04:02 T2.txt

5)[root@localhost findDir]# tar -jxvf Dir.tar.bz2 Dir/less1    只將tar內的部分文件解壓出來,要進行匹配,如果不匹配會報錯“歸檔找不到”   

[root@localhost findDir]# tar -jxvf Dir.tar.bz2 Dir/less1
Dir/less1

6)[root@localhost Documents]# tar -pzcvf P.tar.gz find t3.txt vf uText     文件備份下來,並且保存其權限

[root@localhost Documents]# tar -pzcvf P.tar.gz find t3.txt vf uText
find
t3.txt
vf
uText

7)[root@localhost Documents]# tar -pzxvf P.tar.gz -C Pdir         指定解壓的目錄

[root@localhost Documents]# tar -zcvf P1.tar.gz find t3.txt vf uText               //沒有p參數的打包並壓縮
find
t3.txt
vf
uText
[root@localhost Documents]# ll
總用量 32
-rwx--xrwx. 1 root root   27 5月  19 04:21 core.log
dr-xr-xr-x. 2 root root   46 5月  19 23:29 Dir
-r--r--r--. 1 root root    0 5月  19 04:16 find
dr--r--r--. 3 root root 4096 5月  21 06:52 findDir
-r--r--r--. 1 root root    0 5月  15 18:21 newlocate
-rw-r--r--. 1 root root  317 5月  21 07:06 P1.tar.gz                    //沒有p參數
-rw-r--r--. 1 root root  317 5月  21 07:05 P.tar.gz                     //有p參數
-rw-r--r--. 1 root root   85 5月  19 04:25 t3.txt
--w-------. 1 root root  259 5月  12 21:53 tail_text
--w-------. 1 root root  216 5月  12 22:24 tempory
--w-------. 1 root root    0 5月  15 18:34 uText
-rw-r--r--. 1 root root  105 5月  21 06:35 vf
[root@localhost Documents]# mkdir Pdir
[root@localhost Documents]# tar -zxvf P.tar.gz -C Pdir                  //指定解壓縮的路徑
find
t3.txt
vf
uText
[root@localhost Documents]# mkdir NoPdir
[root@localhost Documents]# tar -zxvf P1.tar.gz -C NoPdir
find
t3.txt
vf
uText
[root@localhost Documents]# ls -l Pdir                         //查看有p無p的區別
總用量 8
-r--r--r--. 1 root root   0 5月  19 04:16 find
-rw-r--r--. 1 root root  85 5月  19 04:25 t3.txt
--w-------. 1 root root   0 5月  15 18:34 uText
-rw-r--r--. 1 root root 105 5月  21 06:35 vf
[root@localhost Documents]# ls -l NoPdir
總用量 8
-r--r--r--. 1 root root   0 5月  19 04:16 find
-rw-r--r--. 1 root root  85 5月  19 04:25 t3.txt
--w-------. 1 root root   0 5月  15 18:34 uText
-rw-r--r--. 1 root root 105 5月  21 06:35 vf          //並沒有發現有什麼區別

8)[root@localhost Documents]# tar -N "2016/05/20" -zcvf 520.tar.gz ./*      在文件夾當中,比某個日期新的文件才備份

[root@localhost Documents]# tar -N "2016/05/20" -zcvf 520.tar.gz ./*
tar: 選項 --after-date: 將日期 ‘2016/05/20’ 當作 2016-05-20 00:00:00
./520.tar.gz
tar: ./520.tar.gz:文件縮小 45 字節;用零填充
./core.log
./Dir/
./Dir/head_text
./Dir/less1
./Dir/less2
./find
./findDir/
./findDir/t1.txt
./findDir/T1.txt
./findDir/T2.txt
./findDir/p1.pdf
./findDir/p2.pdf
./findDir/t2.txt
./findDir/Dir.tar
./findDir/Dir.tar.gz
./findDir/Dir.tar.bz2
./findDir/Dir/
./findDir/Dir/head_text
./findDir/Dir/less2
./findDir/Dir/less1
./log17.tar.gz
./newlocate
./NoPdir/
./NoPdir/find
./NoPdir/t3.txt
./NoPdir/vf
./NoPdir/uText
./P1.tar.gz
./Pdir/
./Pdir/find
./Pdir/t3.txt
./Pdir/vf
./Pdir/uText
./P.tar.gz
tar: ./t3.txt: 文件未改變;未輸出
tar: ./tail_text: 文件未改變;未輸出
tar: ./tempory: 文件未改變;未輸出
tar: ./uText: 文件未改變;未輸出
./vf
9)[root@localhost Documents]# tar --exclude Dir/less1 -zcvf Dir.test2.tar.gz Dir      打包時不包括特定目錄下的文件

[root@localhost Documents]# tar --exclude ./Dir/less1 -zcvf Dir.test.tar.gz Dir       //這裡目錄加個./,經後續驗證,沒有達到不包括的效果
Dir/
Dir/head_text
Dir/less1
Dir/less2
[root@localhost Documents]# tar -ztvf Dir.test.tar.gz
dr-xr-xr-x root/root         0 2016-05-19 23:29 Dir/
-r-xr-xr-x root/root       664 2016-05-09 07:59 Dir/head_text
-r-xr-xr-x root/root        45 2016-05-09 08:15 Dir/less1
-r-xr-xr-x root/root        57 2016-05-09 08:16 Dir/less2
[root@localhost Documents]# tar --exclude Dir/less1 -zcvf Dir.test2.tar.gz Dir
Dir/
Dir/head_text
Dir/less2
[root@localhost Documents]# tar -ztvf Dir.test2.tar.gz
dr-xr-xr-x root/root         0 2016-05-19 23:29 Dir/
-r-xr-xr-x root/root       664 2016-05-09 07:59 Dir/head_text
-r-xr-xr-x root/root        57 2016-05-09 08:16 Dir/less2
[b] (5)其他:[/b]

利用tar,可以為某一特定文件創建檔案(備份文件),也可以在檔案中改變文件,或者向檔案中加入新的文件。tar最初被用來在磁帶上創建檔案,現在,用戶可以在任何設備上創建檔案。

Copyright © Linux教程網 All Rights Reserved