bash 在另一个目录中创建链接时,符号链接不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8601988/
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-09-18 01:12:29  来源:igfitidea点击:

Symlinks not working when link is made in another directory?

bashsymlink

提问by user225643

Wow, I've never really used symlinks that much before, but this is really boggling:

哇,我以前从未真正使用过那么多符号链接,但这真的令人难以置信:

bash-3.2$ echo "weird" > original.txt
bash-3.2$ mkdir originals
bash-3.2$ mv original.txt originals/
bash-3.2$ cat originals/original.txt 
weird
bash-3.2$ mkdir copies
bash-3.2$ ln -s originals/original.txt copies/copy.txt
bash-3.2$ cat copies/copy.txt 
cat: copies/copy.txt: No such file or directory
bash-3.2$ ls copies/copy.txt 
copies/copy.txt
bash-3.2$ ls -l copies/copy.txt 
lrwxr-xr-x  1 zach  staff  22 Dec 22 01:23 copies/copy.txt -> originals/original.txt
bash-3.2$ cat originals/original.txt 
weird
bash-3.2$ cat copies/copy.txt 
cat: copies/copy.txt: No such file or directory
bash-3.2$ cd copies/
bash-3.2$ cat copy.txt 
cat: copy.txt: No such file or directory

Why can't I cat the symlink in the copies directory?

为什么我不能在副本目录中找到符号链接?

If I make the symlink from inside the copies/, I can cat it just fine. If I make the symlink in the current directory, I can also cat it just fine. If I make the symlink in the current directory and then move it to copies/, I get "copies/copy.txt: No such file or directory".

如果我从副本/内部制作符号链接,我可以很好地捕捉它。如果我在当前目录中创建符号链接,我也可以很好地捕捉它。如果我在当前目录中创建符号链接,然后将其移动到副本/,我会得到“副本/copy.txt:没有这样的文件或目录”。

回答by kev

If you create a relative path to a symbolic link, it will store it as a relative symbolic link. Symbolic links are relative to the location the link is in, not the location where it was created or opened.

如果创建符号链接的相对路径,它会将其存储为相对符号链接。符号链接与链接所在的位置相关,而不是与创建或打开链接的位置相关。



Please use absolute path or path relative to the link.

请使用绝对路径或相对于链接的路径。

Change:

改变:

ln -s originals/original.txt copies/copy.txt

To:

到:

# absolute
ln -s /path/to/originals/original.txt copies/copy.txt

# relative
cd copies
ln -s ../originals/original.txt copy.txt

回答by Raghuram

You can also use relative path to achieve this. like

您也可以使用相对路径来实现这一点。喜欢

cd copies
ln -s ../originals/original.txt copy.txt

This will work

这将工作

when you open the symbolic link which it tries to refer to the file from the copies directory and since that doesn't exist you are getting that error.

当您打开它尝试从副本目录中引用文件的符号链接时,由于该文件不存在,您会收到该错误。

When you use relative or absolute path this problem will get solved.

当您使用相对或绝对路径时,这个问题将得到解决。

回答by Prasad Kudalkar

Consider an example where you want to symlink your application logs to somewhere else which may be a directory which is mounted to a secondary directory whose size won't effect your server to stop working. Now the mounted directory will be target for you and you should create a symlink to this directory for your logs directory.

考虑一个示例,您希望将应用程序日志符号链接到其他位置,该位置可能是挂载到二级目录的目录,其大小不会影响您的服务器停止工作。现在挂载的目录将成为您的目标,您应该为您的日志目录创建一个指向该目录的符号链接。

Suppose your application directory is /home/ubuntu/my_app and once you start your application it will generate a log directory inside your my_app directory. Now the end goal is to divert the disk usage burden to the mounted directory and currently don't have our log directory present in our app directory. So just go ahead and follow the below steps:

假设您的应用程序目录是 /home/ubuntu/my_app,一旦您启动您的应用程序,它将在您的 my_app 目录中生成一个日志目录。现在的最终目标是将磁盘使用负担转移到挂载的目录,目前我们的应用程序目录中没有我们的日志目录。因此,请继续并按照以下步骤操作:

mkdir /path_to_mounted_directory/log
ln -s /path_to_mounted_directory/log /home/ubuntu/my_app_log

This will first create a directory named log in mounted section and will map your application logs to this directory. Any file you add in the application log folder will be linked to the mounted directory automatically and thus you can read these files fro anywhere you want, either from the original logs directory or from the mounted folder.

这将首先在挂载部分创建一个名为 log 的目录,并将您的应用程序日志映射到该目录。您在应用程序日志文件夹中添加的任何文件都将自动链接到挂载目录,因此您可以从原始日志目录或挂载文件夹中的任何位置读取这些文件。