Use of / when using cd
This is standard Unix behaviour.
The /
at the beginning of the path represents the root of the disk (or the start/uppermost level of the filesystem tree). As Documents
is not off the root, /Documents
can't be found.
/Users
is off the root directory, so this problem does not occur.
You could use cd /Users/lukas/Documents
to change to that path.
Alternatively, you could use relative addressing. Unix based filesystems have 2 special directories:
-
.
which means "this directory", -
..
which means the parent directory.
If you are in /Users/lukas
, cd ./Documents
would take you to the correct place.
Let's assume you were in /Users/Lukas/Documents
and wanted to go to /Users/Janes/Documents
, you could issue a command cd ../../Janes/Documents
- using ..
would take you back a level - so you would do it twice, before going into the new relative path.
/Users/lukas
is an "absolute" path. The leading /
represents the root directory of your filesystem.
lukas
is a "relative" path. As it is not anchored to the root, it means "look for this in the current directory". Unless the current directory is /Users
(or some other directory with a lukas
in it), this will fail.
So, let's explore your examples, assuming you're in /Users/lukas
:
$ cd Documents/
/Users/lukas/Documents
Relative path given => change to the directory "Documents" that's inside /Users/lukas
.
$ cd /Documents
-bash: cd: /Documents: No such file or directory
Absolute path given => change to the directory /Documents
.
$ pwd
/
This shows that you've now changed the working directory to the root directory, /
(though the cd
command to do this was not shown).
$ cd Users
/Users
Relative path given => change to the directory "Users" that's inside /
.
$ cd /Users
/Users
Absolute path given => change to the directory /Users
.
The key each time is that leading /
. With it, the path is absolute. Without it, the path is relative. This rule is unambiguous because all absolute paths begin with /
(because the root directory is always called /
).
Here's some pseudocode loosely describing that algorithm:
MakePathAbsolute(path):
if <path> starts with '/'
return <path>
else
return <current directory>/<path>
The argument you pass to cd
goes through this algorithm; the directory you end up changing to is the path that the algorithm returns.
Further reading:
-
Unix Files and Directories Tutorial
Joseph L. Zachary
"Introduction to Scientific Programming"