Skip to main content

Section 1.3 File Paths

Imagine that I bring you to the edge of a forest, an immense, thick, unexplored forest. When we get there, I tell you that somewhere in that forest lays a chest of pure diamonds, put there 1000 years ago. Then I give you a task: I need you to go find that chest for me (yes, you get a portion of the loot).
Think about how you might go about this task. The forest is unexplored; we only know that the chest exists inside. Where will you look first? Where will you enter? How will you keep track of where you’ve been? If you find it, how will you get back? There are many issues preventing you from doing your task efficiently. Sure, you could get lucky and the chest might be only 50 feet from the edge. But maybe it’s 3000 feet away (or more!).
Think of your computer like the forest. If I ask my computer to open a file called poem.pdf, how will it know where to look for that file? Like you in the forest, it wouldn’t even know where to start. Likewise, what if I had two files named poem.pdf in different folders? What if I had two files named poem.pdf in the same folder? How would it know which one to grab? It wouldn’t! It’s like me telling you there are two identical chests of diamonds in the forest and I want you to bring me the one I am thinking about. But you don’t know the one I am thinking about.
This situation necessitates file paths. Literally, they are the direct path through which the computer can find the exact file you are thinking of. You are basically telling the computer where the file is. Continuing the forest example, it’s as if I told you to enter the forest, take a right at the first tree with a triangle, turn left at the lake, and walk six steps past the boulder that looks like a watermelon. You’d know exactly how to get there and would always find the correct file.

Subsection 1.3.1 Constructing File Paths

What do computer file paths look like? You might have seen one before. Here is an example of a file path on a Mac:
/Users/<username>/Documents/poem.pdf
With this you can see exactly where my poem lies. First, go to a list of all the users on my computer. Second, look at the files for my username, go to the documents folder, and access the file called poem.pdf. Easy!
Maybe. Here’s what that would look like on a Windows computer:
C:\Users\<username>\Documents\poem.pdf
That looks a little different. This one says to go to the C drive. Then look at the list of the users on the computer. Then go to the files for my username, then the documents folder, then the file poem.pdf.
However, they both work the same. The fundamental structure is “start at the outmost folder and work your way in through folders until you get to the file or folder you are looking for”. Recall from Section 1.1 that files must have an extension while folders do not. This is an easy way to differentiate between folders and files.
The key thing to note is that Mac and Windows are designed differently. Mac uses forward slashes to indicate a folder change and Windows uses backslashes (in Git Bash, Windows users can use forward slashes…one reason for sticking with Git Bash!). But their structures are different as well. Notice that Windows has “C:” at the beginning. These are called “drives”; this is the C drive here. There are other drives based on what is stored where but this and other differences are beyond the scope of this book. A brief history on file paths is given by How-To Geek 24  and a rather hard-to-read explanation can be found on Wikipedia 25 .
There are a few important points to remember when reading or finding a file path. The main goal from this section is that you understand how a file path is structured. Paths will come back later.
  • File paths do not like spaces. Recall the lesson learned in File Names With Spaces. To a computer spaces indicate that you are ending the file path and starting a new command. Do not name folders or files you are planning on accessing with the command line with spaces.
  • When you want to enter a new folder, use a slash to indicate that you are going further in the document tree.
  • File paths are unique; you cannot have one file path refer to two objects. This is why your computer will never let you have two files with the same name and extension in the same folder.
    You can however have files with the same name in different folders. Since the file path is different, there are no issues with duplicate files.

Subsection 1.3.2 Hints and Reminders

File and Folder Naming.

Recall Note 1.1.1.1 for alternative naming methods for files and folders. Please, please, please do not use spaces (and consider renaming your existing files that you plan to use with the terminal). I know it looks weird to name things using these cases, but if you really are set on learning Git and the command line, you will want to follow this convention.

Finding File Paths.

In theory, you will never need to type out the full file path. Your computer actually records this information for you to just copy.
On a Mac, right click on any file or folder you want the path for. Click “Get Info”. Under a label called “Where:” you can find the path. Right click on that path and hit “Copy as pathname” to copy the path in the same format as the paths above.
On Windows, right click on any file or folder you want the path for. Click “Properties”. Under a label called “Location:” you can find the path. Highlight the text and copy it.

Using the fish Shell.

Advanced users only.
If you changed your shell to fish as described in Appendix A, you won’t even have to copy a path or remember a path. fish contains autocomplete that looks for files and folders in your current folder and recommends completions.