Files, folders and paths
You'll learn how files and folders are organised, and how to write the path to any file.
Watch & read
Start with the required ones. The others are there when you need them.
See it step by step
Walk a relative path one step at a time.
Explanation
You want to tell the computer exactly where a file is. Saying "it's in my documents somewhere" is not enough. You need its path.
Think of your university. Inside the university is the CSE department. Inside the department is your batch. Inside your batch is your section's class register. Each place sits inside a bigger one. Files work the same way: a folder (also called a directory) holds files and other folders, which hold more. Drawn out, it looks like an upside-down tree.
/Users/rafi/projects/notes/todo.txtimages/logo.png
A path is the list of folders you walk through to reach a file, joined by /. The absolute path to
the to-do file starts from the very top: /Users/rafi/projects/notes/todo.txt. It works from anywhere.
A relative path starts from where you are now. If you are already in notes, the same file is just
todo.txt, and the logo is images/logo.png. Two dots, .., mean "go up one folder". From inside
images, the to-do file is ../todo.txt.
The part after the last dot, like .txt, .js or .png, is the extension. It tells programs what
kind of file it is. Changing notes.txt to notes.js does not change what is inside.
On macOS and Linux paths use / and start at /. Your home folder is /Users/rafi (macOS) or
/home/rafi (Linux). On Windows they use \ and start with a drive letter, like
C:\Users\Rafi\projects. In code and in web addresses, always use /, even on Windows.
People often name files My Notes.txt or Project Final.js. Spaces break commands in the terminal, and
many systems treat Logo.png and logo.png as two different files. Use lowercase and dashes:
my-notes.txt, project-final.js.
When you read a path, go left to right: each / is one step into a folder. If it starts with / (or
C:\), it starts at the top. If not, ask: where am I starting from?
Quick check
3 questions. Each one explains itself after you answer.
- 1 / 3You are in
/Users/rafi/projects/notes/images. Where does../todo.txtpoint? - 2 / 3Which of these is an absolute path?
- 3 / 3Which file name will give you the least trouble?
Explain it back
How confident are you with Files, folders and paths?
Done
/Users/rafi/projects/notes/todo.txt and say where the file is, and write a relative path from one file to another.