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

本文介绍如何在使用tar命令打包备份数据时,确保备份的是源文件而非符号链接文件,通过使用-tar命令的-h参数实现此功能,并提供实例演示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文链接: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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值