Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

4 Answers 4

up vote 3 down vote accepted

There's no difference.

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

share|improve this answer

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.

share|improve this answer
1  
+1 that's an interesting corner-case –  Tobias Kienzler Feb 22 '11 at 8:17

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.

share|improve this answer
    
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

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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