tags:

views:

1078

answers:

3

What is the simplest way of copying symbolic links on the Mac?

A python or perl solution would be preferred, but any solution would be a help.

I am copying frameworks for an installation package, and need the links to be maintained

+2  A: 

In python you can use os.readlink and os.symlink to perform this action. You should check if what you operate on is actually a symbolic link with os.lstat and 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 source destination

In python that would be with the 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.

Florian Bösch
A: 

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.

Arne Burmeister
I tagged python because it was my preferred scripting language, but it turns out cp does the job quite simply, thanks
David Sykes
+3  A: 

As David mentioned, OS X is missing the handy -a option that gnu cp has.

However, if you use -R to do a recursive copy, then it will copy symlinks by default, so

cp -R source destination

ought to work.

Mark Baker