Pathnames, Absolute and Relative

Each directory and file in the file system has a specific location on the file tree. That location is called the pathname, which you can think of as a street address. Pathnames allow you to specify where you want to put files and help you find your location on the file tree.

How Pathnames Work

All absolute pathnames start from the root directory (/) and work their way through each branch of the file tree from the root directory to the file or directory that you wish to identify. A slash / is placed between each level in the path name to separate each directory name from the previous level. Each file or directory has a unique path name.

Example:

Joe Shmoe’s Home Directory

Linux File Names

  • Up to 256 characters long
  • No special characters: | { } [ ] ( ) ! & * ? \ ^ % @ # ” ‘ `<>
    • FYI (advanced): If you must use a special character, precede it with a backslash (‘\’). For example, to name a file “some!file”, you may have to name it “some\!file” for it to function correctly
  • Cannot include the forward slash (‘/’)
  • Should not include spaces (follows the special characters rule). To separate words, it is advised to use an underscore (_), like_this

Absolute and Relative Pathnames

Absolute Pathnames

In the previous section, pathnames were introduced as the way to navigate through the file system. Remember, absolute pathnames start with the root directory. Your home directory has a shortcut for the long pathname, which is ~.

~/Tilde-slash refers to your home directory. The ~ replaces the /mnt/ncsudrive/u/unityid
/Slash refers to the root directory.

Relative Pathnames

UNIX/Linux systems recognize some special character combinations known as relative pathnames. These relative pathnames can be used as shortcuts to save time while accessing files and directories. For example,

../ Dot-dot-slash refers to the directory above your current working directory (the parent directory). This can be used in conjunction with other shortcuts.
./ Dot-slash refers to the current working directory. Instead of typing the long (absolute) pathname, you can use dot. This is useful when working with files in your current working directory.

You can think of an absolute pathname like a physical, named address, such as 2610 Cates Ave, Raleigh, NC 27606 (Talley Student Union). A relative path for the same location would be something like: if I’m at Withers Hall, then cross the railroad tracks and turn right to get to Talley. The absolute path would tell me where to go from any other location, but the relative path will only work if I’m at Withers.

Example

If your current working directory is MyE115 in the tree above, what is the pathname to index.html in the www directory?

The following relative pathnames are valid:

  • ../www/index.html
  • ./../www/index.html

For comparison, the absolute pathname would be: /mnt/ncsudrive/j/jshmoe/www/index.html and that could be written as ~/www/index.html as well.