简体   繁体   English

在Mac OS X中复制符号链接

[英]Copying symbolic links in Mac OS X

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 我正在复制安装包的框架,并需要维护链接

As David mentioned, OS X is missing the handy -a option that gnu cp has. 正如David所说,OS X缺少gnu cp所具有的方便选项。

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. 应该工作。

In python you can use os.readlink and os.symlink to perform this action. 在python中,您可以使用os.readlinkos.symlink来执行此操作。 You should check if what you operate on is actually a symbolic link with os.lstat and stat.S_ISLNK 你应该检查你操作的是否实际上是os.lstatstat.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 . 你可以使用cp-R选项来完成它。 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 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 alias is not a symbolic link and therefore symbolic link specific treatment will fail on it. 请注意, macosx别名不是符号链接,因此符号链接特定处理将失败。

The solution of @brian d foy used to be correct. @brian d foy的解决方案曾经是正确的。 Newer versions of macOS do support 较新版本的macOS支持

cp -a

As you tagged python, i asume you mean something like copytree(src, dst[, symlinks]). 当你标记python时,我认为你的意思是像copytree(src,dst [,symlinks])。 Real symlinks (created by ln -s) will be copied as on any unix system. 真正的符号链接(由ln -s创建)将像任何unix系统一样被复制。 But if you create an alias with the finder, you wont get a symlink, but an alias. 但是如果你用finder创建一个别名,你就不会得到一个符号链接,而是一个别名。 The MacOS offers two types of links: unix type symlinks and aliases (see http://developer.apple.com/documentation/MacOSX/Conceptual/BPFileSystem/Articles/Aliases.html ). MacOS提供两种类型的链接:unix类型符号链接和别名(请参阅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. 这些别名不被许多工具视为链接 - 我所知道的都不是copytree。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM