Symlinks not working when link is made in another directory?
Asked Answered
R

3

10

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

Reporter answered 22/12, 2011 at 9:41 Comment(0)
P
23

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
Pamela answered 22/12, 2011 at 9:48 Comment(1)
You don't need to to use an absolute path. As long as you preface your path with a dot (which means 'you current directory path') you should be good.Burson
W
5

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.

Weald answered 22/12, 2011 at 9:55 Comment(4)
When writing a script that makes symlinks, is there any way to specify which directory a relative path should be made from?Reporter
With script you should go for absolute path. Have a fixed directory to contain symbolic links and then create the links pertaining to any file in the directoryWeald
All relative symlinks are relative to the directory the link is in. There is no requirement that a link should point to an existing file when created (as you have found). The choice of relative link or absolute should NOT be made on dificulty making the link but wether you want a relative link or an absolute one. Remember that moving a relative link after it was created changes the absolute path of the file it points to. See here for conversion: #2565134Thetes
This should be the preferred answer. For one, if you are using git, you absolute path will not work. The trick to to prepend you path with either a dot ".", meaning "you current position" or a double dot "..", which means "one directory up.Burson
F
1

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.

Fuld answered 3/6, 2016 at 8:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.