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/

Greenonline
  • 1,851
  • 7
  • 17
  • 23

2 Answers2

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)

user1794469
  • 4,067
  • 1
  • 26
  • 42
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.

  • 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 Aug 21 '20 at 23:34
  • 1
    POSIX requires that a path ending in / be treated as a directory, and there is nothing naive about that. – Michael Homer Aug 22 '20 at 01: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 Aug 22 '20 at 05: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 Aug 29 '20 at 06:21