cp -a无法覆盖符号链接目录

时间:2013-08-21 19:18:09

标签: macos bash symlink

我有一个包含符号链接文件夹的文件夹。

root
 |- Current document -> version 2 document
 |- Current folder -> version 2 folder
 |- Archives
     |- version 1 document
     |- version 1 folder
         |- ...
     |- version 2 document
     |- version 2 folder
         |- ...

当我使用cp -r复制此目录时,文件夹会复制,但由于-r跟随符号链接,因此版本2会被复制两次。

当我使用cp -R复制此目录时,该文件夹首次正常复制并保留符号链接。但是,在第二个副本上,它无法覆盖该文件夹,说明:

cp: cannot overwrite directory 'Current folder' with 'Current folder'

我还尝试了cp -a == cp -pPR以及-f版本(cp -fRcp -fa

我认为通过遵循符号链接来查看Current Folder是否是文件夹,然后无法用符号链接覆盖符号链接(它认为是文件夹)。

使用符号链接文件夹一致地复制和覆盖文件夹的正确命令是什么?

2 个答案:

答案 0 :(得分:4)

On OSX, use ditto

它与osx复制/粘贴具有相同的行为。


P.S。 你可能需要注意的一个问题是:

cp -a foo bar

将文件夹foo /移动到bar /(即bar / foo / file1,bar / foo / file2)

ditto foo bar

将文件夹foo /的内容移动到栏中(即bar / file1,bar / file2)

答案 1 :(得分:0)

这可能不完全是答案,但可能有助于了解您正在寻找什么。这是我得到的:

# Assume all these happening in a parent directory name pdir.

mkdir -p test/s
mkdir -p test1/s1
cd test/s
ln -s ../../test1/s1 .  # created a symlink

# go to parent dir pdir

mkdir -p test2
cp -R test/* test2/     # Now I copy all the content of test to test2. test contains a symlink directory

ls -ld test2/s/*
18 Aug 21 14:53 test2/s/s1@ -> ../../test1/s1  # symlink dir is preserved during the copy

# Now I want to modify my source directory before copying again
# This time I will modify inside the source directory which I have already symlinked

touch test1/s1/test.txt

# Without copying I check that the symlink is correctly updated, I don't even need a copy anymore

ls -ld test2/s/s1/*
0 Aug 21 14:55 test2/s/s1/test.txt


# Now I want to create a symlink inside the source symlink directory
cd test1/
touch tmp1.txt
cd s1/
ln -s ../tmp1.txt .  # Here it is created

# go back to parent dir pdir
# Do the same copy again

cp -R test/* test2/  

# You will receive this error:  
cp: cannot overwrite directory test2/stest/stest1 with non-directory test/stest/stest1
#of course it can't because it is already there
# even though it complains it can't overwrite the symlink of the dir, 
# but it correctly updates the files that are recently created inside the source dir

ls -ld test2/s/s1/*
 0 Aug 21 14:55 test2/s/s1/test.txt
 10 Aug 21 14:59 test2/s/s1/tmp1.txt@ -> ../tmp1.txt

希望它有所帮助...

相关问题