4月23日 武汉 OSC 源创会 —— ES6开发体系实践>>> » 
tar 命令用来将很多文件打包成一个单一的磁带或者磁盘归档,并可从归档文件恢复出文件列表。当你需要通过 email 发送大量文件时或者传输文件时非常有用。这里我们介绍一些基本的使用场景。
tar 的语法:
|
1
|
# tar [options] file.tar file1 file2 .. .. .. |
file.tar 是 tar 归档文件,而其他 file1 和 file2 等等是要被打包的文件。
例如我们有两个文件 file1.txt 和 file2.txt
|
1
2
3
4
|
[root@localhost TAR]# lltotal 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 文件
语法:
|
1
|
# tar -cf archive.tar files .. .. |
示例:
|
1
2
3
|
[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 文件中的所有文件列表
|
1
|
# tar -tf archive.tar |
示例:
|
1
2
3
|
[root@localhost TAR]# tar -tf file.tar file1.txtfile2.txt |
从 tar 中提取所有文件
|
1
|
tar -xf archive.tar |
示例
|
1
2
3
4
5
6
|
[root@localhost TAR]# tar -xf file.tar [root@localhost TAR]# lltotal 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:
|
1
|
tar -tvf archive.tar |
Example:
|
1
2
3
|
[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:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[root@localhost TAR]# tar -tf file.tar file2.txtfile3.txtfile1.txt[root@localhost TAR]# tar -df file.tar file1.txt file2.txt file4.txttar: file4.txt: Not found in archivetar: Exiting with failure status due to previous errors----Verbosely----[root@localhost TAR]# tar -dvf file.tar file1.txt file2.txt file2.txtfile1.txt[root@localhost TAR]# tar -dvf file.tar file1.txt file2.txt file6.txtfile2.txtfile1.txttar: file6.txt: Not found in archivetar: Exiting with failure status due to previous errors |
6, –delete
从归档中删除某文件
示例:
从归档 file.tar 中删除 file1.txt
|
1
2
3
|
[root@localhost TAR]# tar --delete -f file.tar file1.txt [root@localhost TAR]# tar -tf file.tarfile2.txt |
7, -r, –append
追加文件到归档中
示例:
追加 file3.txt 到 file.tar
|
1
2
3
4
5
|
[root@localhost TAR]# tar -rf file.tar file3.txt[root@localhost TAR]# tar -tf file.tarfile1.txtfile2.txtfile3.txt |
8, -A, –catenate, –concatenate
将一个tar 归档追加到另外一个归档文件中
创建另外一个 tar 文件
|
1
|
[root@localhost TAR]# tar -cf archive.tar file1.txt file3.txt |
追加方法:
|
1
2
3
4
5
6
7
|
[root@localhost TAR]# tar -Af file.tar archive.tar[root@localhost TAR]# tar -tf file.tar file2.txtfile3.txtfile1.txtfile1.txtfile3.txt |
9, –test-label
测试归档卷标并退出
10, -u, –update
只追加最新的文件
示例:
|
1
2
3
4
5
6
7
8
|
[root@localhost TAR]# tar -tf file.tar file1.txtfile2.txt[root@localhost TAR]# tar -uf file.tar file1.txt file3.txt file2.txt[root@localhost TAR]# tar -tf file.tar file1.txtfile2.txtfile3.txt |
11, -C, –directory=DIR
更改目录到 DIR
例如:
提取文件到另外一个目录
|
1
2
3
4
5
6
7
8
9
|
[root@localhost TAR]# tar -xvf file.tar -C /root/TAR2file1.txtfile2.txt[root@localhost TAR]# cd -/root/TAR2[root@localhost TAR2]# lltotal 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 对归档进行压缩
示例:
|
1
2
3
4
5
6
7
|
[root@localhost TAR]# tar -jcf file.tar.bz file2.txt file1.txt[root@localhost TAR]# lltotal 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 压缩归档
示例:
|
1
2
3
4
5
6
7
8
|
[root@localhost TAR]# tar -zcf file.tar.gz file2.txt file1.txt[root@localhost TAR]# lltotal 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://www.cnblogs.com/grit/p/5396879.html