user225643
user225643

Reputation: 3701

Symlinks not working when link is made in another directory?

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

Upvotes: 10

Views: 27877

Answers (3)

Prasad Kudalkar
Prasad Kudalkar

Reputation: 221

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.

Upvotes: 1

kev
kev

Reputation: 161974

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

Upvotes: 23

Raghuram
Raghuram

Reputation: 3967

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.

Upvotes: 5

Related Questions