moocan
moocan

Reputation: 141

How to create symlink to another symlink without following symlink while keeping relative path?

How to create a symlink to another symlink without following target symlink but keeping relative path between the target symlink and created symlink ?

Using the following structure for test

├── dst
│   
└── src
    ├── file.txt -> folder/file.txt
    └── folder
        └── file.txt


lrwxrwxrwx 1 user group 15 mars  16 20:05 src/file.txt -> folder/file.txt

Each time I try to create a symlink of src/file.txt (itself a relative symlink of src/folder/file.txt) in dst folder, the resulting symlink is never like dst/file.txt -> ../src/file.txt but always directly to the final target dst/file.txt -> ../src/folder/file.txt

ln -s -r [-T] 'src/file.txt' dst/file.txt

Only working without -r and specifying myself the relative path

ln -s '../src/file.txt' dst/file.txt

I need to use this inside a for loop for many files recursively and I would like to keep a relative path between the symlinks but without following target symlink, just a relative symlink to another symlink.

Is it possible to use ln with -r relative option without following target symlink ?

Regards

Upvotes: 1

Views: 1458

Answers (1)

Mr R
Mr R

Reputation: 794

Seems like ln -s -r follows links ..

Without the -r option ln -s puts in exactly what you type - so later the other link can be changed and the first directory still works.

# mkdir ABC
# mkdir DEF
# touch DEF/xx.txt
# ls -lAF DEF
total 8
drwxr-xr-x. 2 root root 4096 2021-03-17 11:40 ./
drwxrwxrwt. 4 root root 4096 2021-03-17 11:40 ../
-rw-r--r--. 1 root root    0 2021-03-17 11:40 xx.txt
# ln -s DEF/xx.txt 
# ls -laF
total 16
drwxrwxrwt.  4 root root 4096 2021-03-17 11:40 ./
drwxr-xr-x. 21 root root 4096 2017-03-17 14:13 ../
drwxr-xr-x.  2 root root 4096 2021-03-17 11:40 ABC/
drwxr-xr-x.  2 root root 4096 2021-03-17 11:40 DEF/
lrwxrwxrwx.  1 root root   10 2021-03-17 11:40 xx.txt -> DEF/xx.txt
# cd ABC/
# ln -s ../xx.txt 
# ls -laF
total 8
drwxr-xr-x. 2 root root 4096 2021-03-17 11:40 ./
drwxrwxrwt. 4 root root 4096 2021-03-17 11:40 ../
lrwxrwxrwx. 1 root root    9 2021-03-17 11:40 xx.txt -> ../xx.txt
# echo "ABC" > DEF/xx.txt
# cat ABC/xx.txt
ABC

So all the soft links are soft-links to another soft-link or the file - not resolved all the way through.

Upvotes: 1

Related Questions