linux中的tar命令

[root@localhost ~]# tree test
test
├── file1.txt

└── file2.txt


压缩:tar -jcv -f 压缩后的文件名 被压缩的文件名

[root@localhost ~]# tar -jcv -f test.tar.bz2

tar: 谨慎地拒绝创建空归档文件
请用“tar --help”或“tar --usage”获得更多信息。

为啥报错?压缩格式:tar -jcv -f 压缩后的文件名 被压缩的文件名。

而tar -jcv -f test.tar.bz2缺少被压缩文件名。补上。

[root@localhost ~]# tar -jcv -f test.tar.bz2 test
test/
test/file2.txt

test/file1.txt


查询:tar -jtv -f filename.tar.bz2

[root@localhost ~]# tar -jtv -f test.tar.bz2
drwxr-xr-x root/root         0 2018-05-11 21:43 test/
-rw-r--r-- root/root         6 2018-05-11 21:43 test/file2.txt

-rw-r--r-- root/root         6 2018-05-11 21:42 test/file1.txt


解压缩:tar -jxv -f filename.tar.bz2 -C 。欲解压目录

[root@localhost ~]# tar -jxv -f test.tar.bz2
test/
test/file2.txt
test/test/
test/test/file2.txt
test/test/file1.txt

test/file1.txt


重头戏:利用tar进行数据备份时,备份文件不应该包含绝对路径。为什么不能包含绝对路径。

[root@localhost /]# cd test/
[root@localhost test]# ll
总用量 8
-rw-r--r--. 1 root root 6 5月  11 21:42 file1.txt
-rw-r--r--. 1 root root 6 5月  11 21:43 file2.txt
[root@localhost test]# tar -jcv -f file.tar.bz2 /test/file1.txt
tar: 从成员名中删除开头的“/”
/test/file1.txt

注意哦,被压缩文件带有绝对路径,那么你解压时文件也是按照你压缩时的路径进行放置的。当我解压时,被解压的文件就会放置在/test/下,那么它就会覆盖/test/下的同名文件。记住:系统文件的放置是按照绝对路径放置的。这就意味着,假如你解压的文件就是绝对路径,那么很可能会覆盖系统的同名文件。


实际中,即便你将欲压缩文件写成绝对路径,系统也会将/根路径删除绝对路径。所以这个问题系统自动帮你解决好啦。但是,知之者,不如乐知者。

[root@localhost test]# tar -jcv -f file.txt
tar: 谨慎地拒绝创建空归档文件
请用“tar --help”或“tar --usage”获得更多信息。
[root@localhost test]# tar -jcv -f file.tar.bz2 /test/file.txt
tar: 从成员名中删除开头的“/”
/test/file.txt
[root@localhost test]# ll
总用量 8
-rw-r--r--. 1 root root 134 5月  11 23:15 file.tar.bz2
-rw-r--r--. 1 root root  12 5月  11 23:06 file.txt
[root@localhost test]# tar -jtv -f file.tar.bz2
-rw-r--r-- root/root        12 2018-05-11 23:06 test/file.txt

猜你喜欢

转载自blog.csdn.net/qq_23143555/article/details/80280436
今日推荐