ln: create symlink using another symlink

mrc02_kr

Let's suppose I have one file and one directory:

$ ls -l
total 4
drwxrwxr-x. 2 user user 4096 Oct  8 09:53 dir
-rw-rw-r--. 1 user user    0 Oct  8 09:53 file

I created a symlink to file called symlink1, and a symlink to dir called dirslink1:

$ ls -l
drwxrwxr-x. 2 user user 4096 Oct  8 09:53 dir
lrwxrwxrwx. 1 user user    3 Oct  8 10:03 dirslink1 -> dir
-rw-rw-r--. 5 user user    0 Oct  8 09:53 file
lrwxrwxrwx. 1 user user    4 Oct  8 09:53 symlink1 -> file

Now I created symlinks to symlink1 using ln -s and ln -sL:

$ ln -s symlink1 symlink2
$ ln -sL symlink1 symlink3
$ ln -s dirslink1 dirslink2
$ ln -sL dirslink1 dirslink3

Now, as far as I understand, symlink3 should point to file and dirslink3 should point to dir. But when I check it, none of the symlink[23] and dirslink[23] points to the original file or dir:

$ ls -l
drwxrwxr-x. 2 user user 4096 Oct  8 09:53 dir
lrwxrwxrwx. 1 user user    3 Oct  8 10:03 dirslink1 -> dir
lrwxrwxrwx. 1 user user    9 Oct  8 10:03 dirslink2 -> dirslink1
lrwxrwxrwx. 1 user user    9 Oct  8 10:03 dirslink3 -> dirslink1
-rw-rw-r--. 5 user user    0 Oct  8 09:53 file
lrwxrwxrwx. 1 user user    4 Oct  8 09:53 symlink1 -> file
lrwxrwxrwx. 1 user user    8 Oct  8 09:54 symlink2 -> symlink1
lrwxrwxrwx. 1 user user    8 Oct  8 09:54 symlink3 -> symlink1

The question is: Is it possible/How do I create a symlink to the original file using another symlink?

Stephen Kitt

-L only works with hard links; as specified in POSIX:

If the -s option is specified, the -L and -P options shall be silently ignored.

If you have readlink you can use that:

ln -s -- "$(readlink symlink1)" symlink4

If your readlink supports the -f option, you can use that to fully canonicalise the target (i.e. resolve all symlinks in the target’s path, if the target symlink includes other symlinks).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Recursive symlink gets created when using ln -f -s

From Dev

Question about the symlink of the ln command

From Dev

Question about the symlink of the ln command

From Dev

Create symlink inside a zipfile in memory using python

From Dev

Create a symlink in /dev using /etc/udev/rules

From Dev

"ln -sf" does not overwrite a symlink to a directory

From Dev

If path is symlink to another path

From Dev

Create Relative Symlink Inside Relative Symlink

From Dev

How to create symlink with Haskell?

From Dev

Create SymLink with GPO

From Dev

How to create a symlink to root

From Dev

How to create a symlink to root

From Dev

Unable to create a symlink to a file

From Dev

Create symlink manually

From Dev

Using ~ in symlink target or making a symlink with a variable

From Dev

Symlink dll files from one folder to another using mklink

From Dev

Makefile target with wildcard to create symlink using target string

From Dev

Privlege error trying to create symlink using python on windows 10

From Dev

Using makefile check if symlink exists and create if it doesn't

From Dev

udev fails to create persistenly named symlink using ID_PATH

From Dev

Copy symlink to another location with ansible?

From Dev

Check file exists and create a symlink

From Dev

What is a symlink and how to create one?

From Dev

Create symlink tree in existing directories

From Dev

Create symlink - overwrite if one exists

From Dev

Is possible to create a symlink for a remote file?

From Dev

How to create a bashrc/zshrc symlink?

From Dev

Create a new symlink for files in a folder

From Dev

Yocto: create a symlink in an image recipe

Related Related

HotTag

Archive