Imagine your file hierarchy as a map, where folders represents streets
and files represent houses. An absolute pathname is like giving
directions starting at the very outside of the map and working
inwards. The directions
for getting to Maria’s house in an absolute fashion would be “Start at Main street, then go to Oak
street, then to Crestview Drive, and there is Maria’s house.” In
terms of Linux pathnames, that would be /main/oak/crestview/maria.
In Linux, the / (slash) separates levels of directory. The
topmost level of the file system has no name, so when you start from the
topmost level, you start with a separator—the slash.
The absolute pathname to get to Frank’s house is
“Start at Main Street, go to Cedar Street, and there is
Frank’s house.” (/main/cedar/frank).
Now let’s say you were on Crestview Drive in front of Maria’s
house and someone asked you
how to get to Vinh’s house. You wouldn’t answer, “Start
at Main Street, then go to Oak Street, then go to Crestview Drive...” You wouldn’t do
that because you’re already on the correct street. You would just say
“it’s next door.” That is a
relative pathname—starting from where you are right now,
how do you get to the destination? In Linux terms, if
you are in directory /main/oak/crestview, the
relative filename for file vinh is just
that: vinh, because it is in your current
directory.
If you were on Cedar Street and
someone asked you how to get to Tim’s house, you would tell them
to go to Rainbow Drive, and there’s Tim’s house. In Linux,
the relative path is rainbow/tim. Relative pathnames
never begin with a slash.
If you were on Oak Street and someone asked you how
to get to High Street, you would answer with a relative path: Go to
Crestview Drive, and from there to High Street. As a relative
path in Linux, that’s crestview/high.
Now comes a tricky one. You’re on Sunset Drive (in the diagram,
that is the vertical dotted line beneath the folder labeled
sunset), and someone asks you
how to get to Frank’s house. You have to say:
“Go back up one street (that puts you on Cedar street, the
vertical dotted line under the cedar folder);
that’s where you will find Frank’s house.” Whenever
you tell someone to go back up one level in the file hierarchy, you use
.. to symbolize the parent directory. In Linux terms, the
relative pathname is ../frank
The relative pathname is not cedar/frank. If you are
on Sunset Drive, you can’t go directly to Cedar Street; it’s
not one of your sub-directories.
Using ../cedar/frank is also incorrect. That would mean
“back up one level (which puts you on Cedar street) and then go to
Cedar street (but you can’t; inside the cedar folder there
is no other folder named cedar!)
If you are on Rainbow Drive and want to go to Cathy’s house, you
have to back up to Cedar Street, and from there back up to Main Street. Once
there, you have a straight shot to Oak Street and Cathy’s house.
Thus, the relative path is ../../oak/cathy
There is one other abbreviation you can use in Linux
paths; the dot . means my current
directory,” so if you were to use the command
ls ., it would list the current directory.
This seems like a useless concept, but it comes in handy
in certain instances.
At the left is a file hierarchy, with home being right
under the topmost level (so it is /home in absolute terms).
At the right, you can select a directory to change to by using the dropdown
next to the cd command. Then select a directory or file to
list by using the dropdown next to the ls command. When you
click the “Describe Path” button, you will see how to form a
relative path for the ls command.
|
In this section, you will be shown a starting directory and a destination. Enter the correct relative path for accessing the destination file or directory.
|