Finding your path

Finding your path

Paths are a simple, yet important concept to understand to interact with files, and CLI programs, arguments, etc.

Paths can be thought of as a way to give out your address. There are always two ways to give your address:

  1. Relative - Relative to a point of reference
  2. Absolute - Relative to a fixed point of reference

Relative

Example: Take the first right from here, then a left, and then keep going straight and you'd see the McDonald's right in front of you.

These directions are relative to where you were / are currently, hence, relative.

Absolute

Example: Marvel Studios, 500 South Buena Vista Street Burbank, California 91521, United States.

Here the fixed point of reference you can see is the earth and since it is intuitively apparent, it is implicit and hence it seems like there is no point of reference and seems absolute

Filesystems and Paths

Filesystems in UNIX systems are a tree structure, everything starts at root and everything gets mounted somewhere on the tree. There is no second tree, there can be many disks, but they all fall in single tree if you want to access it.

Some conventions:

  • / is used to denote tree and is the reference point in absolute path. / would be earth if we were trying to specify address in form of UNIX paths.
  • / is also the delimiter that specifies the thing to the right is child of the thing to the left - grand-parent/parent/child
  • .. is used to denote parent directory. If I am in California, .. would mean USA.
  • . is used to denote current directory. If I am in San Francisco, . would mean San Francisco.
  • ~ is used to denote your home. On unix it is usually /home/<username>, on mac it is /Users/<username>.

Continuing with our real world addresses analogy, if I were to type out absolute address of Marvel Studios in form of UNIX paths, it may look in one of several ways:

  • /United States/California/91521/500 South Buena Vista Street Burbank/Marvel Studios
  • /Countries/United States/State/California/Zip/91521/Street/South Buena Vista Street/500/Marvel Studios

Or if I wanted a path relative to California(meaning I am currently in california) it could be:

  • 91521/500 South Buena Vista Street Burbank/Marvel Studios
  • ./91521/500 South Buena Vista Street Burbank/Marvel Studios

If I were in Texas, the same relative path would appear like:

  • ../California/91521/500 South Buena Vista Street Burbank/Marvel Studios

Relative paths need context, what is it in reference to? The current working directory. Absolute paths come with context, / is the reference point and part of the path, it is the same independent of current working directory.

Observation

  • /home/meet is relative to /
  • ../meet is relative to ..
  • ~/dev is relative to ~
  • ./dev is relative to .

Paths and shell

Most often you'd use paths as arguments to certain commands like ls, cd, find, anything that requires a path as an input.

In shell, your current location is the current working directory which by default is shown on your shell prompt unless you customized your prompt. You can get the absolute path of your current working directory with PWD variable or using pwd command.

meet@Meets-Mac-mini ~> pwd
/Users/meet

Just like any shell command, you also need to escape whitespace that are part of path with \ or you want to use "" like "United States" else it would be interpreted as a separate argument.