2013-08-21 53 views
3

我有一個包含符號鏈接文件夾的文件夾。cp -a無法覆蓋符號鏈接目錄

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是否是一個文件夾,然後無法用符號鏈接覆蓋符號鏈接(它認爲它是一個文件夾)。

什麼是正確的命令來一致地複製和覆蓋與符號鏈接文件夾的文件夾?

+0

我不理解的東西。如果第一次保留符號鏈接,爲什麼你需要覆蓋?如果你查看複製的符號鏈接文件夾,你沒有看到更新的文件在那裏...? – iamauser

+0

我將使用新版本進行更新,如版本3,然後需要在新版本中指向符號鏈接。 – kels

+0

另一件我不明白的事情是進入查找器並複製粘貼是正確的行爲。 – kels

回答

4

On OSX, use ditto

它具有相同的行爲OSX複製/粘貼。


P.S.你可能要注意 有一個問題:

cp -a foo bar 

將移動文件夾富/成棒/(即酒吧/富/文件1,酒吧/富/文件2)

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/[email protected] -> ../../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/[email protected] -> ../tmp1.txt 

希望它可以幫助...