2

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/

1

2 Answers 2

2

/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)

0

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.

4
  • 2
    The trailing / 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 /.
    – xenoid
    Commented Aug 21, 2020 at 23:34
  • 1
    POSIX requires that a path ending in / be treated as a directory, and there is nothing naive about that. Commented Aug 22, 2020 at 1:01
  • 1
    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 /.
    – Stewart
    Commented Aug 22, 2020 at 5:46
  • If I recall right FreeBSD and macOS even require the trailing slash if you wanted to copy a directory into a target directory. Without that slash it was replacing the target directory with the newly copied one. So, this one character may have a devastating difference under some circumstances. On Linux 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.
    – Phoenix
    Commented Aug 29, 2020 at 6:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .