> cd /tmp > ln -s foo > ls -alhF /tmp lrwxrwxrwx 1 user user 3 Jul 29 14:00 foo -> foo
Is this a bug in
ln or is there a use case for symlinking a file to itself?
This is with
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
It's not a bug. The use case is for when you want to link a file to the same basename but in a different directory:
cd /tmp ln -s /etc/passwd ls -l passwd lrwxrwxrwx 1 xxx xxx 11 Jul 29 09:10 passwd -> /etc/passwd
It's true that when you do this with a filename that is in the same directory it creates a link to itself which does not do a whole lot of good!
This works regardless of whether you use symlinks or hard links.
The easiest way to find out of course, is to try it and see. When no 2nd argument is given,
ln will create a link in the current directory with the same name as the original:
$ ln -s /etc $ ls -l lrwxrwxrwx 1 terdon terdon 4 Jul 29 16:09 etc -> /etc
This is also explained in
In the 2nd form, create a link to TARGET in the current directory.
The "2nd form" refers to:
ln [OPTION]... TARGET (2nd form)