Why doesn't ln -s tell that it fails when creating a symlink to an existing symlinked directory?
14,349
Solution 1
Because in the second ln
it doesn't fail it creates a
symlink_dir/dir_2 -> dir_2
symbolic link
Do a:
ls -l symlink_dir/dir_2
And you'll see a (probably broken) symlink there.
That's how ln
is meant to work if the target is a directory (or a symlink to a directory).
A third ln
could fail because there's already a dir_2
inside symlink_dir
(aka dir_2
).
Solution 2
As is desired, specifying -n
will make ln
fail in the second command:
$ ln -ns realdir symdir
$ ln -ns realdir symdir
ln: creating symbolic link `symdir' to `realdir': File exists
Note that -v
is of course irrelevant to the outcome.
Related videos on Youtube
Author by
user2139806
Updated on September 18, 2022Comments
-
user2139806 almost 2 years
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?
-
user2139806 over 11 yearsI see. Then the second "ln -sv dir_2 symlink_dir" will then then print the error msg because it is a broken link or something.
-
Kevin over 11 yearsI would strongly recommend against putting a
--force
option in any alias, there's a reason they're not the default behavior. -
cjaphe about 10 yearsJust
-sn
(or-ns
) is sufficient to makeln
fail as desired.