How to copy symbolic links?
cp --preserve=links
From the man page:
--preserve[=ATTR_LIST]
preserve the specified attributes (default: mode,owner-
ship,timestamps), if possible additional attributes: context,
links, xattr, all
Personally, I use cp -av
for most of my heavy copying. That way, I can preserve everything - even recursively - and see the output. Of course, that is just personal preference.
As to why your other options did not do what you expected, -s
makes a link instead of copying and -L
follows the links in the source to find the file to copy instead of copying the links themselves.
Just as the man page says, use -P
. This setting says:
-P, --no-dereference
never follow symbolic links in SOURCE
If the links contain relative paths, then, copying the link will not adjust the relative path.
Use readlink
, with the switch -f
to follow recursively, in order to get the absolute path of the link. For example:
ln -s $(readlink -f old/dir/oldlink) new/dir/newlink
If preserving the relative paths is what you want, than the option -P
of cp
, as said by Ignacio Vazquez-Abrams, is what you need.