macos 在 Mac OS X 中复制符号链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/221267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copying symbolic links in Mac OS X
提问by David Sykes
What is the simplest way of copying symbolic links on the Mac?
在 Mac 上复制符号链接的最简单方法是什么?
A python or perl solution would be preferred, but any solution would be a help.
首选 python 或 perl 解决方案,但任何解决方案都会有所帮助。
I am copying frameworks for an installation package, and need the links to be maintained
我正在复制安装包的框架,需要维护链接
回答by Mark Baker
As David mentioned, OS X is missing the handy -a option that gnu cp has.
正如大卫所提到的,OS X 缺少 gnu cp 具有的方便的 -a 选项。
However, if you use -R to do a recursive copy, then it will copy symlinks by default, so
但是,如果您使用 -R 进行递归复制,则默认情况下它将复制符号链接,因此
cp -R source destination
ought to work.
应该工作。
回答by n1000
The solution of @brian d foyused to be correct. Newer versions of macOS do support
@brian d foy的解决方案曾经是正确的。较新版本的 macOS 确实支持
cp -a
回答by Florian B?sch
In python you can use os.readlinkand os.symlinkto perform this action. You should check if what you operate on is actually a symbolic link with os.lstatand stat.S_ISLNK
在 python 中,您可以使用os.readlink和os.symlink来执行此操作。您应该检查您操作的内容是否实际上是与os.lstat和stat.S_ISLNK的符号链接
import os, stat
if stat.S_ISLNK(os.lstat('foo').st_mode):
src = os.readlink('source')
os.symlink(src, 'destination')
You could do it with the -R option of cp. This works because cp by default does not follow symbolic links but barks at copying non-files without specifying -R which means recursion.
您可以使用cp的-R 选项来完成。这是有效的,因为 cp 默认情况下不遵循符号链接,而是在不指定 -R 的情况下复制非文件,这意味着递归。
cp -R source destination
In python that would be with the subprocess.call
在 python 中,这将与subprocess.call
from subprocess import call
call(['cp', '-R', 'source', 'destination'])
Note that a macosx aliasis not a symbolic link and therefore symbolic link specific treatment will fail on it.
请注意,macosx 别名不是符号链接,因此符号链接特定处理将失败。
回答by Arne Burmeister
As you tagged python, i asume you mean something like copytree(src, dst[, symlinks]). Real symlinks (created by ln -s) will be copied as on any unix system. But if you create an alias with the finder, you wont get a symlink, but an alias. The MacOS offers two types of links: unix type symlinks and aliases (see http://developer.apple.com/documentation/MacOSX/Conceptual/BPFileSystem/Articles/Aliases.html). These aliases are not treated as links by many tools - neither copytree as i know.
当你标记 python 时,我假设你的意思是像 copytree(src, dst[, symlinks])。真正的符号链接(由 ln -s 创建)将像在任何 unix 系统上一样被复制。但是,如果您使用查找器创建别名,您将不会获得符号链接,而是获得别名。MacOS 提供两种类型的链接:unix 类型符号链接和别名(请参阅http://developer.apple.com/documentation/MacOSX/Conceptual/BPFileSystem/Articles/Aliases.html)。许多工具不会将这些别名视为链接——据我所知,也不是 copytree。