1. 程式人生 > >linux系列(二十二):tar命令

linux系列(二十二):tar命令

1、命令格式

    tar[必要引數][選擇引數][檔案] 

2、命令功能

    用來壓縮和解壓檔案。tar本身不具有壓縮功能。他是呼叫壓縮功能實現的

3、命令引數    

必要引數:
-A 新增壓縮檔案到已存在的壓縮
-B 設定區塊大小
-c 建立新的壓縮檔案
-d 記錄檔案的差別
-r 新增檔案到已經壓縮的檔案
-u 新增改變了和現有的檔案到已經存在的壓縮檔案
-x 從壓縮的檔案中提取檔案
-t 顯示壓縮檔案的內容
-z 支援gzip解壓檔案
-j 支援bzip2解壓檔案
-Z 支援compress解壓檔案 -v 顯示操作過程 -l 檔案系統邊界設定 -k 保留原有檔案不覆蓋 -m 保留檔案不被覆蓋 -W 確認壓縮檔案的正確性 可選引數: -b 設定區塊數目 -C 切換到指定目錄 -f 指定壓縮檔案 --help 顯示幫助資訊 --version 顯示版本資訊

4、常見解包/壓縮命令

(1)、.tar

解包:tar xvf FileName.tar

打包:tar cvf FileName.tar DirName

(2)、.tar.gz和.tgz

解包:tar zxvf FileName.tar.gz

壓縮:tar zcvf FileName.tar.gz DirName

(3)、.tar.bz2

解壓:tar jxvf FileName.tar.bz2

壓縮:tar jcvf FileName.tar.bz2 DirName

(4)、.zip

解壓:unzip FileName.zip

壓縮:zip FileName.zip DirName

 (5)、.rar

解壓:rar x FileName.rar

壓縮:rar a FileName.rar DirName 

5、簡單例項

 (1)、將檔案打包成不同格式

命令:

tar -cvf t1.tar tar_test/    (僅打包,不壓縮!)
tar -zcvf t2.tar.gz tar_test/   (打包後,以 gzip 壓縮)
tar -jcvf t3.tar.bz2 tar_test/   (打包後,以 bzip2 壓縮 )

輸出:

[email protected]:~/test$ ls tar_test/
1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt
[email protected]-computer:~/test$ tar -cvf t1.tar tar_test/
tar_test/
tar_test/1.txt
tar_test/2.txt
tar_test/7.txt
tar_test/5.txt
tar_test/4.txt
tar_test/6.txt
tar_test/3.txt
[email protected]-computer:~/test$ ls
t1.tar  tar_test
[email protected]-computer:~/test$ tar -zcvf t2.tar.gz tar_test/
tar_test/
tar_test/1.txt
tar_test/2.txt
tar_test/7.txt
tar_test/5.txt
tar_test/4.txt
tar_test/6.txt
tar_test/3.txt
[email protected]-computer:~/test$ ls
t1.tar  t2.tar.gz  tar_test
[email protected]-computer:~/test$ tar -jcvf t3.tar.bz2 tar_test/
tar_test/
tar_test/1.txt
tar_test/2.txt
tar_test/7.txt
tar_test/5.txt
tar_test/4.txt
tar_test/6.txt
tar_test/3.txt
[email protected]-computer:~/test$ ls
t1.tar  t2.tar.gz  t3.tar.bz2  tar_test
[email protected]-computer:~/test$ 

(2)、檢視tar包內有哪些內容

命令:

    tar -tvf t1.tar

    tar -ztvf t2.tar.gz

    tar -jtvf t3.tar.bz2

輸出:

[email protected]:~/test$ tar -tvf t1.tar 
drwxr-xr-x felix/felix       0 2018-12-12 10:19 tar_test/
-rw-r--r-- felix/felix       0 2018-12-12 10:19 tar_test/1.txt
-rw-r--r-- felix/felix       0 2018-12-12 10:19 tar_test/2.txt
-rw-r--r-- felix/felix       0 2018-12-12 10:19 tar_test/7.txt
-rw-r--r-- felix/felix       0 2018-12-12 10:19 tar_test/5.txt
-rw-r--r-- felix/felix       0 2018-12-12 10:19 tar_test/4.txt
-rw-r--r-- felix/felix       0 2018-12-12 10:19 tar_test/6.txt
-rw-r--r-- felix/felix       0 2018-12-12 10:19 tar_test/3.txt
[email protected]-computer:~/test$ tar -ztvf t2.tar.gz 
drwxr-xr-x felix/felix       0 2018-12-12 10:19 tar_test/
-rw-r--r-- felix/felix       0 2018-12-12 10:19 tar_test/1.txt
-rw-r--r-- felix/felix       0 2018-12-12 10:19 tar_test/2.txt
-rw-r--r-- felix/felix       0 2018-12-12 10:19 tar_test/7.txt
-rw-r--r-- felix/felix       0 2018-12-12 10:19 tar_test/5.txt
-rw-r--r-- felix/felix       0 2018-12-12 10:19 tar_test/4.txt
-rw-r--r-- felix/felix       0 2018-12-12 10:19 tar_test/6.txt
-rw-r--r-- felix/felix       0 2018-12-12 10:19 tar_test/3.txt
[email protected]-computer:~/test$ tar -jtvf t3.tar.bz2 
drwxr-xr-x felix/felix       0 2018-12-12 10:19 tar_test/
-rw-r--r-- felix/felix       0 2018-12-12 10:19 tar_test/1.txt
-rw-r--r-- felix/felix       0 2018-12-12 10:19 tar_test/2.txt
-rw-r--r-- felix/felix       0 2018-12-12 10:19 tar_test/7.txt
-rw-r--r-- felix/felix       0 2018-12-12 10:19 tar_test/5.txt
-rw-r--r-- felix/felix       0 2018-12-12 10:19 tar_test/4.txt
-rw-r--r-- felix/felix       0 2018-12-12 10:19 tar_test/6.txt
-rw-r--r-- felix/felix       0 2018-12-12 10:19 tar_test/3.txt
[email protected]-computer:~/test

(3)、打包的時候排除某些檔案

命令:

tar --exclude tar_test/1* -zcvf tt.tar.gz tar_test/tar_test/  (使用--exclude)

輸出:

[email protected]:~/test$ tar --exclude tar_test/1* -zcvf tt.tar.gz tar_test/tar_test/
tar_test/2.txt
tar_test/7.txt
tar_test/5.txt
tar_test/4.txt
tar_test/6.txt
tar_test/3.txt
[email protected]-computer:~/test$ ls tar_test/
1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt
[email protected]-computer:~/test$ tar -ztvf tt.tar.gz 
drwxr-xr-x felix/felix       0 2018-12-12 10:19 tar_test/
-rw-r--r-- felix/felix       0 2018-12-12 10:19 tar_test/2.txt
-rw-r--r-- felix/felix       0 2018-12-12 10:19 tar_test/7.txt
-rw-r--r-- felix/felix       0 2018-12-12 10:19 tar_test/5.txt
-rw-r--r-- felix/felix       0 2018-12-12 10:19 tar_test/4.txt
-rw-r--r-- felix/felix       0 2018-12-12 10:19 tar_test/6.txt
-rw-r--r-- felix/felix       0 2018-12-12 10:19 tar_test/3.txt
[email protected]-computer:~/test$