43

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?

Ciro Santilli OurBigBook.com
  • 18,092
  • 4
  • 117
  • 102

4 Answers4

35

The only thing I can think of is that adding the slash "protects" you from someone deleting the actual directory and replacing it with a file, as the link without the slash would still be valid, now pointing to a different object (a file instead of a directory).

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

Sinipelto
  • 111
  • 7
13

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 02 '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 02 '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. – Gilles 'SO- stop being evil' Sep 02 '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 02 '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
  • @Flimm What edge case are you refering to? – Gilles 'SO- stop being evil' Jan 17 '17 at 18:20
  • 1
    @Flimm The question was about a symlink whose target is a directory. That answer points out that if the target is not a directory, there's a difference. That's an interesting remark, but it's out of scope of the question. My statement that there's no difference applies only in the context of the question! – Gilles 'SO- stop being evil' Jan 18 '17 at 17:19
6

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.

Joe Inwap
  • 524
  • 4
  • 3
5

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.

rvs
  • 1,663