2

I have found that when creating a symbolic link to a folder it is produced with or without the trailing slash based on your input. for example:

$ ln -sfv /ln-test/FOLDER/ test-tail
test-tail -> /ln-test/FOLDER/

$ ln -sfv /ln-test/FOLDER test-notail
test-notail -> /ln-test/FOLDER

$ ll /ln-test
total 16
drwxr-xr-x  2 user  wheel    68B  2 Aug 08:35 FOLDER
lrwxr-xr-x  1 user  wheel    15B  2 Aug 08:36 test-notail -> /ln-test/FOLDER
lrwxr-xr-x  1 user  wheel    16B  2 Aug 08:36 test-tail -> /ln-test/FOLDER/

In the example above tested on both my Mac and Debian boxes the resulting link matches the trailing slash of the input.

As I understand it this should probably not matter, however recently we have run into a bug in Objective-c that is the result of having a trailing slash. We still need to track this bug down but remaking the link without the trailing slash fixes the issue.

So my real question is; should it matter if a link to a directory has a trailing slash or not?

hoss
  • 143

2 Answers2

3

Accessing a file through a symbolic link is equivalent to replacing the file's base name by the symlink text if the symlink text doesn't start with / (relative link), and to replacing the file's full path by the symlink text if the symlink starts with / (absolute link). If there's a trailing slash in the symlink text, so be it.

A trailing slash in a file name means “the file must be a directory”. If the target of the link is a directory, then accessing a file in it results in a calculated path that contains two slashes: one from the symlink text and one as the directory separator. Given

lrwxr-xr-x  1 user  wheel    15B  2 Aug 08:36 test-notail -> /ln-test/FOLDER
lrwxr-xr-x  1 user  wheel    16B  2 Aug 08:36 test-tail -> /ln-test/FOLDER/

then test-notail/foo is equivalent to /ln-test/FOLDER/foo and test-tail/foo is equivalent to /ln-test/FOLDER//foo.

Multiple slashes are as good as one (with one exception: a path that begins with exactly two slashes, on some systems). So a trailing slash (or multiple trailing slashes) in a symbolic link to a directory doesn't make a difference to the system.

If an extra slash makes a difference to an application, that's a bug in the application.

2

To the application, no it should not matter. The link without the trailing slash is (at a lower level than you would normally think of it) a link to the file called FOLDER, a file which just happens to be a directory. The link with the trailing slash is explicitly to a directory called FOLDER. As a counter-example:

$ mkdir test
$ cd test
$ touch foo
$ ll
drwxrwxr-x.   2 jwbernin jwbernin  4096 Aug  2 09:33 .
drwx------. 102 jwbernin jwbernin 20480 Aug  2 09:33 ..
-rw-rw-r--.   1 jwbernin jwbernin     0 Aug  2 09:33 foo
$ ln -s foo/ TRAIL
$ ll
drwxrwxr-x.   2 jwbernin jwbernin  4096 Aug  2 09:33 .
drwx------. 102 jwbernin jwbernin 20480 Aug  2 09:33 ..
-rw-rw-r--.   1 jwbernin jwbernin     0 Aug  2 09:33 foo
lrwxrwxrwx.   1 jwbernin jwbernin     4 Aug  2 09:33 TRAIL -> foo/

The color highlighting isn't shown, but that last is a broken link. If you ln -s foo NOTRAIL, the NOTRAIL entry is a valid link to the file foo. Because foo is not a directory, the link using the trailing slash ends up broken.

All directories are files (to the OS), but not all files are directories.

John
  • 17,011
  • I minced words in my original question I made a directory called folder. (original comment deleted). – hoss Aug 02 '17 at 13:45
  • that makes sense, basically the trailing slash is technically correct (since the link is to a directory) but either should work since all directories are files and the link just resolves to a file that says it's a directory. – hoss Aug 02 '17 at 13:47