19

Is there a proper way to link /home/user/app/public to /home/user/public_html, considering the fact that the target already exists?

If I do ln -s /home/user/app/public /home/user/public_html, I end up having /home/user/public_html/public.

I guess it would be possible also to inverse the action such as ln -s /home/user/public_html /home/user/app/public, however I'm not sure how to avoid /home/user/app/public/public_html as the end result.

17

Lemme understand your issue in the right way

Your source is /home/user/public_html and I assume it has files in there. And you want to make a link to this directory right in /home/user/app/public. In this way then, you could do something like ls /home/user/app/public and see the files on that exist on /home/user/public_html.

If this is correct, then your source is /home/user/public_html and the target is /home/user/app/public. The proper command is this one:

ln -s /home/user/public_html /home/user/app/public

Just make sure that /home/user/app/public isn't a valid directory or file previous enter the command.

  • 3
    This is an old one, but I think the user asked exactly how to do what you describe, but, in the case that /home/user/app/public may already exist (which is exactly the caveat you mention at the end of your answer). So IMHO this doesn't answer the original question as the user wanted to know precisely how to deal with an already existing target (ie. how to override it with the symlink, without having to delete it prior to creating the symlink). – jotadepicas May 31 '18 at 18:55
  • Try it -T option :) – Milkmannetje Nov 21 '18 at 16:51
2

If I understand your question correctly, you wanted to know how to deal with already existing targets. If that's the case, there are two possible scenarios.

1) the existing target is a file. If this is the case, you can force the creation of the symlink with "-f"

2) the existing target is a directory. If this is the case, then it depends on your current implementation of the ln command. It could have the "-n" argument available, which causes symlink to directories be handled properly, instead of duplicating them as you say.

Reference: https://unix.stackexchange.com/questions/207294/create-symlink-overwrite-if-one-exists

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.