15+ 個 tar 命令的使用示例 - Unix/Linux

jopen 10年前發布 | 16K 次閱讀 Linux UNIX

tar 命令用來將很多文件打包成一個單一的磁帶或者磁盤歸檔,并可從歸檔文件恢復出文件列表。當你需要通過 email 發送大量文件時或者傳輸文件時非常有用。這里我們介紹一些基本的使用場景。

tar 的語法:

# tar [options] file.tar file1 file2 .. .. ..

file.tar 是 tar 歸檔文件,而其他 file1 和 file2 等等是要被打包的文件。

例如我們有兩個文件 file1.txt 和 file2.txt

[root@localhost TAR]# ll
total 8
-rw-r--r--. 1 root root 2770 Feb  7 22:37 file1.txt
-rw-r--r--. 1 root root  887 Feb  7 22:38 file2.txt

tar 常用的使用場景

創建一個 tar 文件
語法:

# tar -cf archive.tar files .. ..

示例:

[root@localhost TAR]# tar -cf file.tar file1.txt file2.txt
[root@localhost TAR]# ll file.tar 
-rw-r--r--. 1 root root 10240 Feb  7 22:42 file.tar

列出 tar 文件中的所有文件列表

# tar -tf archive.tar

示例:

[root@localhost TAR]# tar -tf file.tar 
file1.txt
file2.txt

從 tar 中提取所有文件

tar -xf archive.tar

示例

[root@localhost TAR]# tar -xf file.tar 
[root@localhost TAR]# ll
total 20
-rw-r--r--. 1 root root  2770 Feb  7 22:37 file1.txt
-rw-r--r--. 1 root root   887 Feb  7 22:38 file2.txt
-rw-r--r--. 1 root root 10240 Feb  7 22:42 file.tar

參數選項

1, -v, –verbose
verbosely list files processed:
Syntax:
List all files in an archive.tar verbosely:

tar -tvf archive.tar

Example:

[root@localhost TAR]# tar -tvf file.tar 
-rw-r--r-- root/root      2770 2014-02-07 22:37 file1.txt
-rw-r--r-- root/root       887 2014-02-07 22:38 file2.txt

2, -c, –create
創建新的歸檔文件

3, -t, –list
列出歸檔文件中的內容

4, -x, –extract, –get
從歸檔中提取文件

5, -d, –diff, –compare
比較歸檔和文件系統的差異
Example:

[root@localhost TAR]# tar -tf file.tar 
file2.txt
file3.txt
file1.txt
[root@localhost TAR]# tar -df file.tar file1.txt file2.txt file4.txt
tar: file4.txt: Not found in archive
tar: Exiting with failure status due to previous errors
----Verbosely----
[root@localhost TAR]# tar -dvf file.tar file1.txt file2.txt 
file2.txt
file1.txt
[root@localhost TAR]# tar -dvf file.tar file1.txt file2.txt file6.txt
file2.txt
file1.txt
tar: file6.txt: Not found in archive
tar: Exiting with failure status due to previous errors

6, –delete
從歸檔中刪除某文件
示例:
從歸檔 file.tar 中刪除 file1.txt

[root@localhost TAR]# tar --delete -f  file.tar  file1.txt 
[root@localhost TAR]# tar -tf file.tar
file2.txt

7, -r, –append
追加文件到歸檔中
示例:
追加 file3.txt 到 file.tar

[root@localhost TAR]# tar -rf file.tar file3.txt
[root@localhost TAR]# tar -tf file.tar
file1.txt
file2.txt
file3.txt

8, -A, –catenate, –concatenate
將一個tar 歸檔追加到另外一個歸檔文件中
創建另外一個 tar 文件

[root@localhost TAR]# tar -cf archive.tar file1.txt file3.txt

追加方法:

[root@localhost TAR]# tar -Af file.tar archive.tar
[root@localhost TAR]# tar -tf file.tar 
file2.txt
file3.txt
file1.txt
file1.txt
file3.txt

9, –test-label
測試歸檔卷標并退出

10, -u, –update
只追加最新的文件
示例:

[root@localhost TAR]# tar -tf file.tar 
file1.txt
file2.txt
[root@localhost TAR]# tar -uf file.tar file1.txt file3.txt file2.txt
[root@localhost TAR]# tar -tf file.tar 
file1.txt
file2.txt
file3.txt

11, -C, –directory=DIR
更改目錄到 DIR

例如:
提取文件到另外一個目錄

[root@localhost TAR]# tar -xvf file.tar -C /root/TAR2
file1.txt
file2.txt
[root@localhost TAR]# cd -
/root/TAR2
[root@localhost TAR2]# ll
total 28
-rw-r--r--. 1 root root 23250 Feb  7 23:11 file1.txt
-rw-r--r--. 1 root root   887 Feb  7 22:38 file2.txt

12, -p, –preserve-permissions
抽取文件時保留原有的文件權限

壓縮歸檔文件,使用 BZIP 和 GZIP 兩種方法

跟壓縮相關的參數

13, -j, –bzip2
使用 bzip2 對歸檔進行壓縮

示例:

[root@localhost TAR]# tar -jcf file.tar.bz file2.txt file1.txt
[root@localhost TAR]# ll
total 128
-rw-r--r--. 1 root root 23250 Feb  7 23:11 file1.txt
-rw-r--r--. 1 root root   887 Feb  7 22:38 file2.txt
-rw-r--r--. 1 root root 30720 Feb  7 23:30 file.tar
-rw-r--r--. 1 root root  1797 Feb  7 23:42 file.tar.bz

請看,上面的文件大小通過 BZIP 降低到 1797 字節。

14, -z, –gzip
使用 gzip 壓縮歸檔

示例:

[root@localhost TAR]# tar -zcf file.tar.gz file2.txt file1.txt
[root@localhost TAR]# ll
total 132
-rw-r--r--. 1 root root 23250 Feb  7 23:11 file1.txt
-rw-r--r--. 1 root root   887 Feb  7 22:38 file2.txt
-rw-r--r--. 1 root root 30720 Feb  7 23:30 file.tar
-rw-r--r--. 1 root root  1797 Feb  7 23:42 file.tar.bz
-rw-r--r--. 1 root root  1673 Feb  7 23:45 file.tar.gz
就這些了!
來自:http://crybit.com/tar-command-usages-with-examples/

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!