I am just wondering is there a difference in what happens when I leave / at the end of a file path for example:
And omitting it like:
How is this handled or is there a difference?
The context really matters. For
nginx, while specifying the
root for a location, it does not make any difference, since
nginx knows it is a directory. However, it may make a difference elsewhere, since double-slashes can cause problems in URLs (that is, if you do something like
blah/$request_uri while rewriting or proxying).
In general, it doesn't make a difference when it comes to *nix platforms. There are a few exceptions:
rsyncbehaves differently depending on whether the source directory path has a trailing slash or not. A good example of how rsync treats trailing slashes.
Adding a trailing slash can make an application aware that it is looking for a directory, instead of accidentally creating a file:
cp blah non-existent-directory cp blah non-existent-directory2/
Similar to the above, in globbing, it can be used to force expansion to directories only:
$ printf "%s\n" * | wc -l 26 $ printf "%s\n" */ | wc -l 20
Excuse the example. It's done on the root of a fairly standard Ubuntu installation, so no weird names were harmed during the execution of this example.
Trailing slash after a directory name matters in some programs and doesn't in others. For example:
/ after filename means only directory
and not a single file (from
man 5 gitignore):
If the pattern ends with a slash, it is removed for the purpose of the following description, but it would only find a match with a directory. In other words, foo/ will match a directory foo and paths underneath it, but will not match a regular file or a symbolic link foo (this is consistent with the way how pathspec works in general in git).
ls (from wikipedia):
When a directory listing of a symbolic link that points to a
directory is requested, only the link itself will be displayed. In
order to obtain a listing of the linked directory, the path must
include a trailing directory separator character ('/', slash).
yet some other programs that I can't remember now (mostly Emacs macros I saw) that expect only a directory and not a regular file will accept both
dir/ and will add
/ when needed, for example to refer to a file inside
I don't know how nginx works but see muru's answer.
In general, a path that does not have a trailing slash is ambiguous: it could be either a file or a directory. A path with trailing slash can only be a directory. Most shells give you the trailing slash automatically when using tab completion.
That does not mean you should always use a trailing slash; in fact some programs disallow its use. As the others stated, it depends on context. Each program interprets this differently.
One example where a trailing slash makes a huge difference, is
Given the directories
target, the command
rsync -a source target would copy everything to
target/source. But with a trailing slash,
rsync -a source/ target copies the contents of
target directly, without creating a
I'm often confused by this behaviour, so instead of relying on implicit subdirectory creation, I prefer to write it out. I sometimes even use a trailing
/. to make sure no unwanted subdirs would be created since the subdir
. is already there.
For URLs in browsers, http://www.example.com/ and http://www.example.com are the same URL. Whether or not the trailing slash is shown in the browser address bar is purely cosmetic - when the request is sent to the server the slash will be included. (http://www.example.com/foo and http://www.example.com/foo/ on the other hand are different URLs.)
The following is a very good explanation, taken from Stack Overflow: Trailing slash in URLs - which style is preferred?
In my personal opinion trailing slashes are misused.
Basically the URL format came from the same UNIX format of files and folders, later on, on DOS systems, and finally, adapted for the web.
A typical URL for this book on a Unix-like operating system would be a file path such as file:///home/username/RomeoAndJuliet.pdf, identifying the electronic book saved in a file on a local hard disk.
Another good source to read: Wikipedia: URI Scheme
According to RFC 1738, which defined URLs in 1994, when resources contain references to other resources, they can use relative links to define the location of the second resource as if to say, "in the same place as this one except with the following relative path". It went on to say that such relative URLs are dependent on the original URL containing a hierarchical structure against which the relative link is based, and that the ftp, http, and file URL schemes are examples of some that can be considered hierarchical, with the components of the hierarchy being separated by "/".
That is the question we hear often. Onward to the answers! Historically, it’s common for URLs with a trailing slash to indicate a directory, and those without a trailing slash to denote a file:
http://example.com/foo/ (with trailing slash, conventionally a directory)
http://example.com/foo (without trailing slash, conventionally a file)
A slash at the end of the URL makes the address look "pretty".
A URL without a slash at the end and without an extension looks somewhat "weird".
You will never name your CSS file (for example) http://www.sample.com/stylesheet/ would you?
BUT I'm being a proponent of web best practices regardless of the environment. It can be wonky and unclear, just as you said about the URL with no ext.
If you type in the shell:
It moves your current directory to path/somedir.
But if you type:
bash: cd: path/somedir/: No such file or directory
Maybe because the
/ character means that the path is not complete, but continues, so if you put nothing after it is the same as referencing to a inexistent file.