Root, home, and the current directory
On this page
You open a new terminal to read your to-do list from lesson B04.1, and you type the command you have used many times:
learner@rootvolt:~$ cat todo.txtcat: todo.txt: No such file or directoryThe file exists, and the command is right. What is wrong is the place: a terminal always works in one directory, and this one is looking in the wrong one. Every lesson of module B04 told you to run cd ~/lab in a new terminal, and never said why.
By the end of this lesson, you can say which directory you are in, move one directory down and one up, and explain the four names /, ~, . and ... You can also tell apart three things that are all called root.
Before you start
Section titled “Before you start”- The lessons of module B04, from Command anatomy (B04.1) to Practice: Understanding Commands.
- The
~/labpractice directory from lesson B04.1 (setup).
This time, do not run cd ~/lab first. Open a new terminal with Ctrl+Alt+T, as in lesson B03.2, and start there.
Ask where you are
Section titled “Ask where you are”In the new terminal, run:
pwd/home/learnerpwd stands for “print working directory”. It prints the current directory: the directory your shell works in. Another name for it is the working directory, as in help cd in lesson B04.3: “Change the shell working directory”. Each terminal window runs its own Bash (lesson B03.2), with its own current directory, and a new one starts in /home/learner.
This explains the opening. A program looks for a name such as todo.txt in the current directory. todo.txt is not in /home/learner. It is in lab, one level further down.
Read the directory name from the left
Section titled “Read the directory name from the left”/home/learner is a chain of names. Read it from the left:
- The
/at the very start is the root directory: the top of the file tree, which holds all other directories. Lesson B02.3 said that the installer puts your system in/. homeis a directory inside/.learneris a directory insidehome.
The other / characters only separate the names. Every directory except / is inside another directory, and following them upward always ends at /.
The command ls / lists the root directory. On the course VM, it prints:
bin home mnt sbin.usr-is-merged usrbin.usr-is-merged lib opt snap varboot lib64 proc srvcdrom lib.usr-is-merged root swap.imgdev lost+found run sysetc media sbin tmpFind home in the list, then list it:
ls /homelearner/home holds the home directories of the regular users; lesson B02.3 created one. Your ls / can differ slightly, for example in swap.img, which depends on how the system was installed. Lesson B05.4 explains the main directories.
~ is a short name for your home directory
Section titled “~ is a short name for your home directory”/home/learner is your home directory. The prompt shows it as ~, and Files shows it as Home (lesson B03.1). When a word is ~ or starts with ~/, Bash replaces the ~ with the full name of your home directory before it runs the command. So cd ~/lab and cd /home/learner/lab mean the same thing.
If you chose another user name in lesson B02.3, your home directory is /home/ followed by that name.
Move down into a child directory
Section titled “Move down into a child directory”cd means “change directory”. Give it the name of a directory inside the current one:
cd labcd prints nothing. Look at the prompt instead: it now ends in ~/lab$. Run the command from the opening again:
cat todo.txtInstall UbuntuVerify the ISO checksumTake a snapshotThe command did not change. The place did.
A directory inside another one is its child (lesson B04.3 called it a subdirectory), and the directory that holds it is its parent. So lab is a child of /home/learner, and /home/learner is the parent of lab.
Go one level further down, and check where you are:
cd Documentspwd/home/learner/lab/DocumentsThe prompt says ~/lab/Documents, and pwd gives the same directory with its full name, starting at /.
Move up with ..
Section titled “Move up with ..”To go back to the parent, give cd the name .., two dots:
cd ..The prompt ends in ~/lab$ again. .. means the parent of the current directory, so you do not need to know the parent’s name.
The replay below shows this walk from a new terminal, with the current directory marked in the Files panel. Above lab, the panel draws only /, home, and your home directory.
learner@rootvolt:~$ pwd/home/learnerlearner@rootvolt:~$ cd lablearner@rootvolt:~/lab$ cd Documentslearner@rootvolt:~/lab/Documents$ pwd/home/learner/lab/Documentslearner@rootvolt:~/lab/Documents$ cd ..learner@rootvolt:~/lab$ Files Other folders in / are not shown
- /
- home/
- learner/~
- lab/cwdargument
- Documents/
- notes.txt-rw-r--r--
- report.txt-rw-r--r--
- .draft.txthidden-rw-r--r--
- projects/
- todo.txt-rw-r--r--
Steps (13)
- Type
pwd— Where am I? A new terminal starts in your home directory - Press Enter
- Output:
/home/learner - Type
cd lab— Move down into the child directory lab - Press Enter
- Type
cd Documents— One more level down - Press Enter
- Type
pwd— The full name of the current directory - Press Enter
- Output:
/home/learner/lab/Documents - Type
cd ..— .. is the parent: one level up - Press Enter
- cd with a child's name moves down one level; cd .. moves up to the parent.
In words: pwd prints /home/learner. cd lab moves the marker from your home directory down to lab, and cd Documents down to Documents. pwd prints /home/learner/lab/Documents. cd .. moves the marker back up to lab.
The two entries in every directory
Section titled “The two entries in every directory”In lesson B04.1, ls -a showed two names you never created, . and ... Go into Documents and look again:
cd Documentsls -a. .. notes.txt report.txtEvery directory has these two entries. .. is its parent, the name you gave cd above. . is the directory itself. Compare:
You run in ~/lab/Documents |
Output | Lists |
|---|---|---|
ls . |
notes.txt report.txt |
Documents itself, the same as ls |
ls .. |
Documents projects todo.txt |
Its parent, lab |
So cd . moves you nowhere: you are already there.
You met .. once before: in lesson B02.2, ../SHA256SUMS named the file SHA256SUMS in the parent of the altered folder. Lesson B05.2 explains names like this.
Climb to the top
Section titled “Climb to the top”You are in ~/lab/Documents. Run cd .. once to get back to ~/lab.
Now guess. From ~/lab, you run cd .. four times in a row. Where are you after the third time? And what does the fourth one do, when there may be nothing above?
Run it, then compare with your guess
learner@rootvolt:~/lab$ cd ..learner@rootvolt:~$ cd ..learner@rootvolt:/home$ cd ..learner@rootvolt:/$ cd ..learner@rootvolt:/$ pwd/The first cd .. reaches your home directory, the second /home, and the third /. Outside your home directory, the prompt shows the full name.
The fourth cd .. gives no error and does not move you. / has no parent, so in /, the name .. means / itself.
To go home from anywhere, run cd ~. cd with no argument does the same: it moves you to your home directory.
learner@rootvolt:~/lab$ cd ..learner@rootvolt:~$ cd ..learner@rootvolt:/home$ cd ..learner@rootvolt:/$ cd ..learner@rootvolt:/$ pwd/learner@rootvolt:/$ cd ~learner@rootvolt:~$ Files Other folders in / are not shown
- /
- home/
- learner/~cwd
- lab/
- Documents/
- notes.txt-rw-r--r--
- report.txt-rw-r--r--
- .draft.txthidden-rw-r--r--
- projects/
- todo.txt-rw-r--r--
Steps (14)
- Type
cd ..— From ~/lab up to your home directory - Press Enter
- Type
cd ..— Up to /home - Press Enter
- Type
cd ..— Up to /, the root directory - Press Enter
- Type
cd ..— At / there is no level above - Press Enter
- Type
pwd— Check where you are - Press Enter
- Output:
/ - Type
cd ~— ~ takes you home from anywhere - Press Enter
- Each cd .. goes up one level until /, the top. cd ~ goes straight home.
In words: from ~/lab, three cd .. commands move the marker to your home directory, /home, and /. A fourth leaves it on /, where pwd prints /. cd ~ moves it straight back home.
Tell the three roots apart
Section titled “Tell the three roots apart”You have now met three things called root:
| Name | What it is | Where you met it |
|---|---|---|
/ |
The root directory, the top of the file tree | This lesson; lesson B02.3 |
| root | The administrator account | Lesson B03.3 |
/root |
The root account’s home directory | Lesson B03.3 |
/root is not /: it is one directory inside /, next to home, as the ls / output above shows. And root’s home directory is not inside /home, which holds only learner.
The course VM can also show who owns each directory. The option -d makes ls describe a directory itself instead of listing what is in it:
ls -ld / /root /home /home/learnerdrwxr-xr-x 23 root root 4096 Nov 2 00:28 /drwxr-xr-x 3 root root 4096 Nov 2 00:28 /homedrwxr-x--- 15 learner learner 4096 Nov 5 06:57 /home/learnerdrwx------ 5 root root 4096 Nov 2 00:53 /rootThe third column names the account that owns each directory: root owns /, /home, and /root, and learner owns its home directory. Module B11 explains the other columns. Your dates and numbers will differ.
Mistakes to recognize
Section titled “Mistakes to recognize”Read each message in three parts, as in lesson B04.4: who is speaking, which word it names, and what went wrong.
No space after cd.
learner@rootvolt:~/lab$ cd..cd..: command not foundNo program name speaks first. The word is cd.., and it is not found. Without the space, Bash reads one word and looks for a command called cd.., as with ls-l in lesson B04.1. Type cd .., with a space.
A child of another directory.
learner@rootvolt:~/lab/Documents$ cd projectsbash: cd: projects: No such file or directoryBash speaks for cd, which is part of Bash (lesson B04.4). The word is projects, and the message says there is no such directory. Yet you created projects in lesson B04.1: it is a child of lab, not of Documents, and cd looks for the name in the current directory only. Go up with cd .. first, then run cd projects.
On the course VM, the same mistake can give no error at all. Your home directory has its own Documents, which Files showed in lesson B03.1, separate from ~/lab/Documents. So in a new terminal, cd Documents moves you into that other directory. Read the prompt: the lab’s directory is ~/lab/Documents.
Root’s home directory.
learner@rootvolt:~/lab/projects$ cd /rootbash: cd: /root: Permission deniedlearner@rootvolt:~/lab/projects$ pwd/home/learner/lab/projectsBash speaks for cd again, and the word is /root. This time the directory exists, and Permission denied says that your account may not enter it: it is the root account’s home, as in lesson B03.3. A cd that fails does not move you, as pwd shows.
Try it yourself
Section titled “Try it yourself”Goal: Reach a directory the lesson did not visit, look at its parent, and climb partway up.
- Open a new terminal. Move into
~/lab/projectsone level at a time, and check where you are. - Without leaving
projects, list what is in its parent. - Using only
cd .., go up until you are in/home. Count how many times you need it, and check where you are. - Go back to your home directory with a two-letter command.
Expected: pwd prints /home/learner/lab/projects, then /home. The listing in step 2 shows Documents projects todo.txt. At the end, the prompt is learner@rootvolt:~$, and pwd prints /home/learner.
Hint
Start with lab. Every directory has an entry for its parent, and ls can list that entry.
Solution
learner@rootvolt:~$ cd lablearner@rootvolt:~/lab$ cd projectslearner@rootvolt:~/lab/projects$ pwd/home/learner/lab/projectslearner@rootvolt:~/lab/projects$ ls ..Documents projects todo.txtlearner@rootvolt:~/lab/projects$ cd ..learner@rootvolt:~/lab$ cd ..learner@rootvolt:~$ cd ..learner@rootvolt:/home$ pwd/homelearner@rootvolt:/home$ cdlearner@rootvolt:~$ pwd/home/learnerYou need cd .. three times: to lab, to your home directory, and to /home. The two-letter command is cd with no argument; cd ~ works as well.
- Every shell works in one current directory.
pwdprints its full name, and the prompt shows it too, with~for your home directory. /is the root directory, the top of the file tree./home/learnerislearnerinsidehomeinside/.cdwith the name of a child moves one level down.cd ..moves to the parent. In/,..is/itself.cd ~orcdtakes you home.- Every directory has two entries:
.for itself and..for its parent. /is the root directory, root is the administrator account, and/rootis that account’s home directory: three different things.
Next, Absolute and relative paths (B05.2) shows how to name a directory from anywhere in the tree, so cd can reach it in one step.
Further reading
Section titled “Further reading”- GNU Bash Reference Manual: Bourne Shell Builtins, the entries for
cdandpwd. On your system,help cdandhelp pwdprint short versions. - GNU Bash Reference Manual: Tilde Expansion, how Bash replaces
~with your home directory. man 7 hieron your system, or the Ubuntu 24.04 hier(7) page online: the layout of the file tree.- Filesystem Hierarchy Standard 3.0: The Root Filesystem, with sections on /home and /root.
man lsfor the-aand-doptions, or the Ubuntu 24.04 ls(1) page online.