15 15. Files, Folders, and Paths
Before you can move around your computer with Bash, you need a mental map of how the computer organizes files. This page gives you that map. The commands that use the map (pwd, ls, cd, …) are on the next pages.
15.1 Files and folders (directories)
- A file holds data: a document, a photo, a program, a spreadsheet.
- A folder holds files and other folders. In the command-line world a folder is usually called a directory. The two words mean the same thing.
Because folders can contain folders, two different files can have the same name as long as they are in different folders. For example, projects/website/README.txt and projects/data-analysis/README.txt are two completely separate files.
15.2 The file system is a tree
All of the folders on a computer form one big upside-down tree. The folder at the very top is called the root. On Mac and Linux the root is written as a single forward slash: /.
/ <- the root (top of the tree)
├── Applications
├── Users
│ ├── maria <- maria's home directory
│ │ ├── Desktop
│ │ ├── Documents
│ │ │ └── notes.txt
│ │ └── Downloads
│ └── sam <- sam's home directory
└── ...
Windows organizes files by drive (usually C:). Git Bash (the Bash program we use on Windows) shows the C: drive as the folder /c, so the same ideas apply.
15.3 Your home directory and ~
Every user account has a personal folder called its home directory. Your Desktop, Documents and Downloads folders live inside it. When you open a new terminal window, you usually start out in your home directory.
/Users/maria
/c/Users/sam (Windows itself writes this as C:\Users\sam)
/home/maria
(Throughout this book, Maria uses a Mac and Sam uses Windows. Replace maria or sam with your own user name.)
The tilde character ~ is Bash’s shortcut for “my home directory”. These two lines mean exactly the same thing to Maria’s Bash on a Mac:
/Users/maria/Documents/notes.txt
~/Documents/notes.txt
~ is handy because it works for every user on every computer - your ~ is your home directory, my ~ is mine.
15.4 Paths: the address of a file
A path is the “address” of a file or folder. It lists the folders you pass through to reach it, separated by forward slashes /:
/Users/maria/Documents/notes.txt
Read it left to right: start at the root /, go into Users, then maria, then Documents, and there you will find notes.txt.
Windows normally writes paths with backslashes (C:\Users\sam). In Bash, always use forward slashes (/c/Users/sam). Bash treats a backslash as a special “escape” character, so backslash paths will not work the way you expect.
15.5 Full (absolute) paths vs. relative paths
At every moment, the terminal is “standing” in one folder. That folder is called your current working directory - think of it as the “you are here” marker on a map.
There are two ways to write the path to a file:
| Full path (also called absolute path) | Relative path | |
|---|---|---|
| Starts from… | the root / (or your home ~) |
your current working directory |
| Starts with… | / or ~ |
anything else (a name, . or ..) |
| Example | /Users/maria/Documents/notes.txt |
Documents/notes.txt |
| Works from any folder? | Yes - it always means the same file | No - its meaning depends on where you are |
An analogy: a full path is like a complete mailing address (“123 Main St, Springfield”). A relative path is like directions from where you are standing (“two doors down on the left”).
For example, if Maria’s working directory is /Users/maria, the relative path Documents/notes.txt refers to /Users/maria/Documents/notes.txt. If her working directory were /Users/maria/Desktop, the same relative path would point to /Users/maria/Desktop/Documents/notes.txt - a different place (which probably doesn’t exist).
15.6 The special names . and ..
Every folder contains two special entries:
.(one dot) means this folder - the current directory...(two dots) means the parent folder - the folder one level up.
You can use them inside relative paths. Using the practice files below, if you are standing in bash-tutorial-files/documents/notes:
| Relative path | Means |
|---|---|
ideas.txt |
the file ideas.txt in the current folder |
./ideas.txt |
the same file (. = this folder) |
.. |
the parent folder, documents |
../reports/annual-summary.txt |
go up to documents, then down into reports |
../.. |
two levels up: bash-tutorial-files |
../../music/song1.mp3 |
up two levels, then down into music |
15.8 Tips for naming files and folders
- Avoid spaces.
quarterly-report.txtorquarterly_report.txtis much easier to type in Bash thanquarterly report.txt. If a name does contain spaces, you must put quotes around it, e.g."My Documents/notes.txt". (Quotes get their own pages later.) - Treat names as case-sensitive. Linux treats
Notes.txtandnotes.txtas two different files. Mac and Windows usually don’t, but code you write often ends up running on Linux servers, so it’s best to be consistent.
Git Bash translates Windows drives into folders under /:
| Windows writes | Git Bash writes |
|---|---|
C:\Users\sam\Documents |
/c/Users/sam/Documents |
D:\photos |
/d/photos |
If you ever need the Windows-style version of your current location (for example, to paste into File Explorer’s address bar), Git Bash’s pwd -W command prints it (e.g. C:/Users/sam/Documents). File Explorer accepts forward slashes too.
~ is expanded by Bash, not by the computer
~ is not a real folder name. Bash replaces ~ with your home directory’s full path before running a command. This only happens when ~ appears at the start of a word and is not inside quotes. For example, ls ~/Documents works, but ls "~/Documents" looks for a folder literally named ~. This is one reason to avoid unnecessary quotes around paths that start with ~.
15.9 Practice files
Download the practice files and unzip them into the Documents folder in your home directory, so that you have the folder ~/Documents/bash-tutorial-files (the next pages assume this location): Download the practice files (zip)
If OneDrive backs up your Documents folder, the “Documents” you see in File Explorer is actually inside your OneDrive folder (whose name starts with OneDrive), not at ~/Documents. If cd ~/Documents/bash-tutorial-files later says “No such file or directory”, run ls ~ to find the OneDrive folder and look for Documents/bash-tutorial-files inside it (see Getting Bash on Your Computer).
bash-tutorial-files/
├── README.txt
├── documents/
│ ├── notes/
│ │ ├── ideas.txt
│ │ └── meeting-notes.txt
│ └── reports/
│ ├── annual-summary.txt
│ └── quarterly-report.txt
├── music/
│ ├── song1.mp3
│ └── song2.mp3
├── pictures/
│ ├── family.jpg
│ └── vacation.jpg
└── projects/
├── data-analysis/
│ ├── analysis.py
│ └── data.csv
└── website/
├── index.html
└── styles.css
The practice questions below assume this tree is located at /Users/maria/Documents/bash-tutorial-files (Maria is using a Mac).
Practice Questions
Easy
Question 1 · Short answer
What is another name for a “folder”?
Question 2 · Short answer
What is the name of the folder at the very top of the file system, and how is it written on Mac and Linux?
Question 3 · Multiple choice
What does ~ stand for in Bash?
- The root of the file system
- The current working directory
- Your home directory
- The parent of the current directory
Question 4 · Matching
Match each symbol with its meaning.
| Symbol | Meaning | |
|---|---|---|
1. / (by itself) |
A. the parent folder | |
2. . |
B. your home directory | |
3. .. |
C. the root of the file system | |
4. ~ |
D. the current folder |
Question 5 · True or false
Two different files can have exactly the same name as long as they are stored in different folders.
Question 6 · Short answer
What is the “current working directory”?
Medium
Question 7 · Multiple choice
Which of the following is a full (absolute) path?
documents/notes/ideas.txt../reports/annual-summary.txt/Users/maria/Documents/bash-tutorial-files/README.txt./README.txt
Question 8 · Write the path
Maria’s working directory is /Users/maria/Documents/bash-tutorial-files. Write the relative path to quarterly-report.txt.
Question 9 · Write the path
Write the full path to song2.mp3 in two different ways: once starting with / and once starting with ~.
Question 10 · Write the path
Maria’s working directory is .../bash-tutorial-files/documents/notes. Write the shortest relative path to annual-summary.txt.
Question 11 · Short answer
Starting in .../bash-tutorial-files/projects/website, which folder does the relative path ../.. refer to?
Question 12 · Convert
A Windows user named Sam sees this path in File Explorer: C:\Users\sam\Documents\budget.xlsx. How should Sam type this path in Git Bash?
Question 13 · Short answer
Why are files such as .gitignore and .env not shown by a plain ls command or by the Mac’s Finder? What are such files usually used for?
Hard
Question 14 · Predict
Maria’s working directory is /Users/maria/Desktop. She refers to the relative path Documents/notes.txt. Which file does that path point to, and why might she get a “No such file or directory” error?
Question 15 · Simplify
Simplify the relative path documents/notes/../reports/./annual-summary.txt (working directory: bash-tutorial-files).
Question 16 · Short answer
A script uses the relative path data.csv. It works when Maria runs it from projects/data-analysis but fails when she runs it from her home directory. Explain why, and give one way to make it work from anywhere.
Question 17 · Long answer
Give two reasons why it is a good idea to avoid spaces in file and folder names when you work with the command line, and explain what you must do if a name does contain a space.
Question 18 · True or false
The command ls "~/Documents" lists the contents of your Documents folder.