Linux命令(30):tar命令-歸檔工具

tar命令


功能說明

    tar是tape archive的縮寫,是Linux下經常使用的歸檔工具,經常看到文件名以.tar和tgz結尾的文件,它們分別是用普通的tar命令歸檔的文件和用gzip歸檔的文件。tar歸檔文件可以由許多獨立的文件、一個或多個目錄層次或者兩者的混合組合而成。用法如下:

  tar [主選項+輔助選項] 文件或目錄

常用參數

選項說明
-c新建文件
-C指定位置解壓
-r列出歸檔文件的內容
-t列出檔案文件中已經歸檔的文件列表
-x從打包的檔案文件中還原文件
-u更新檔案文件,用新建文件替換檔案中的原始文件
-z調用gzip命令在打包過程中壓縮/解壓文件 .tar.gz
-j調用bzip2命令在打包過程中壓縮/解壓文件
-J調用xz命令在打包過程中壓縮/解壓文件
-w在還原文件時,把所有文件的修改時間設定爲現在時間
-Z用compress命令過濾檔案
-f"-f"選項後面緊跟檔案文件的存儲設備,默認是磁盤,需要指定檔案文件名;如果是磁帶,只需指定磁帶設備名即可。注意,在"-f"選項之後不能再跟任何其他選項,也就是說"-f"必須是tar命令的最後一個選項
-v指定在創建歸檔文件過程中,顯示各個歸檔文件的名稱
-p在文件歸檔的過程中,保持文件的屬性不發生變化
-N "yyyy/mm/dd"在指定日期之後的文件纔會打包到檔案文件中
--exclude file在打包過程中,不將指定的file文件打包
-X把不打包的文件寫到文件裏,配合X使用如:tar zcvf 1.tar.gz paichu.list ./shell/
常用組合命令
zcvf打包.tar.gz
zxvf解壓.tar.gz

示例                    

    使用cf壓縮,把當前目錄下的test文件夾打包成test.tar,爲便於演示,提前把app裏的文件複製到test裏一份

[root@c7 app]# tar -cf test.tar test/

[root@c7 app]# ll -h

總用量 432K

-rw-r--r--. 1 root root  181 11月 22 17:34 99.sh

-rw-------. 1 root root  12K 11月 22 14:08 messages

-rw-r--r--. 1 root root 1.2K 11月 22 15:17 messages2.gz

-rw-r--r--. 1 root root 1.2K 11月 22 14:36 messages.gz

-rw-r--r--. 1 root root 141K 11月 23 10:17 shell.txt

-rw-r--r--. 1 root root  48K 11月 23 10:17 shell.txt.bz2

drwxr-xr-x. 2 root root 4.0K 11月 23 17:07 test

-rw-r--r--. 1 root root 210K 11月 23 17:21 test.tar


    使用xf解壓使用-C把文件解壓到指定目錄下

[root@c7 app]# mkdir test1                       #創建目標目錄test1

[root@c7 app]# pwd

/root/app

[root@c7 app]# tar xf test.tar -C ./test1/     #解壓到當前目錄test1下

[root@c7 app]# ls ./test1/test/                  #驗證查看

99.sh  messages  messages2.gz  messages.gz  shell.txt  shell.txt.bz2

    將test目錄下的文件打包並壓縮,打包成test2.tar.gz

   將test目錄下的文件打包並壓縮,打包成test2.tar.gz2

[root@c7 app]# tar zcvf test2.tar.gz test          #調用gzip打包成test2.tar.gz

test/

test/99.sh

test/messages

test/messages2.gz

test/messages.gz

test/shell.txt

test/shell.txt.bz2

[root@c7 app]# tar jcvf test2.tar.bz2 test        #調用bzip2打包成test2.tar.bz2

test/

test/99.sh

test/messages

test/messages2.gz

test/messages.gz

test/shell.txt

test/shell.txt.bz2

[root@c7 app]# ll -h

...略

drwxr-xr-x. 2 root root 4.0K 11月 23 17:07 test

drwxr-xr-x. 3 root root   17 11月 23 17:24 test1

-rw-r--r--. 1 root root 105K 11月 24 09:06 test2.tar.bz2

-rw-r--r--. 1 root root 105K 11月 24 09:05 test2.tar.gz

-rw-r--r--. 1 root root 210K 11月 23 17:21 test.tar

    使用tf、ztvf參數,查看壓縮包的內容

[root@c7 app]# tar tf test2.tar.gz 

[root@c7 app]# tar ztvf test2.tar.gz 

drwxr-xr-x root/root         0 2016-11-23 17:07 test/

-rw-r--r-- root/root       181 2016-11-23 17:07 test/99.sh

-rw------- root/root     11595 2016-11-23 17:07 test/messages

-rw-r--r-- root/root      1149 2016-11-23 17:07 test/messages2.gz

-rw-r--r-- root/root      1202 2016-11-23 17:07 test/messages.gz

-rw-r--r-- root/root    143538 2016-11-23 17:07 test/shell.txt

-rw-r--r-- root/root     48218 2016-11-23 17:07 test/shell.txt.bz2

    使用zxvf參數,解壓tar.gz和tgz文件,這個最常用了

[root@c7 tmp]# pwd                               #到/tmp目錄下,在哪裏解壓文件,解壓後的文件就會在當前目錄下

/tmp

[root@c7 tmp]# tar zxvf /root/app/test2.tar.gz 

test/

test/99.sh

test/messages

test/messages2.gz

test/messages.gz

test/shell.txt

test/shell.txt.bz2

[root@c7 tmp]# ll -h test/

總用量 216K

-rw-r--r--. 1 root root  181 11月 23 17:07 99.sh

-rw-------. 1 root root  12K 11月 23 17:07 messages

-rw-r--r--. 1 root root 1.2K 11月 23 17:07 messages2.gz

-rw-r--r--. 1 root root 1.2K 11月 23 17:07 messages.gz

-rw-r--r--. 1 root root 141K 11月 23 17:07 shell.txt

-rw-r--r--. 1 root root  48K 11月 23 17:07 shell.txt.bz2

    使用zcvpf參數,將/tmp/test目錄打包到當前目錄下,並保存每個文件的權限

[root@c7 tmp]# tar zcvpf test3.tar.gz test/

test/

test/99.sh

test/messages

test/messages2.gz

test/messages.gz

test/shell.txt

test/shell.txt.bz2

[root@c7 tmp]# tar ztvf test3.tar.gz        #權限和上面的演示相同

drwxr-xr-x root/root         0 2016-11-23 17:07 test/

-rw-r--r-- root/root       181 2016-11-23 17:07 test/99.sh

-rw------- root/root     11595 2016-11-23 17:07 test/messages

-rw-r--r-- root/root      1149 2016-11-23 17:07 test/messages2.gz

-rw-r--r-- root/root      1202 2016-11-23 17:07 test/messages.gz

-rw-r--r-- root/root    143538 2016-11-23 17:07 test/shell.txt

-rw-r--r-- root/root     48218 2016-11-23 17:07 test/shell.txt.bz2

    在家目錄下,僅解壓/tmp/test3.tar.gz壓縮文件中的test/shell.txt文件

[root@c7 ~]# pwd

/root

[root@c7 ~]# tar zxvf /tmp/test3.tar.gz test/shell.txt

test/shell.txt

[root@c7 ~]# ll

總用量 8

-rw-------. 1 root root 1104 4月  13 2016 anaconda-ks.cfg

drwxr-xr-x. 4 root root 4096 11月 24 09:06 app

drwxr-xr-x. 2 root root   22 11月 24 09:34 test

[root@c7 ~]# ll test/

總用量 144

-rw-r--r--. 1 root root 143538 11月 23 17:07 shell.txt

    使用-N指定日期,打包/root/app目錄中2016/11/24以後的文件

[root@c7 tmp]# tar -N "2016/11/24" -zcvf log.tar.gz /root/app

    使用-exclude,打包文件時,排除指定文件

打包時排除expr1.sh文件

[root@redmine ~]# ls ./shell/

expr1.sh  judge_int.sh

[root@redmine ~]# tar zcvf sh.tar.gz --exclude=./shell/expr1.sh ./shell/

./shell/

./shell/judge_int.sh


打包時排除.sh的所有文件

[root@redmine ~]# ls ./shell/

1.txt  expr1.sh  judge_int.sh

[root@redmine ~]# tar zcvf sh.tar.gz --exclude=./shell/*.sh ./shell/     

./shell/

./shell/1.txt

[root@redmine ~]# tar tf sh.tar.gz 

./shell/

./shell/1.txt

    使用-C,指定位置解壓

[root@redmine ~]# tar xf sh.tar.gz -C /tmp/

[root@redmine ~]# ls /tmp/shell/

1.txt


學習自:

高俊峯《循序漸進Linux》

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章