Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What's the difference between

cd abc/xyz/


cd abc/xyz

What's the importance of / at the end? Which is the right way of using it? Because both seem to work properly.

share|improve this question

migrated from Jan 18 '10 at 7:26

This question came from our site for professional and enthusiast programmers.

title should have s/Doubt/Question/, or should be the first line of the quesiton – Anonymous Jan 18 '10 at 10:26
up vote 9 down vote accepted

The difference may be minor in the cd example you provided, but beware when you mv files.

For example if you want to move several files to another directory, and you type

for i in *.mp3 ; do mv $i dest ;  done

Forgetting the trailing slash, then each of the files moved will overwrite a file called dest (the first will actually create a file called dest if it doesn't already exist). The result is that all files except the last are overwritten with the last file, and it's now called dest.

To move to a directory, you must specify the trailing /

for i in *.mp3 ; do mv $i dest/ ;  done

One side effect of the slash is that if the directory doesn't exist, you'll get an error message.

share|improve this answer
+1 for giving an example where it does matter. – Torben Gundtofte-Bruun Jan 18 '10 at 12:30
Yes. This isn't an issue of pathname resolution, though, it's how an ambiguity in the specification of mv gets resolved in practice. Likewise the issue with rsync. – Charles Stewart Jan 18 '10 at 13:40
"To move to a directory, you must specify the trailing /". No you don't. It is simply a safety trick in case the directory does not exist, which works since "dest/" is (as I write in my answer below) equivalent to "dest/.", which will be an invalid path if "dest" is not a directory. The slash it not needed. – Teddy Oct 14 '12 at 0:38
cd abc/xyz/

is the correct way of doing it. But if you specify

cd abc/xyz

and if cd figures out that xyz is a directory, it assumes the presence of the trailing /

share|improve this answer
There is no reason to prefer one over the other. – Charles Stewart Jan 18 '10 at 10:21
cd abc/xyz

Is correct, since "abc/xyz" refers to 'the "xyz" entity in the "abc" directory'. When you type

cd abc/xyz/

it refers to 'The "" entity in the "xyz" directory in the "abc" directory'. The "" (empty string) entity gets automatically translated to ".", which is the "current" directory, which in this case is the "xyz" directory. So it all works out to the same thing.

(Some people feel that directories "must" have a slash appended. They are mistaken.)

share|improve this answer
s/must/should/ ... – DevSolar Aug 22 '12 at 15:41

They are identical. The string is treated by the filename operators as a "path" which is an abstraction, and each path is associated with a basename and another path, the dirname. Two paths are identical if each of these are the same string. For your example:

cas var$ dirname abc/xyz; basename abc/xyz
cas var$ dirname abc/xyz/; basename abc/xyz/

Note that, while some programs treat strings identically, including all the usual UNIX utilities, if they are the same paths, there are others that do care about trailing slashes. The most important of these is rsync.

Postscript From the POSIX spec, Pathname Resolution:

A pathname that contains at least one non-slash character and that ends with one or more trailing slashes shall be resolved as if a single dot character ( '.' ) were appended to the pathname.

share|improve this answer
so to interpolate from your postscript, the example abc/xyz/ is actually interpreted (under POSIX) as abc/xyz/.. (Teddy made this point in his answer as well, but thanks for providing a source.) – quack quixote Jan 18 '10 at 13:36

You must log in to answer this question.

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