linux 硬链接 cp l,linux – cp -L与cp -H

问题

使用cp -H或cp -L复制文件时,我得到相同的结果:

$ls -l fileA

fileA -> fileB

$cp fileA somewhere/ -H

$ls -l somewhere/

fileA # fileA is a copy of fileB, only renamed, with same properties!

This answer here将两个选项描述为与-R组合使用的类似UNLESS.不适合我.软 – 硬链接文件成为它们在源指向的文件的重命名副本.

题:

cp -H和cp -L的正确用法是什么?这是预期的行为吗?

我试图解决:

man cp告诉我这两个选项完全一样,但是info cp的措辞让我更加困惑.也许有人可以帮我解决这个问题:

-H

If a command line argument specifies a symbolic link, then copy the file it points to rather than the symbolic link itself. However, copy (preserving its nature) any symbolic link that is encountered via recursive traversal.

这听起来像是一个矛盾:我想这是一个符号链接的本质«它指向某个地方……

-L, --dereference

Follow symbolic links when copying from them. With this option, cp cannot create a symbolic link. For example, a symlink (to regular file) in the source tree will be copied to a regular file in the destination tree.

我知道符号链接不是常规文件,但是……我承认我在这里解释过这个解释.

解决方法:

使用符号链接,工具有两件事可以做:

>将符号链接视为符号链接(“保留其性质”),或

>将符号链接视为它指向的文件类型.

说–H“保留其性质”并不矛盾.考虑替代方案.如果使用-L,将打开任何符号链接cp查找,并将其内容复制到目标文件名.所以源代码是符号链接,但它的副本不是符号链接.所以它“失去了作为符号链接的本质”.

考虑

$mkdir subdir

$echo "some contents" > subdir/file

$ln -s file subdir/link

# definition of "list", the abbreviated ls -l output used below

$list() { ls -l "$@" | \

awk '$0 !~ /^total/ { printf "%s %s\t%s %s %s\n", $1, $5, $9, $10, $11 }' ; }

$list subdir

-rw-rw-r-- 14 file

lrwxrwxrwx 4 link -> file

$cp -rH subdir subdir-with-H

$list subdir-with-H

-rw-rw-r-- 14 file

lrwxrwxrwx 4 link -> file

$cp -rL subdir subdir-with-L

$list subdir-with-L

-rw-rw-r-- 14 file

-rw-rw-r-- 14 link

标签:cp,linux,command-line,options,coreutils

来源: https://codeday.me/bug/20190808/1622869.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值