Skip to content
Advertisement

How to copy a folder containing absolute symlinks and preserve their relative targets

Suppose I have a folder: /usr/lib/x86_64-linux-gnu/wxcrafter containing some library files and their corresponding symlinks, e.g:

/usr/lib/x86_64-linux-gnu/wxcrafter/lib1.so.0 -> /usr/lib/x86_64-linux-gnu/wxcrafter/lib1.so.0.0.0

/usr/lib/x86_64-linux-gnu/wxcrafter/lib2.so.0 -> /usr/lib/x86_64-linux-gnu/wxcrafter/lib2.so.0.0.0

I want to make a copy of wxcrafter to a new location codeblocks, preserving the relative location of the symlink targets:

usr/lib/x86_64-linux-gnu/codeblocks/lib1.so.0 -> /usr/lib/x86_64-linux-gnu/codeblocks/lib1.so.0.0.0

/usr/lib/x86_64-linux-gnu/codeblocks/lib2.so.0 -> /usr/lib/x86_64-linux-gnu/codeblocks/lib2.so.0.0.0

How do I do this on the command line, given that there may be a number of such symlinks?

Advertisement

Answer

Use the symlinks program to convert your absolute symlinks into relative ones:

symlinks -c  wxcrafter

then copy the entire directory:

cp -a wxcrafter codeblocks

If you must use absolute symlinks (but why would you?), make a copy of the original directory before running symlinks and restore it after you’re done.

The copy (codeblocks) will have relative links, if you need them absolute you’ll have to write a little script (using ln -fs $(pwd)/$(readlink $l) $l to convert any symlink $l from relative to absolute), as symlinks only converts in one direction. But, again, why bother?

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement