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

发布于 2024-12-22 14:13:09 字数 895 浏览 3 评论 0原文

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

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

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

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

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".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

口干舌燥 2024-12-29 14:13:09

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


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

更改:

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

至:

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

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

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
独享拥抱 2024-12-29 14:13:09

您还可以使用相对路径来实现此目的。

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

当您打开符号链接时,这将起作用

它会尝试引用副本目录中的文件,并且由于该文件不存在,因此您会收到该错误。

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

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.

坦然微笑 2024-12-29 14:13:09

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

假设您的应用程序目录是 /home/ubuntu/my_app ,一旦您启动应用程序,它将在 my_app 目录中生成一个日志目录。现在的最终目标是将磁盘使用负担转移到已安装的目录,并且当前应用程序目录中不存在我们的日志目录。因此,只需继续执行以下步骤:

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

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

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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文