Chinaunix首页 | 论坛 | 博客
  • 博客访问: 100133
  • 博文数量: 32
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 75
  • 用 户 组: 普通用户
  • 注册时间: 2015-07-16 12:56
文章分类

全部博文(32)

文章存档

2017年(7)

2016年(25)

我的朋友

分类: LINUX

2016-04-01 11:04:02

原文链接

使用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
    注意:—h 参数不能够与 -cvf 一起连在一起,否则不能够正常工作

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

阅读(1598) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~