在Ubuntu中创建一个目录的符号链接。

时间:2022-05-02 16:27:57

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.

我已经创建了目录/etc/nginx。我只是希望源目录(/usr/local/nginx/conf/)的内容位于目标目录(/etc/nginx)的内容中。但是当我执行代码时,/etc/nginx包含一个名为conf的目录,而不是conf的内容。

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

为什么它把目录放在目标文件夹中,而不是把目录的内容放在目标文件夹中?

3 个解决方案

#1


231  

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.

这是ln的行为,如果第二个arg是一个目录。它在里面放置一个链接到第一个arg。如果您希望/etc/nginx是symlink,您应该首先删除该目录并运行相同的命令。

#2


27  

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!

当目标已经存在且是目录时,ln就是这么做的。如果您希望/etc/nginx是一个符号链接,而不是包含一个符号链接,您最好先不要将它创建为目录!

#3


8  

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

它可以防止在重新运行脚本后重新创建“坏”循环符号链接

#1


231  

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.

这是ln的行为,如果第二个arg是一个目录。它在里面放置一个链接到第一个arg。如果您希望/etc/nginx是symlink,您应该首先删除该目录并运行相同的命令。

#2


27  

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!

当目标已经存在且是目录时,ln就是这么做的。如果您希望/etc/nginx是一个符号链接,而不是包含一个符号链接,您最好先不要将它创建为目录!

#3


8  

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

它可以防止在重新运行脚本后重新创建“坏”循环符号链接