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.
feedback
|
This question came from our site for professional and enthusiast programmers.
|
The difference may be minor in the For example if you want to move several files to another directory, and you type
Forgetting the trailing slash, then each of the files moved will overwrite a file called To move to a directory, you must specify the trailing /
One side effect of the slash is that if the directory doesn't exist, you'll get an error message. | |||||||
feedback
|
is the correct way of doing it. But if you specify
and if cd figures out that xyz is a directory, it assumes the presence of the trailing / | |||||
feedback
|
Is correct, since "abc/xyz" refers to 'the "xyz" entity in the "abc" directory'. When you type
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.) | |||
|
feedback
|
|
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 Postscript From the POSIX spec, Pathname Resolution:
| ||||
feedback
|