-1

How can I copy a folder that contains symlinks and retain the symlinks in the destination folder? I'm doing something like this with PHP/Bash:

system("cp -r production-clone-target production-sites/{$instanceName}");

but the symlinks do not appear in the destination folder.

  • 1
    On BSD: cp -PR source destination. On Linux: cp -dr source destination. However, with PHP there are probably much better approaches than running system() (why do you have system() enabled, in the first place?). – Satō Katsura Oct 21 '16 at 7:39
  • You mean from a security standpoint? – shmuli Oct 21 '16 at 7:43
  • I'd bet many things that the answer is yes. See stackoverflow.com/questions/3115559/exploitable-php-functions – schaiba Oct 21 '16 at 7:48
  • 1
    This seems to be a far more reasonable solution. – Satō Katsura Oct 21 '16 at 7:49
2

Try adding the --preserve=links switch to your cp command.

From man cp:

    --preserve[=ATTR_LIST]
          preserve the specified attributes (default: mode,ownership,timestamps), 
if possible additional attributes: context, links,xattr, all

Edit: If under OS X; use cp -a.

  • 1
    Assuming Linux FTW. – Satō Katsura Oct 21 '16 at 7:39
  • @SatoKatsura True, fixed! – maulinglawns Oct 21 '16 at 7:41
  • 1
    On OpenBSD: "cp: unknown option -- a" – Satō Katsura Oct 21 '16 at 7:42
  • @SatoKatsura Huh! Didn't know that, thanks for the info, just tested under OS X, where the -a switch works. Thought it was the same for all BSD's. – maulinglawns Oct 21 '16 at 7:44
0

Setting aside the valid concerns about using system() in your PHP program:

cp may not have standardized options for doing this that are common to Linux and the BSDs, but pax has.

( cd source && pax -p p -r -w * destination/ )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.