Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Say under directory /usr/local/boost_1_52_0/stage/lib, I have a symbolic link: libboost_timer.so, which points to the regular file libboost_timer.so.1.52.0 in the same directory.

How can I cp the symbolic links to directory /usr/local/lib while pointing to the same file /usr/local/boost_1_52_0/stage/lib/libboost_timer.so.1.52.0.

I have searched and tried several options. First one copies the regular file to the destination directory using the name of the link. Second one just copies the link itself, but the link is not valid in the new directory. Third one makes new link points to the original link, so it would de-reference twice to get the regular file. And I want to use the link file name, so directly making the links of the regular file would not work.

How can I do this?

share|improve this question
What is wrong with dereferencing twice? – Chris Down Jan 17 '13 at 8:20
@ChrisDown If there's no simple way to do this, then I'll stick to de-referencing twice. – WiSaGaN Jan 17 '13 at 8:37

The best way is to dereference twice, but I guess the fact that you mentioned it in your question implies that it isn't any good for your use case (why?).

The second best option you have in a standard bash shell is to use readlink -f (note, this will break if the filename contains trailing newlines, which is extremely unlikely, but possible).


fileLinkedTo=$(readlink -f "$src")
if [[ -d "$dest" ]]; then
    ln -s "$fileLinkedTo" "${dest%/}/${src##*/}"
    ln -s "$fileLinkedTo" "$dest"

And then run it as:

./script /usr/local/boost_1_52_0/stage/lib/libboost_timer.so /usr/local/lib
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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