cp -a不能覆盖符号链接的目录。

4

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

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 -acp -pPR,以及带有 -f 的版本 (cp -fRcp -fa)。
我认为它是通过跟随符号链接测试 当前文件夹 是否为文件夹,然后失败地试图用符号链接覆盖符号链接(它认为这是一个文件夹)。
什么是正确的命令来持续地复制和覆盖一个具有符号链接的文件夹?

有些事我不太明白。如果第一次保留符号链接,为什么还需要覆盖呢?如果你查看复制的符号链接文件夹,难道不会看到其中更新后的文件吗……? - iamauser
我将会更新一个新版本,比如说版本3,然后需要将符号链接指向新版本。 - kels
我不理解的另一件事是,进入Finder并进行复制和粘贴是否是正确的行为。 - kels
2个回答

6

在OSX上,使用ditto

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


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

cp -a foo bar

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

ditto foo bar

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


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

希望能有所帮助...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接