What is the difference between a directory name listed between slashes, after a slash, and ending with a slash?
For example, /bin/
, /bin
, and bin/
/bin/
is an absolute path to the 'bin' directory.
/bin
is an absolute path to the 'bin' file (that may be a directory)
bin/
is a relitive path to the 'bin' directory.
For completeness:
bin
is a relative path to the 'bin' file (that may be a directory)
There is no practical difference between /bin/
and /bin
, except /bin/
is more explicit in communicating that it is a directory. A naive program may look for that as a clue to decide if it looks like a file or directory.
The last one, though, will refer to a bin
directory in the current working directory. If you are in the root directory, /
, it will coincide with the previous two references. Whenever you change directories, the directory that is referenced will change, if it exists at all.
/
is more than a clue. In a bash pattern it will only match directories, and many of the core utilities (stat, ls, grep...) will complain if an argument with a trailing /
is not a directory. And in some applications it is important, for instance rsync
will omit a level of directory nesting when copying directories if the source has a trailing /
.
/
be treated as a directory, and there is nothing naive about that.
Commented
Aug 22, 2020 at 1:01
cp file /somedir
will either make a copy of a file called /somedir
if it isn't a directory or /somedir/file
if it is. I don't know how many times I've had to delete files created that way. cp file /somedir/
will throw an error if /somedir/
doesn't exist. Things are just easier when you are in the habit of using a trailing /
.
cp dir /some_other_dir
is smartly handled and “virtually” (it will not appear on screen) adds the BSD-slash to still copy the source into the destination. Habitually I’m adding it when copying into and leave it away when acting directly on it.