[linux] Create a symbolic link of directory in Ubuntu

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?

This question is related to linux terminal symlink

The answer is


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!


In script is usefull 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


Examples related to linux

grep's at sign caught as whitespace How to prevent Google Colab from disconnecting? "E: Unable to locate package python-pip" on Ubuntu 18.04 How to upgrade Python version to 3.7? Install Qt on Ubuntu Get first line of a shell command's output Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is the docker daemon running? Run bash command on jenkins pipeline How to uninstall an older PHP version from centOS7 How to update-alternatives to Python 3 without breaking apt?

Examples related to terminal

Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Can't compile C program on a Mac after upgrade to Mojave Flutter command not found VSCode Change Default Terminal How to switch Python versions in Terminal? How to open the terminal in Atom? Color theme for VS Code integrated terminal How to edit a text file in my terminal How to open google chrome from terminal? Switch between python 2.7 and python 3.5 on Mac OS X Nginx sites-enabled, sites-available: Cannot create soft-link between config files in Ubuntu 12.04 How to see full absolute path of a symlink Create a symbolic link of directory in Ubuntu How do I find all of the symlinks in a directory tree? Apache won't follow symlinks (403 Forbidden) Git Symlinks in Windows How to check if a symlink exists How can I symlink a file in Linux? Can you change what a symlink points to after it is created? How does Git handle symbolic links?