Lesson 2: Moving around the filesystem
In the previous lesson, you learnt about the Home folder and how to list files that this folder and its subfolders contain.
Essentially, the Home folder is where your personal files such as Documents, Downloads and Pictures are stored by default. You also learnt that the Home folder is located at /home/username
on Linux and /Users/username
on macOS.
Entering and exiting folders
In a GUI, we navigate the filesystem with a combination of double clicking folders and pressing the Back and/or Up buttons in a file browser.
On the command line, we do this with the cd
(think change directory) command. As an example, let's go to the Documents folder and see what it contains:
> whoami
> pwd
/home/username
> cd Documents
> pwd
/home/username/Documents
> ls
'Office presentation.ppt' Firstdraft.doc syllabus.pdf
Here's what just happened:
- The first
pwd
(present working directory) command revealed the current directory. In this case, your Home directory. - The
cd Documents
command tells the system to "change directory" to the Documents folder. - After moving to the Documents folder, the second
pwd
command displays the name and path of the current directory. In this case,/home/username/Documents
.
This step is only necessary because the command line, in its infinite compassion, does not offer a single word of encouragement when you do everything right. It only lets you know when you mess up.
- The
ls
command "listed" the files and folders in your Documents folder.
Taking a step back
Let's try to go back to where we started, i.e., the parent directory -- the Home folder in this case. The symbol for the parent directory is the cryptic ..
(two dots).
> cd ..
> pwd
/home/username
Here, we told the cd
command to change directory to the parent directory.
Running the command again will take us to the parent directory of /home/username
:
> cd ..
> pwd
/home
Unlike the ~
, or tilde symbol which is absolute, the ..
is relative. For example, running cd ~
while you are in, say, /example/folder
always takes you to your Home directory.
cd ..
, however, depends on where you are in the file system -- running cd ..
in /example/folder
takes you to /example
.
Exercises
1. Run the following commands and observe the output. Is the behaviour what you expected it to be?
> cd ~/Documents
> cd ~/Pictures
> pwd
2. Similarly, run cd ~/Downloads
, followed by ls ../Documents
and observe the output.
3. Do the first exercise again. Now, run the following commands:
> cd -
> pwd
> cd -
> pwd
Based on the output, try to guess what the -
symbol means. Is it relative, or absolute?
Doubts and explanations
Explaining the -
symbol
Running the commands in Exercise 3 should go something like this:
> cd ~/Documents
> cd ~/Pictures
> cd -
> pwd
/home/username/Documents
> cd -
> pwd
/home/username/Pictures
The -
symbol, like ..
is a relative symbol. Running cd -
takes you back to the directory that you were in previously.
Think of it as alternatingly pressing the "Back" and "Forward" buttons in a GUI file browser.