您的位置:首页 > 其它

使用tar命令打包文件时,如何将符号链接文件替换为源文件

2016-04-01 11:00 477 查看
原文链接:http://www.yuanzhichina.com/show.asp?id=3989

使用tar命令打包文件时,如何将符号链接文件替换为源文件

 

问题描述:

我们在用tar命令打包备份数据的时候,某些情况下希望备份的是源文件,而不是符号链接文件,因为符号链接文件并不包含真实的文件内容。这时该如何操作?

 

解答:

使用“-h”的参数可以实现这个要求,它会把符号链接文件视作普通文件或目录,从而打包的是源文件。

 

# man tar

-h

Forces the tar command to follow symbolic links as if they were

normal files or directories. Normally, the tar command does not

follow symbolic links.

 

举例如下:

 

myhost:/tmp/link#ls -l

total 0

lrwxrwxrwx 1 root system 9 Mar 31 22:34 testfile -> /smit.log

myhost:/tmp/link#ls -l /smit.log

-rw-r--r-- 1 root system 691 Mar 31 22:31 /smit.log

 

myhost:/tmp/link#tar -cvf test.tar testfile

a testfile symbolic link to /smit.log.

myhost:/tmp/link#tar -tvf test.tar

?rwxrwxrwx 0 0 0 Mar 31 22:43:14 2009 testfile symbolic link to /smit.log

.

 

myhost:/tmp/link#tar -h -cvf test1.tar testfile

a testfile 2 blocks.

myhost:/tmp/link#tar -tvf test1.tar

-rw-r--r-- 0 0 691 Mar 31 22:31:16 2009 testfile

 

另外请注意,“cp”命令也有“-h”的参数,但定义恰好相反,它会拷贝符号链接本身而不是源文件,不加“-h”参数的时候cp命令默认拷贝源文件。

# man cp

-h

Forces the cp command to copy symbolic links. The default is to

follow symbolic links, that is, to copy files to which symbolic

links point.

 

举例如下:

 

myhost:/tmp/link#cp testfile newfile

myhost:/tmp/link#ls -l

total 8

-rw-r--r-- 1 root system 691 Mar 31 22:59 newfile

lrwxrwxrwx 1 root system 9 Mar 31 22:50 testfile -> /smit.log

 

myhost:/tmp/link#cp -h testfile newfile1

myhost:/tmp/link#ls -l

total 8

-rw-r--r-- 1 root system 691 Mar 31 22:59 newfile

lrwxrwxrwx 1 root system 9 Mar 31 23:00 newfile1 -> /smit.log

lrwxrwxrwx 1 root system 9 Mar 31 22:50 testfile -> /smit.log
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  tar link