Why does ‘ls -L’ print the name of the link itself, not the name of the file that the link points to

lssymlink

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?

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:

  1. 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.

  2. 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:

Evaluate the file information and file type for all symbolic links (whether named on the command line or encountered in a file hierarchy) to be those of the file referenced by the link, and not the link itself; however, ls shall write the name of the link itself and not the file referenced by the link. When -L is used with -l, write the contents of symbolic links in the long format (see the STDOUT section).

There is no further discussion about this in the Rationale section of the POSIX ls manual.