symlink to already existing directory

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.


Solution 1:

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.

Solution 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