[SOLVED] ln -sf does not work with a directory target, but ln -nsf does.
SlackwareThis Forum is for the discussion of Slackware Linux.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Yes, this one used to catch me out regularly too when replacing an existing link. Used to see it most often with the /usr/src/linux symlink
example
Code:
gazl@slack:/tmp$ mkdir linux-5.0.0 linux-5.0.1
gazl@slack:/tmp$ ln -sf linux-5.0.0 linux
gazl@slack:/tmp$ ls -l
total 0
lrwxrwxrwx 1 gazl users 11 Nov 18 11:27 linux -> linux-5.0.0
drwxr-xr-x 2 gazl users 40 Nov 18 11:27 linux-5.0.0
drwxr-xr-x 2 gazl users 40 Nov 18 11:27 linux-5.0.1
gazl@slack:/tmp$ ln -sf linux-5.0.1 linux
gazl@slack:/tmp$ ls -l
total 0
lrwxrwxrwx 1 gazl users 11 Nov 18 11:27 linux -> linux-5.0.0
drwxr-xr-x 2 gazl users 60 Nov 18 11:28 linux-5.0.0
drwxr-xr-x 2 gazl users 40 Nov 18 11:27 linux-5.0.1
gazl@slack:/tmp$ ls -l linux-5.0.1
total 0
gazl@slack:/tmp$ ls -l linux-5.0.0
total 0
lrwxrwxrwx 1 gazl users 11 Nov 18 11:28 linux-5.0.1 -> linux-5.0.1
I work around this by using the '-T' option rather than '-n', which seems more correct in this particular example.
Without this option ln assumes that you want to put the symlink in the existing target directory, which is not unreasonable when you thinki about it, but it does catch you out if you're not aware of this little quirk.
I work around this by using the '-T' option rather than '-n', which seems more correct in this particular example.
Without this option ln assumes that you want to put the symlink in the existing target directory, which is not unreasonable when you think about it, but it does catch you out if you're not aware of this little quirk.
I hadn't thought of it that way. Even so, the resulting symlink is useless when you link directories like that...
Code:
bash-4.1$ mkdir one
bash-4.1$ mkdir two
bash-4.1$ ln -s one test
bash-4.1$ ls -l
total 0
drwxrwxr-x 2 notroot users 6 Nov 19 02:10 one/
lrwxrwxrwx 1 notroot users 3 Nov 19 02:10 test -> one/
drwxrwxr-x 2 notroot users 6 Nov 19 02:10 two/
bash-4.1$ ln -sf two test
bash-4.1$ ls -l
total 0
drwxrwxr-x 2 notroot users 16 Nov 19 02:11 one/
lrwxrwxrwx 1 notroot users 3 Nov 19 02:10 test -> one/
drwxrwxr-x 2 notroot users 6 Nov 19 02:10 two/
bash-4.1$ cd test
bash-4.1$ ls -l
total 0
lrwxrwxrwx 1 notroot users 3 Nov 19 02:11 two -> two
bash-4.1$ cd two
bash: cd: two: Too many levels of symbolic links
bash-4.1$
...so it seems to me that this is broken behavior. But, since a symlink is simply text that is resolved later, maybe the -T option is the best they could do. Thanks for the tip!
I should extend this to say that the target for the symlink does not have to exist at the time of creation, the path to the target may also not resolve from the current directory but may resolve from the destination directory, which is the case I illustrated.
This is mentioned in brief in the man page for ln When creating hard links, each TARGET must exist. Symbolic links can hold arbitrary text; if later resolved, a relative link is interpreted in relation to its parent directory.
The section in bold basically means you can tell ln to create the symlink with whatever data you feel like, powerful but dangerous.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.