Practice: Navigating the Filesystem
On this page
A classmate is about to build the same lab VM, and asks you for a short map of it: where the practice files are, and where Ubuntu keeps its settings, its programs, its temporary files, and the information about running processes. “And tell me which of these are still there after I restart the VM,” they add.
You could write the map from memory. But one wrong path sends your classmate to a No such file or directory message, so you decide to check every line in the terminal before you write it down. You also want to check from one place. Your notes are in ~/lab/Documents, so you work from there, and when a check takes you somewhere else, you come back without typing the path.
This practice puts all four lessons of the module to work on that map.
Before you start
Section titled “Before you start”- The four lessons of this module, from Root, home, and the current directory (B05.1) to Important system directories (B05.4).
- The
~/labpractice directory from lesson B04.1 (setup). This time, do not runcd ~/labfirst: open a new terminal with Ctrl+Alt+T and start there.
Every command in this practice only reads. Nothing on your system changes. Write the map on paper or in a message to your classmate; module B06 teaches you to create files in the terminal.
Your task
Section titled “Your task”Reach these six goals, in order:
- In a new terminal, you know which directory you start in. Then you are in
~/lab/Documents, reached with onecd, and you have its absolute path for the map. - Without leaving
Documents, you have a list of everything in the lab directory: one name on each line, the hidden name included, and each directory marked. - The map gives a path to the to-do list that works from any directory. Before you run anything, decide which of these four paths name
todo.txtfrom~/lab/Documents:lab/todo.txt,../todo.txt,~/lab/todo.txt, and/lab/todo.txt. Then check all four with one command. - From inside the directory that holds the settings for the whole system, you have checked that it contains the Bash settings file for every user and the directory
skel, without listing what is insideskel. Then you are back in~/lab/Documentswithout typing its path. - For the settings, the temporary files, and the information about processes, the map says whether each is stored on the disk or made by the kernel in memory, and what happens to it when the VM restarts. Decide first, then check.
- A guide your classmate found runs
/bin/head, buttype headon your system names/usr/bin/head. You know whether these are two programs or one, and why.
Expected evidence
Section titled “Expected evidence”When you are done, your terminal shows:
pwdprinting/home/learner, and later/home/learner/lab/Documents.- Four lines that list the lab: two names with a
/at the end, and one name that starts with a dot. - One
lscommand with four paths, which prints twoNo such file or directorymessages and two paths. bash.bashrc skel, printed in/etc, then acdthat prints/home/learner/lab/Documents.- The type of file system that holds
/etc,/tmp, and/proc, and the path that/binpoints to.
At the end, the prompt ends in ~/lab/Documents$. You can also say in one sentence why lab/todo.txt fails from Documents.
Open a hint only when you are stuck on that goal.
Hint for goal 1
pwd answers “where am I?” in any directory. The lab is not where a new terminal starts, but a path that starts with ~/ works from anywhere (lesson B05.2).
Hint for goal 2
The lab directory is the parent of Documents, and every directory has a short name for its parent. ls takes that name like any other path. Lesson B05.3 has one option for each part of the goal, and short options can share one dash.
Hint for goal 3
Read each path from the left, one name at a time, starting in ~/lab/Documents, as in lesson B05.2. A path that starts with / starts at the root directory instead. Bash replaces a ~ at the start before ls runs, so look at how ls prints that path.
Hint for goal 4
Lesson B05.4 names the directory for settings. Once you are inside it, the names in it are short relative paths. With a directory as its argument, ls lists what is inside; one option makes it describe the directory itself. The way back is the previous directory.
Hint for goal 5
Lesson B05.4 ran one findmnt line for ten directories; you can copy it from there. In its output, ext4 is the file system on the disk, and the other types are made by the kernel. For the restart, remember what Ubuntu does to /tmp each time the system starts.
Hint for goal 6
In ls -l /, a line that starts with l is a link. One command prints the path that a link points to.
Solution
Section titled “Solution”Solution
Goal 1: where you start, and one cd to the notes
pwd/home/learnerA new terminal starts in your home directory, ~. A path that starts with ~/ works from any directory, because Bash replaces the ~ with /home/learner before cd runs:
cd ~/lab/Documentspwd/home/learner/lab/DocumentsThat is the first line of the map. cd lab/Documents also works here, but only because you start in your home directory. From any other directory, it fails.
Goal 2: the lab, from one level below
The lab directory is the parent of Documents, so its name from here is ..:
ls -1AF ..Documents/.draft.txtprojects/todo.txt| Option | What it adds |
|---|---|
-1 |
One name on each line (the digit one, not a lowercase L) |
-A |
Hidden names, without . and .. |
-F |
A / after each directory |
Documents and projects are directories; .draft.txt and todo.txt are files. Your shell did not move: .. was an argument for ls, not for cd.
Goal 3: a path that works from anywhere
Read each path from ~/lab/Documents before you run anything:
| Path | Names todo.txt from ~/lab/Documents? |
|---|---|
lab/todo.txt |
No. It is relative, so it means lab inside Documents, and there is none |
../todo.txt |
Yes. .. is ~/lab, which holds todo.txt |
~/lab/todo.txt |
Yes. Bash turns the ~ into your home directory |
/lab/todo.txt |
No. It starts at /, which has no lab |
Now check all four with one ls:
ls lab/todo.txt ../todo.txt ~/lab/todo.txt /lab/todo.txtls: cannot access 'lab/todo.txt': No such file or directoryls: cannot access '/lab/todo.txt': No such file or directory/home/learner/lab/todo.txt ../todo.txtls reports the two missing paths first, then prints the two that exist. Look at the last line: ls prints /home/learner/lab/todo.txt. It never saw the ~; it received the absolute path.
lab/todo.txt is the path that looks right. It is correct in your home directory, where a new terminal starts, but from Documents it is read from the wrong place. /lab/todo.txt fails because the first / makes it start at the root directory.
For the map, write ~/lab/todo.txt or /home/learner/lab/todo.txt: both work from any directory. ../todo.txt works only from a directory inside ~/lab.
Goal 4: settings for the whole system, and back
Settings for the whole system are in /etc (lesson B05.4). Go there with an absolute path, and check both names with relative paths:
cd /etcls -d bash.bashrc skelbash.bashrc skelBoth names exist, so there is no error. bash.bashrc is the file that Bash reads for every terminal window (lesson B03.3), and skel holds the files that every new account starts with (lesson B05.3). Without -d, ls would also list what is inside skel.
Now go back to the previous directory:
cd -/home/learner/lab/DocumentsGoal 5: on the disk or in memory
Make your guess, then run the findmnt line from lesson B05.4. On the course VM:
for d in / /home /etc /usr /var /tmp /dev /proc /sys /run; do findmnt -n -o TARGET,FSTYPE --target "$d"; done/ ext4/ ext4/ ext4/ ext4/ ext4/ ext4/dev devtmpfs/proc proc/sys sysfs/run tmpfsThe lines follow the order of the command: the third is /etc, the sixth /tmp, and the eighth /proc. /etc and /tmp are on /, in the ext4 file system on the VM’s disk. /proc is a file system of its own, of type proc, which the kernel makes in memory.
The surprise is /tmp: it is on the disk, yet Ubuntu empties it each time the system starts. /proc is made again at each start, for the processes that run then. Only /etc keeps its files.
Goal 6: /bin/head and /usr/bin/head
type headhead is /usr/bin/headreadlink /bin prints the path that /bin points to. Lesson B05.4 ran it with three more links. On the course VM:
readlink /bin /sbin /lib /lib64usr/binusr/sbinusr/libusr/lib64/bin is a link to usr/bin, read from /. So /bin/head leads to /usr/bin/head: one program with two paths, in the merged /usr layout. The guide’s command works on your system.
Your map
| Path | What it holds, and where it is kept |
|---|---|
~/lab |
The practice files, on the disk. The to-do list is ~/lab/todo.txt |
/etc |
Settings for the whole system, on the disk |
/usr/bin |
Programs, on the disk. /bin leads to the same directory |
/tmp |
Temporary files, on the disk, but emptied at each start |
/proc |
Information about processes, made by the kernel in memory at each start |
Recall check
Section titled “Recall check”Answer each question without running anything, then open the answer.
1. You are in /home/learner/lab/projects. Which file does cat ../Documents/../todo.txt read? Follow the path one name at a time.
Answer
Start in ~/lab/projects. .. leads to ~/lab, Documents to ~/lab/Documents, the second .. back to ~/lab, and todo.txt to the file ~/lab/todo.txt. So it reads your to-do list: the path is a longer way to write ../todo.txt.
2. You are in / and run cd root. Which directory does cd try to enter, and what happens for the account learner? What are the other two things called root?
Answer
root is a relative path, read from /, so cd tries to enter /root, the home directory of the root account. Bash reports Permission denied, and you stay in /. The other two are the root directory /, the top of the file tree, and root, the administrator account.
3. In ~/lab, what does ls -l projects print, and what does ls -ld projects print?
Answer
ls -l projects lists what is inside projects. The directory is empty, so only total 0 appears. ls -ld projects prints one line about the directory itself, and that line starts with d.
4. Your classmate says that .draft.txt is safe from other people because it is hidden. Is that right?
Answer
No. A dot at the start of a name hides it only from listings: ls leaves it out unless you add -a or -A. cat .draft.txt reads it like any other file. Who may read or change a file is set by its permissions, which module B11 explains.
5. On the course VM, ls -ld /proc reports a size of 0, yet ls /proc lists many names. Why, and which command tells you whether a directory is on the disk?
Answer
The contents of /proc are not stored anywhere. The kernel produces them in memory when a program reads them, so ls has no stored size to report. findmnt with --target names the file system that holds a path: for /proc it says proc, for a directory on the disk ext4.
- Before you use a relative path, check where you are, with
pwdor the prompt. From another directory, the same path names a different file, or nothing. - A path that starts with
/or~/works from anywhere, andcd -takes you back to where you were, so you can work from one directory. lsanswers several questions in one command: it takes several paths, and-1,-A,-F, and-dchoose what it shows.- On the course VM,
/etc,/usr, and/tmpare on the disk, but/tmpis emptied at each start./procand/sysare made by the kernel in memory, and/binis a link tousr/bin.
Next, Creating and organizing files (B06.1) builds a project workspace with copies of your existing notes.
Further reading
Section titled “Further reading”- GNU Bash Reference Manual: Bourne Shell Builtins, the entries for
cd(including-) andpwd, and Tilde Expansion. man 7 path_resolutionon your system, or the Ubuntu 24.04 path_resolution(7) page online: how Linux follows a path, including.and...man lson your system, or the Ubuntu 24.04 ls(1) page online: the options-1,-a,-A,-d, and-F.man 7 hieron your system, or the Ubuntu 24.04 hier(7) page online, and the Filesystem Hierarchy Standard 3.0: The Root Filesystem.man findmnton your system, or the Ubuntu 24.04 findmnt(8) page online, including the option--target.man 5 tmpfiles.don your system, or the Ubuntu 24.04 tmpfiles.d(5) page online: the rule that empties/tmpat each start.