Absolute and Relative Paths

Absolute and Relative Paths


So, the top most directory in the file
system is the root directory. And we denote the root
directory with a slash. You can describe
the location of any file or directory in the file system,
with a full path beginning with a slash. The full path is called
the absolute path, and that means that it tells every step
that has to be taken from the root, or the absolute beginning
of the file system. Absolute paths are unambiguous and
easy to understand, but they can also be
inconvenient to work with, especially if you’re working
with deeply nested directories. To make things simpler,
we can use relative paths instead. A file’s relative path, is its location relative to
the current working directory. If you’re working with files in or near the current working directory,
this can save you a lot of typing. Every time you’ve referred
to a file by just its name, you’ve actually been
using a relative path. This is the simplest
sort of relative path. The shell looks for the specified file
name within the current directory. For example, if the current
working directory is ocean, then the relative path of
the clam directory is just clam. Rather than having the full
path from the root, we can just have the path
from the working directory. Oh, and cwd here is just an abbreviation
for current working directory. You could also write relative paths for
files and other directories. For example,
the relative path of the giant file, and the clam directory,
is just clam/giant. Notice that, unlike a full path, the relative
path does not start with a slash. That’s how you can tell a relative
path from an absolute one. You can also write relative paths for
items that are closer to the root. The special directory entry ‘..’
points from a directory to its parent. So, if you’re in /home/philip/ocean,
and you refer to ‘..’, you’re talking about
the directory /home/philip. And if you refer to ../mountain, you’re
referring to /home/philip/mountain. There’s also a special
entry which is just ‘.’. ‘.’ points from each
directory to itself. For example, because our current
working directory is ocean, if we use .,
that refers to /home/philip/ocean. Another handy shortcut is ~. ~ is an abbreviation for
your own home directory. By starting a relative path with ~, you can easily specify paths
relative to your own home directory.