Paths
In OS X, the root (or utmost top-level) of your file system has the path of /
. Each sub-directory, including your personal files, lives somewhere down in the file system tree. When you're accessing files in the Finder, you'll typically only work with files within your home directory, which we can refer to at the command line as ~
. If you're curious as to what the actual path of your home directory is, you can change to it using cd ~
and then using pwd
to display your current location.
Each file and directory has a unique path. The path is made up of the location within the filesystem tree as well as the name of the file or directory. Eg, my music directory might have the name 'music', and be located at /Users/alice
, therefore have the absolute path of /Users/alice/music
.
Tips
- When you are working with directories in a graphical user interface, they will likely be referred to as a folder. Same thing, different name and representation.
- If you have a file or directory selected in Finder, hit CMD+I to open the Inspector and see its path
- As you change directory, type the first letter of a path and press the TAB button on your keyboard. This allows you to 'tab complete' the path - a way of avoiding typing and also to probe out a path name
Relative paths
Any path that doesn't start with a forward slash is a relative path, meaning that its actual location depends on where you are when you refer to it. Relative paths are essential because otherwise we'd always have to type absolute (or full paths. Note that ~
points to your home directory's absolute path.
For example, in my home directory, let's say I have documents, music, and photos subdirectories. If am at my home directory, I can type cd music
to change into the music subdirectory (so I will be at ~/music
). When I use the relative path 'music' as part of cd music
, the system needs to resolve it to an absolute path. In this case, because there is a subdirectory called 'music' in our current directory, it knows what we mean by that relative path.
If the system can't resolve a relative path, you'll get an error message along the lines of 'No such file or directory'.
You can traverse as many levels down the filesystem tree as you like:
cd ~/music/r/radiohead/amnesiac
You'll also want to go 'up' the filesystem tree, to shift your location to the parent directory of the directory you are in. You can use the special reference ..
for this. ..
always means 'up one level from the current path reference'.
For example, if we're deep down in our music:
AlicesMac:amnesiac alice$ pwd
/Users/alice/music/r/radiohead/amnesiac
AlicesMac:amnesiac alice$ cd ..
AlicesMac:radiohead alice$ pwd
/Users/alice/music/r/radiohead
You can use several of these references to jump multiple levels up:
AlicesMac:amnesiac alice$ pwd
/Users/alice/music/r/radiohead/amnesiac
AlicesMac:amnesiac alice$ cd ../../../
AlicesMac:music alice$ pwd
/Users/alice/music
Another handy reference to know is the single period .
which refers to your current location. In many cases it is not necessary to use it, but there are exceptions like when you start a program in the current directory.