Absolute and Relative Paths

Written by Chris Gregg, with modifications by Nick Troccoli

Click here for a walkthrough video.

As discussed in our unix filesystem overview, the linux file system root path is / and all files and directories are underneath /. The absolute path of a file starts with / and shows each folder and file from the root path, separated by /. E.g.:

/afs/.ir/users/c/g/cgregg/cs107/assign2/docs

We often simply use the two special files, . (current directory) and .. (parent directory), and our home directory, ~, to simplify how we refer to a file, and it can significantly cut down on typing. It can also be used if we move files around our directory structure, so that we don't always have to know the absolute path. In this case, we refer to the path as a relative path. For example, from my home directory, I can refer to the file above as:

~/cs107/assign2/docs/howTo.txt

If I was inside the assign2 directory, I can simply refer to the file as follows:

./docs/howTo.txt

Let's say I was in a sibling directory to assign2 called assign3 (i.e., both assign2 and assign3 are directories inside of cs107). I could refer to the file in the assign2 directory as follows:

../assign2/docs/howTo.txt

This refers to the parent directory (cs107) of the current directory (assign3), and then the rest of the path from there (assign2/docs/howTo.txt).