The shell and moving around the file system
Lesson 2 of 11
2The shell and moving around the file system
What the picture shows: Each cd moves your current place one node in the tree, and the prompt changes with it.
Every code block here runs with one click on the page. Read, press Run, compare the output, then change something and run again. If you know cd and ls, jump to the exercises.
Map of this part
The concepts in order, click any one to jump to it:
Two ways into the same idea
The terminal is like talking to a receptionist who only sees what you type. Ask "where am I?" and they name the room, "what is here?" and they list it, "take me to the kitchen" and you move. Four questions are enough to get around the whole house without looking.
The terminal is a text window and the shell (bash or zsh) is the program that interprets what you type. pwd prints the current folder, ls -la lists contents including hidden ones, cd moves you (cd .. goes up a level), and touch and mkdir create a file and a folder. Files starting with a dot are hidden by default.
Before You Start
🗂️The files and paths lesson
You know the tree, absolute and relative paths, and ...
⌨️Typing
You will type short English commands. You do not need speed, you need accuracy in the letters.
The Core Idea
1Terminal, shell and prompt
The terminal is the black window you type in. The shell is a program inside it that reads what you type and runs it. The best known shell is bash.
The line waiting for your typing is the prompt, usually showing your name, the machine's name and your current place, then $. Everything after $ is yours.
A command is a word, then options starting with a dash, then the things it works on. ls -a notes: the command ls, the option -a, the folder notes.
2Where am I, what is here, go there
You always have a current place in the tree. pwd prints it, ls shows what is in it, cd moves you. Follow the picture, then run the code.
mkdir -p darb/notes made a folder inside a folder in one go. touch made an empty file. And basename "$PWD" printed only the current folder's name without its full path.
mkdir -p darb/notes cd darb basename "$PWD" ls touch notes/today.txt ls notes
Output:
darb notes today.txt
3Up and down the tree
cd .. goes up one step. Repeat it to go up two. And cd alone takes you home from anywhere.
Notice we went down three levels with one relative path, then came up level by level. A relative path shortens the way, and .. brings you back.
mkdir -p city/street/house cd city/street/house basename "$PWD" cd .. basename "$PWD" cd .. basename "$PWD"
Output:
house street city
4Hidden files
Any file whose name starts with a dot is hidden: ls does not show it, ls -a shows everything. Settings are often hidden files in your home folder.
Notice . and .. in the list: the current folder and its parent really exist as entries in every folder, which is why cd .. works.
mkdir -p box cd box touch visible .hidden echo "--- ls ---" ls echo "--- ls -a ---" ls -a
Output:
--- ls --- visible --- ls -a --- . .. .hidden visible
How It Works, Step by Step: What happens when you press Enter after a command
The shell splits the line into words: the command, then its options, then its targets.
It looks for a program with that name in known folders.
It runs it, passes it the words, and shows you what it prints.
When the program ends the prompt returns, and that is your cue to type the next command.
Worked example
ideas.txt inside ~/projects/darb while you are in your home folder. Two lines: mkdir -p projects/darb then touch projects/darb/ideas.txt. No cd needed, because the relative path reached from where you were.Myth vs Reality
| Myth | Reality |
|---|---|
| The terminal is for experts and dangerous. | Four commands cover most of your day, and as long as you do not type a delete command nothing is lost. |
| cd opens the folder the way a double click does. | cd only changes your current place and shows nothing. ls is what shows. |
| A hidden file is secret. | Hidden means tools do not list it by default to keep folders tidy. Anyone sees it with ls -a. |
Try it yourself
You are in ~/darb/notes and type cd ../code. Where do you end up?
.. goes up to darb, then code goes down into the sibling of notes. One command with one relative path; the two dots are part of it, not a second path.
Quick review cards
pwd do?ls versus ls -a?cd ../..mkdir -p a/b/c do?Check Your Understanding
Why does cd print nothing on success?
Because Unix tools stay quiet on success and speak on failure; silence is good news.
touch versus mkdir?
The first creates an empty file, the second a folder.
Why do . and .. exist in every folder?
Because the system stores a reference to the folder itself and to its parent inside it, which is what makes relative paths work.
Resources for this lesson
Grouped by type. Start with the official docs for precision, or an article or video for the explanation.
Done with this lesson?
Sign in to save your progress.