Skip to content

Navigating File Systems using the Shell

Files store the data you need to work with, so the shell needs to provide you with convenient ways of accessing and interacting with both your files and the folders that organise them. To achieve this, the shell has a working directory. This represents the folder that you are working with. You can execute shell commands to list the files within the working directory, move to new directories, and perform other operations on your files.

File Access Commands

You can directly access files within the current working directory, which we often just call the current directory. Let’s take a look at the main commands you need to get stared exploring files and manipulating the working directory in your terminal:

ActionCommandDescription
Print Working DirectorypwdOutputs the current working directory.
Change DirectorycdMoves the shell to a different working directory (i.e., folder).
List FileslsOutputs a list of files in the current directory.

The pwd command will let you know the path to the terminal’s current working directory. Give it a go in your terminal. Type pwd at the shell’s prompt, press enter to execute the command, and see the path to where you currently are.

Terminal window
pwd

Change Directory (cd)

Changing directory will require you to tell the shell where you want to go to. We call this passing information to a command. There are some conventions on how you pass information to comamnds which we will look at as we go through them.

The main way to pass data to a command is through an argument. In the shell, arguments are provided after the command name. You can pass 0, 1, or more arguments to a command, with each argument separated by a space.

Here is an example on how the change directory command is used. This will change the current working directory into the folder at the /home path.

Terminal window
cd /home

The cd command takes one argument - the path to the directory you want to make the current working directory.

Give it a try!

Use the following commands to move to the root of the file system, print out the working directory, then move back to your home folder and print out the working directory again.

Terminal window
cd /
pwd
# This will output > / (you are in the root folder)
cd /home/multitool
pwd
# This will output > /home/multitool (if you are using the username multitool)

Looking around

Moving folders is great, but we also need to be able to see what is in these folders.

Listing folder contents (ls)

Use the ls command to list the files in a folder. By default, ls lists the files in the current folder. Alternatively, you can pass in an argument for the path to the folder you want to list the contents of.

Terminal window
ls
# You should see a list of the files and folders in the current directory (folder)

In addition to arguments, many commands can also take options. These are arguments that start with a -. There are a number of options you can use to help format the output of the list command. One common set of options for ls is to use ls -lha. The l option asks for a long format list (one file per line), h asks for file sizes in human-readable numbers, and a asks for all files (including hidden files).

Give it a try!

Try running the following commands in your terminal. Compare the output of using ls with different options.

Terminal window
ls -l
# compare with the ls output
ls -lha
# check the file sizes and hidden files

You can also use arguments with wildcards to indicate the kind of file you want to list. Try ls -lha D* The * is a wildcard, meaning it can be replaced by anything. So D* will be any file or folder starting with the letter D.

Try moving around and exploring your file system. Go to the root and list the files and folders there. Then move into some of your folders and list what they contain. Look at a file in your graphical file explorer, and see if you can find the same file in the terminal.

Folder shortcuts

There are some shortcuts we can use in the shell to help move to common folders, and move between folders without having to type out the entire path from the root.

Going home (~)

To get back to your home directory, the one where your files are generally stored, you can either use the full path or the shortcut ~. The shell knows that you will often want to go to your home folder, so it added ~ as a shortcut.

Give it a try. Move back home using cd ~. Use pwd to check where you are.

Terminal window
cd /
pwd
# This will output > / (we are in the root folder)
cd /
cd ~
pwd
# This will output > /home/multitool (if you are using the username multitool)

Parent Folders (..)

How can you move to the parent of the current?

Once again you could use the full absolute path, but you could also use the convenient shortcut ... In the shell, . is a shortcut for the current directory and .. is the shortcut for the current directory’s parent.

Terminal window
cd /home
pwd
# This will output > /home
cd ..
pwd
# This will output > / -- the parent folder of home

Current folder (.)

There is also a shortcut to the current folder. This will be very useful later. You can use . to represent the current folder.

Terminal window
cd /home
pwd
# This will output > /home
cd .
pwd
# This will output > /home -- you moved to the current folder!
cd ./multitool
cd ./Documents
cd ./Code
pwd
# This will output > /home/multitool/Documents/Code

Relative paths

Using folder shortcuts, you can create relative paths. These are paths relative to the terminal’s current location. That is, they describe a path from your current location to another location within the file system. The following examples demonstrate the use of relative paths and folder shortcuts to navigate between sibling folders (folders with the same parent).

Terminal window
cd /home/multitool/Documents/code
pwd
# This will output > /home/multitool/Documents/code
cd ../presentations
pwd
# This will output > /home/multitool/Documents/presentations

You can combine multiple shortcuts together to navigate anywhere in your filesystem without using an absolute path:

Terminal window
cd /home/multitool/Documentations/presentations
cd ../../Downloads
pwd
# This will output > /home/multitool/Downloads
cd ~/Documents
# This will output > /home/multitool/Documents
cd ~/../other-user
pwd
# This will output > /home/other-user (assuming this folder exists)