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 -a==cp -pPR以及-f版本(cp -fRcp -fa

我认为通过跟踪符号链接来测试是否Current Folder是一个文件夹,然后无法用符号链接覆盖符号链接(它认为它是一个文件夹)。

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

4

2 回答 2

5

在 OSX 上,使用ditto

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


PS 您可能需要注意的一个问题:

cp -a foo bar

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

ditto foo bar

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

于 2013-08-21T22:30:11.130 回答
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

希望能帮助到你...

于 2013-08-21T20:11:04.117 回答