Why does ln -s accept a single argument

> 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 coreutils 8.21-1ubuntu5.1.

Asked By: Squidly


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 man ln:

In the 2nd form, create a link to TARGET in the current directory.

The “2nd form” refers to:

ln [OPTION]… TARGET (2nd form)

Answered By: terdon

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.

Answered By: Celada
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.