Why doesn't ln -s prompt that it fails when creating a symlink to an existing symlinked directory?

2

When running (on linux different ubuntu variations):

>ln -s dir_1 symlink_dir
>ln -s dir_2 symlink_dir

It fails without telling that it fails. But if you do the same thing on a file instead or, add v to the option it does tell you that it fails:

>ln -s file_1 symlinkg_file
>ln -s file_2 symlinkg_file

or

>ln -sv dir_1 symlink_dir
>ln -sv dir_2 symlink_dir

It fails with the error msg:

ln: failed to create symbolic link

For me this seems to be a very strange behaviour? Is there a reason for this?

giZm0

Posted 2012-10-16T07:01:46.110

Reputation: 123

Question was closed 2014-03-18T08:13:56.847

The question was cross-posted to Unix & Linux

– Der Hochstapler – 2014-03-18T08:13:56.847

Answers

3

It does not actually fail. It creates your link inside the given directory:

% mkdir dir_1 dir_2
% ln -s dir_1 symlink_dir
% ln -s dir_2 symlink_dir
% ls -l
total 0
drwxr-xr-x 2 user group 60 Oct 16 12:47 dir_1
drwxr-xr-x 2 user group 40 Oct 16 12:47 dir_2
lrwxrwxrwx 1 user group  5 Oct 16 12:47 symlink_dir -> dir_1
% ls -l dir_1
total 0
lrwxrwxrwx 1 user group  5 Oct 16 12:47 dir_2 -> dir_2

This behaviour is described in the manpage:

 ln [OPTION]... TARGET... DIRECTORY     (3rd form)
 ...
 In the 3rd and 4th forms, create links to each TARGET in DIRECTORY.

However, this link fails to link back to dir_2 as it is not set properly. This is also expected behaviour though, and not meant to fail. From the manpage:

Symbolic links can hold arbitrary text; if later resolved, a relative link is interpreted in relation to its parent directory.

By the way, it works the same way for me even with -sv. Maybe you are using a different implementation of ln. Are you sure you are not using -T? Maybe that is set in your ~/.bashrc/~/.zshrc/etc. Try which ln.

dset0x

Posted 2012-10-16T07:01:46.110

Reputation: 1 897

-sv works the same way yes. But it tells you that something has gone wrong. Which probably is because you have it is a "borken" link – giZm0 – 2012-10-16T10:05:47.597

1@giZm0 It does not output any error for me. As you can see in the manpage blockquote above, it should at least create it, even if it warns you that it doesn't link to something that exists: % mkdir a b % ln -sv a c ‘c’ -> ‘a’ % ln -sv b c ‘c/b’ -> ‘b’ – dset0x – 2012-10-16T10:10:19.617

As I have answered in the cross-post, specifying -ns makes ln fail as desired. -v is irrelevant to the outcome.

– Acumenus – 2014-04-02T20:23:56.893