Macos cp-a无法覆盖符号链接目录
我有一个包含符号链接文件夹的文件夹Macos cp-a无法覆盖符号链接目录,macos,bash,symlink,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
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-fR
和cp-fa
)
我认为这是一个测试,通过跟踪符号链接来查看当前文件夹是否是一个文件夹,然后无法用符号链接覆盖符号链接(它认为它是一个文件夹)
使用符号链接的文件夹一致地复制和覆盖文件夹的正确命令是什么?这可能不是确切的答案,但可能有助于理解您到底在寻找什么。以下是我得到的:
# 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
希望它能帮助…
它具有与osx复制/粘贴相同的行为
附言。 您可能需要注意的一个问题:
cp -a foo bar
将文件夹foo/移动到bar/(即bar/foo/file1、bar/foo/file2)
将文件夹foo/的内容移动到bar(即bar/file1,bar/file2)我不明白的地方。如果第一次保留符号链接,为什么需要覆盖?如果您查看复制的符号链接文件夹,您没有看到其中的更新文件吗?我会更新一个新版本,比如说版本3,然后需要将符号链接指向新版本。另一件我不明白的事情是,进入finder并复制和粘贴是正确的行为。
ditto foo bar