I have a symbolic link to a file in one directory. I would like to have that same link in another directory. How do I copy a symbolic link?
I tried to cp the symbolic link but this copies the file it points to instead of the symbolic link itself.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityUse cp -P
(capital P) to never traverse any symbolic link and copy the symbolic link instead.
This can be combined with other options such as -R
to copy a directory hierarchy — cp -RL
traverses all symbolic links to directories, cp -RP
copies all symbolic links as such. cp -R
might do one or the other depending on the unix variants; GNU cp
(as found on CentOS) defaults to -P
.
Even with -P
, you can copy the target of a symbolic link to a directory on the command line by adding a /
at the end: cp -RP foo/ bar
copies the directory tree that foo
points to.
GNU cp
has a convenient -a
option that combines -R
, -P
, -p
and a little more. It makes an exact copy of the source (as far as possible), preserving the directory hierarchy, symbolic links, permissions, modification times and other metadata.
cp
" is not the same thing as gcp
, which I use. gcp -P
is currently broken, even in 2019. (sigh).
Jun 21, 2019 at 19:46
Check this answer https://superuser.com/a/315757/53590 for a specifically CentOS take on the problem. The whole question might help you but the bit at the bottom is specifically CentOS.
cp
on CentOS support-P
or--no-dereference
switch?