Linux 在 Ubuntu 中创建目录的符号链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9104337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:19:20  来源:igfitidea点击:

Create a symbolic link of directory in Ubuntu

linuxterminalsymlink

提问by

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/nginxcontains 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?

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

采纳答案by FatalError

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

这是ln如果第二个 arg 是目录的行为。它把一个链接到第一个参数里面。如果您想/etc/nginx成为符号链接,您应该先删除该目录并运行相同的命令。

回答by Celada

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

ln当目标已经存在并且是一个目录时,这就是记录要做的事情。如果你想/etc/nginx成为一个符号,而不是包含一个符号,你最好不要将其创建为一个目录第一!

回答by kayn

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

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