Should a directory path variable end with a slash?

When defining a directory path as a variable or constant, should it end with a trailing slash? What is an agreement?

pwd on unix shows your current directory without a trailing slash, while the tab consisting of cd /var/www/apps/ includes a trailing slash, which left me unsure.

+76
variables unix file path
Jun 11 '09 at 9:47
source share
11 answers

I do not include trailing slash when, for example, I specify a directory for storing files. This is because I will use it as

 $store_file = "$store_path/$file_id"; 

I will always add a trailing slash before using a variable that should contain the directory path. I think it's better to always add it than to wonder if the trailing slash is enabled.

+21
Jun 11 '09 at 10:05
source share

I am going with a trailing slash because:

  • "If it ends with a slash, this is a directory. If not, this is a file." this is a simple convention to remember.

  • At least on the operating systems that I usually use, doubling the slash does not cause problems, while dropping the slash causes big ones. Thus, it is most safe to put a slash in a variable and use "$ path / $ file" when using it.

+89
Jun 11 '09 at 11:34
source share

Yes, he should, like:

Pathname + filename = full file location.

SO, the slash between the last directory and the file name must be either at the end of the path or at the beginning of the file name. A file name prefix with / means that you need to take this into account if you just want to open the file (i.e. if you assume that the unqualified file name is in the current working directory).

+9
Jun 11 '09 at 10:47
source share

Whenever I store directory paths or return them from the API, I try to stick to the slash preservation agreement. This avoids the whole “file or directory ambiguity”.

Addendum :
This is not intended to replace methods that can endure either a trailing slash or its absence. Even using this convention, I still always use Path.Combine(...) and similar methods.

+5
Jun 11 '09 at 9:53
source share

Perhaps you should think about what your solution will mean for files. If you do not include a trailing slash at the end of the directory name, you need to add it to the beginning of the file name.

Now, if for some reason the path leading to the file is missing when you concatenate the lines, you get something like /filename , which is not just a file, but an absolute path from the root directory (wherever it is in this context).

This is why I end my paths with a slash and save the files as files.

+4
Jul 15 2018-11-11T00:
source share

I know this is an old thread, but I thought I would share what I was doing. If possible, I would usually allow both (if it was PHP):

 $fullPath = rtrim($directory, '/') . '/filename.txt'); 

Thus, if the directory is specified in the configuration file, it does not matter whether the next person will change it, including the trailing slash or not.

+4
Aug 03 '15 at 10:52
source share

In php, since the dirname (__ FILE __) function returns the directory name without a slash at the end. I tend to stick to this agreement.

Otherwise, using a slash at the end of the directory name will conflict with how dirname (..) works, and then you are stuck with handling two cases, since you don’t know if the directory name was derived from dirname (..) or a constant defined with a trailing slash.

Bottom line: Do not use trailing slash, as dirname (..) does not work.

 // PHP Example dirname(__FILE__); // returns c:\my\directory without a trailing slash, so stick to it! 

For other languages, check the function that retrieves the path name and see if it uses a trailing slash or not, then stick with the language conventions.

+4
Feb 22 '16 at 8:36
source share

I know this is 10 years old, but I wanted to add my very self-confident 0.02 dollars.

No. Absolutely not.

We are talking about a Unix system. As for the directory itself, this is a node, like any other. When accessing the directory, it should never have a slash in the name (link: dirname , pwd , ~ , echo $HOME , echo $PATH , output from ls , etc.),

When accessing the contents of a directory, you need a slash. That is, ls /home/karl/ more suitable than ls /home/karl (FTR, I almost always do the latter, because ... well, lazy).

When using a variable containing a directory to create the full path to a file, you should always include a slash (for example, e: cp ${HOME}/test ${OTHER_DIR}/ ).

The directory is not expected to end with a slash. Any expectation that the directory ends with a slash is incorrect. Thus, adding a slash to the end of the value of the variable *_DIR would undermine expectations.

Is it worth noting that just because it is wrong does not mean that tools / packages / libraries never do this. This is too common when such things add a slash when nothing should exist. Therefore, as Bevan and Paul F. suggested, when using third-party tools, it is best to remove any slashes that may exist in directory names.

Unix inodes

An index (index node) is a data structure in a Unix-style file system that describes a file system object, such as a file or directory.

- https://en.wikipedia.org/wiki/Inode

File System Hierarchy Standard

The Unix file system standard (File System Hierarchy Standard, AKA FHS) clearly shows that directories do not have a trailing slash, and directory contents begin with a slash (the only exception to this is / , because we will not refer to the root of the file system with using an empty line ... and in any case, you should not create files there.)

- http://www.pathname.com/fhs/pub/fhs-2.3.html

- https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard

+4
Dec 20 '18 at 18:37
source share

I usually add trailing slashes, as I will more than likely use this directory to add / receive files ...

In terms of web links, it can actually increase performance by leaving a trailing slash in

http://www.netmechanic.com/news/vol4/load_no11.htm

+3
Jun 11 '09 at 9:56
source share

Yes, there are many file systems that support files without any extensions, so always add a trailing slash to avoid any problems.

+2
Jun 11 '09 at 10:49
source share

I have never seen a solid convention anyway.

Although, of course, no matter what you stop, someone else will be 100% sure that this should be the other way around. So, the best idea is to endure things set up anyway.

In the .NET world, Path.Combine () provides you with a way to handle this - in other environments there are equivalents, from cmd files up.

+1
Jun 11 '09 at 9:57
source share



All Articles