Tech Tutorial: How to Use File Paths

File paths show up a lot when you work with computers. The two most common scenarios in which you'll meet them are when you're moving around your computer via a command prompt and when you're linking files while creating web pages. (In this tutorial I'll be discussing how file paths look/work on Unix-based machines like Linux and Mac OS X, but the Windows verity is very, very similar.)

File paths are pieces of text that look something like: /User/Ruth/Documents/myTextFile.txt. This particular file path tells me exactly where myTextFile.txt lives on my hard drive. Forward-slashes mean something like "go to" or "look in", and the words in between the slashes are folder names. In this case, "/User" tells me "look in the User folder", then "/Ruth" says "look in the folder named Ruth", then "look in Documents", then, finally, end at the file myTextFile.txt. Notice, the last word/name in a file path can be an actual file, not a folder.

Okay, so we looked at an example of a file path that started with a "/". This sort of file path is called an absolute file path. The initial "/" means "start at the root folder". The root folder is simply the first folder on your hard drive. Start at the first folder, and then move into each of the following folders until you get to that text file.

But there is another, slightly different file path type, and this one is way more common. It's called a relative path, and it's relative to wherever you are at the moment when you use the file path. Say you're writing a web page and you want to link to a CSS file. Maybe you keep your CSS file in a folder called "css". So let's say your folder has the HTML file named index.html, a folder named "css" and a folder named "images". To "get to" the CSS file from your HTML file, you have to link the CSS file with this relative path: "css/style.css". It's a relative path because it doesn't start with "/". Instead it starts in the folder that the HTML file is already in, and then it heads straight into the first folder in the path, in this case the "css" folder. If there were no folder named "css" (perhaps the folder is called "styles" instead) then the path wouldn't work.

But what if the file you're going for isn't "ahead" of you in the file structure? What if your file structure looked like this:

/User/Ruth/Documents/website --> website folder has: index.html, css (folder), images (folder)
/User/Ruth/Desktop/catImage.jpg

and you wanted to link the catImage.jpg in your website. You should never do this when creating a website, instead you should always put your images (or images folder) in the same folder as your HTML pages, but for the sake of teaching you about file paths, let's assume this is the case. There's a file on the desktop called catImage.jpg that you want to link to your HTML file that's in a folder called website. Since the HTML file is in the website folder, the path you'd put in your HTML document would look like this: "../../Desktop/catImage.jpg"

".." means "go back one folder". So the file path is saying "go back twice" or "go back two folders" then "go into the Desktop folder" and then "grab the catImage.jpg" file. Notice, if we hadn't gone back two folders and simply wrote "/Desktop" ("go into Desktop") the path would fail because there is no "Desktop" folder in "website". We have to back up into "Ruth" before we can see "Desktop" because in this scenario "Desktop" and "Documents" are both in "Ruth".

One period (.) in a file path means "the folder I'm already in". Single periods aren't used that often because they're almost always optional. For example, the file path we used a few paragraphs above, "css/style.css", could've been written as "./css/style.css". As you can imagine, saying "start at the folder I'm in" is kind of redundant.

And there you have it! That's all of what you need to know about file paths to be proficient in linking files and traversing folders using the command line.

Comments

Popular posts from this blog

The Left Hand of Darkness - spoiler review

Ender's Game - spoiler-free review

House of Suns by Alastair Reynolds - spoiler-free review