Jeremy W. Sherman

stay a while, and listen

Absolute and relative paths

"garden steps" by 2is3

Summary:

  • A path is a list of components (directory or file names) separated by a slash (/).
  • An absolute path starts from the root directory and works its way down: /A/B/file.
  • A relative path starts from some contextually-determined parent directory and works its way down from there: A/B/file.
  • In a terminal context, the default parent directory for a relative path is the present working directory, which you can print using the pwd command.

A path is how you refer to a file or directory. A path like /A/B/file is a concise description of how to find a file:

  • start at the system root directory /,
  • move to the A directory,
  • next move to the B directory,
  • and end with file.

As you can see, a path is just a list of components (A, B, file) separated by slashes (/).

/A/B/file is an absolute path. An absolute path is in truth relative to the filesystem root directory /. No matter the context, no matter what terminal you paste this path into, it will always specify the same path and so the same file.

A/B/file is a relative path. It tells you to move down two directories and end with the file named “file”. It doesn’t tell you how to get to two directories above file: a relative path relies on context to provide its starting point. When you pair it with that starting point, you resolve it into an absolute path.

The terminal understands relative paths as relative to its present working directory, just as a Finder window provides a context to situate the filenames displayed in it. When you double-click the Downloads folder in a window showing the contents of the /Users/Me folder, the Finder understands that you want to view the contents of the /Users/Me/Downloads folder. Likewise, if your working directory is /Users/Me/, then a command to ls Downloads/ will be understood by starting with the absolute path to your present working directory /Users/Me/ and appending the relative path Downloads/ to end up with /Users/Me/Downloads/. The command is carried out as if you had typed ls /Users/Me/Downloads/, but you just saved yourself some typing by relying on the context provided by the present working directory.

Use an absolute path when you want to refer to the same file or directory in the same location regardless of the current working directory. This is useful when you want to reference the same path from multiple contexts. The meaning of an absolute path does not change with the circumstances: it is fixed no matter which user is logged in, no matter what your present working directory is, no matter what computer you are working with.

Use a relative path when you want to refer to a file relative to some folder. You’ll see this a lot in step-by-step instructions that rely on the Terminal: step 1 will have you change to some directory whose absolute path is unknown to the author (“After unzipping the file, change to the unpacked directory…”), and step 2 will have you execute some command relative to that path (“…and make the awesome-script file executable by running chmod u+x awesome-script”).