Symlinking to a directory gives to different results with ls -l depending on whether I ln -s dir or ln -s dir/. But what's the actual difference, and which one should I prefer why?


The only thing I can think of is that it "protects" you from someone deleting the directory and creating a file.

[user@host linktest]$ mkdir test
[user@host linktest]$ ln -s test/ slash
[user@host linktest]$ ln -s test noslash
[user@host linktest]$ ls -l
total 4
lrwxrwxrwx 1 paul paul    4 Feb 21 21:00 noslash -> test
lrwxrwxrwx 1 paul paul    5 Feb 21 21:00 slash -> test/
drwxrwxr-x 2 paul paul 4096 Feb 21 20:59 test
[user@host linktest]$ file *slash
noslash: symbolic link to `test'
slash: symbolic link to `test/'
[user@host linktest]$ rmdir test
[user@host linktest]$ file *slash
noslash: broken symbolic link to `test'
slash: broken symbolic link to `test/'
[user@host linktest]$ touch test
[user@host linktest]$ file *slash
noslash: symbolic link to `test'
slash: broken symbolic link to `test/'
[user@host linktest]$

The version with the slash breaks when the target is replaced with a file.


There's no difference. (There would be a difference if the target was not an existing directory.)

The final slash might have ended up there because of shell completion: with some configuration, ln -s tarTabSpacelink completes to ln -s target/ link.

  • The linked question seems to be about multiple successive slashes in paths, but not about trailing slashes on links. I'm not sure it has anything to say here.
    – mwfearnley
    Sep 2 '16 at 12:12
  • Actually, I shouldn't have said that. It has quite a bit to say, on a common concern that's closely related to the question. I don't think it leads all the way to this conclusion though.
    – mwfearnley
    Sep 2 '16 at 12:41
  • @mwfearnley It's a logical consequence: if foo -> bar/ then foo/qux is equivalent to bar//qux. While the title of the question, formally speaking, doesn't include foo -> bar/, I also discuss that case in my answer there. Sep 2 '16 at 13:11
  • Hi, thanks for responding.. It tells me that paths including symlinks are equivalent. It doesn't necessarily tell me what happens if I access the symlink itself (without a trailing slash), and without being a Unix expert, I don't necessarily know what 'equivalence' means or doesn't mean.
    – mwfearnley
    Sep 2 '16 at 13:37
  • There is at least one edge-case where it makes a difference, so I had to downvote this answer.
    – Flimm
    Jan 17 '17 at 16:01

Your question is really about the behavior of the ls program.

1) If you do ls -l $dir where $dir is actually a symlink, you get information about the symlink.

2) If you do ls -lL $dir where $dir is a symlink to a directory, you get information about the target directory.

3) If you do ls -l $dir/. it forces the symlink to be followed and provides information about the target directory.

4) If you do ls -l $dir/, the results may be the same as #1 or may be the same as #3 depending on which version of ls is being used. I was used to an older version of Solaris doing it like #1 and was surprised by Linux doing it like #3.

and which one should I prefer why?

Without trailing slash if you may be concerned whether a give directory name is an actual directory versus a symlink to a directory.

With trailing slash if you are more concerned about the files in the directory instead of the directory itself.


Interesing question. I've made small test:

$ mkdir dir
$ ln -s dir/ test_slash
$ ln -s dir test_noslash
$ ls -l
total 4
drwxr-xr-x 2 vrusinov vrusinov 4096 Feb 21 16:41 dir
lrwxrwxrwx 1 vrusinov vrusinov    3 Feb 21 16:41 test_noslash -> dir
lrwxrwxrwx 1 vrusinov vrusinov    4 Feb 21 16:41 test_slash -> dir/
$ strace ls test_slash 2> trace_slash
$ strace ls test_noslash 2> trace_noslash
$ wc -l trace_*
   79 trace_noslash
   79 trace_slash
$ diff -u trace_* | less

As you can see, there is no difference in number of system calls (at least for ls) and traces looks very similar. Howewer, this is just dump test and I'm not sure - there might be some differences.

  • I'm also wondering if it actually makes a difference, but why would that additional slash be stored then? Feb 21 '11 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.