Create a symbolic link of directory in Ubuntu

LinuxTerminalSymlink

Linux Problem Overview


Below is my code for creating a symlink of a directory:

sudo ln -s /usr/local/nginx/conf/ /etc/nginx

I already created the directory /etc/nginx. I just want the contents of the source directory (/usr/local/nginx/conf/) to be in the contents of the target directory (/etc/nginx). But when I execute the code, /etc/nginx contains a directory called conf, instead of the contents of conf. That directory contains the contents I want, but in the wrong location.

Why did it put a directory in the target folder, instead of just putting the contents of the directory in the target folder?

Linux Solutions


Solution 1 - Linux

This is the behavior of ln if the second arg is a directory. It places a link to the first arg inside it. If you want /etc/nginx to be the symlink, you should remove that directory first and run that same command.

Solution 2 - Linux

That's what ln is documented to do when the target already exists and is a directory. If you want /etc/nginx to be a symlink rather than contain a symlink, you had better not create it as a directory first!

Solution 3 - Linux

In script is useful something like this:

if [ ! -d /etc/nginx ]; then ln -s /usr/local/nginx/conf/ /etc/nginx > /dev/null 2>&1; fi

it prevents before re-create "bad" looped symlink after re-run script

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser959129View Question on Stackoverflow
Solution 1 - LinuxFatalErrorView Answer on Stackoverflow
Solution 2 - LinuxCeladaView Answer on Stackoverflow
Solution 3 - LinuxkaynView Answer on Stackoverflow