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?

- 18,092
- 4
- 117
- 102

- 9,374
4 Answers
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.

- 111
- 7

- 1,086
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 tar
TabSpacelink
completes to ln -s target/ link
.

- 829,060
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.

- 524
- 4
- 3
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.

- 1,663
-
I'm also wondering if it actually makes a difference, but why would that additional slash be stored then? – Tobias Kienzler Feb 21 '11 at 14:09
foo -> bar/
thenfoo/qux
is equivalent tobar//qux
. While the title of the question, formally speaking, doesn't includefoo -> bar/
, I also discuss that case in my answer there. – Gilles 'SO- stop being evil' Sep 02 '16 at 13:11