4

What's the difference between

cd abc/xyz/

and

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.

1
  • title should have s/Doubt/Question/, or should be the first line of the quesiton
    – Anonymous
    Jan 18, 2010 at 10:26

4 Answers 4

9

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.

2
  • 1
    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. Jan 18, 2010 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, 2012 at 0:38
3
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.)

1
  • s/must/should/ ...
    – DevSolar
    Aug 22, 2012 at 15:41
2
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 /

1
  • 3
    There is no reason to prefer one over the other. Jan 18, 2010 at 10:21
1

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
abc
xyz
cas var$ dirname abc/xyz/; basename abc/xyz/
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.

1
  • 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.) Jan 18, 2010 at 13:36

You must log in to answer this question.

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