When executing a "long" listing of a soft link, ls -l
displays the file attributes of the soft link. When executing ls -lL
(or ls -l --dereference
), the file attributes are those of the file that the link points to, but ls
still prints the name of the link itself. The man
page doesn't say anything about this. The info
page on ls
just says that "ls
still prints the name of the link itself, not the name of the file that the link points to.", without an explanation as to the reason why.
I suppose this is a deliberate choice, but does anyone know the rationale behind this behavior of ls -L
?
Why does ‘ls -L’ print the name of the link itself, not the name of the file that the link points to
lssymlink
Best Answer
Because the filename at the other end of the link is not (or might not be) the filename in the directory that you're accessing with
ls
.Two problems with displaying the name of the target of the symbolic link in place of the name of the link itself:
The file does not exist. If the name of target of the symbolic link is displayed in the directory listing, you may be led to believe that this is the name of the file in that directory, but it isn't. The name of the target is not the name by which you access the file in that directory; the file with that filename does not exist (in that directory), or, in the worst case, it may be a totally different file (or a directory, or whatever) when accessed by that name.
Files may appear to have identical names. If the name of the target of the symbolic link is displayed, then you may find that it's exactly identical to another filename in that directory, which can not be true on a Unix system.
In these cases, it leads to confusion for the users, and they would have to verify the listing by using
ls
without-L
, which would render the-L
option pretty pointless.This (not displaying the name of the target) is also the behaviour specified by POSIX, quite explicitly:
There is no further discussion about this in the Rationale section of the POSIX
ls
manual.