我是靠谱客的博主 能干汽车,最近开发中收集的这篇文章主要介绍使用tar命令打包文件时,如何将符号链接文件替换为源文件,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

原文链接: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命令打包文件时,如何将符号链接文件替换为源文件的全部内容,希望文章能够帮你解决使用tar命令打包文件时,如何将符号链接文件替换为源文件所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部