To drop into the Terminal, click on the fourth icon from the left along the top of the Raspberry Pi desktop, the one with a right-facing arrow and an underscore. This is the shell, or Terminal.
Step 1 – First, you’re going to look at directories and the directory path. A directory is the same thing as a folder, however in Linux it’s always called a directory. These are placed inside each other using a “/” character. So when you see / home/pi it means the pi directory is inside the home directory. Enter: clear and press return to clean the screen. Now enter: pwd. This stands for Print Working Directory and displays /home/pi.
Step 2 – When you log in to your Raspberry Pi, you don’t start at the base of the hard drive, known as the ‘root’ (also known as the topmost directory). Instead you begin inside your user directory, which is named ‘pi’ by default and is itself in a directory called ‘home’. Directories are indicated by the ‘/’ symbol. So, “‘/home/pi’” tells you that in the root is a directory called home, and the next “‘/’” says that inside “home” is a directory called “pi”. That’s where you start.
Step 3 – Enter: ls to view the contents of the current directory. You should see Desktop, Documents, and Downloads and Scratch in Blue. You may also see other items depending on how much you have used your Raspberry Pi. The colour code is worth knowing: directories are blue while most files are white. As you go on you’ll see other colours: executable files (programs) are bright green, archived files are red and so on. Blue and white are the two you need to know to get started.
Step 4 – Now you’re going to move from the pi directory into the Documents directory. Enter: cd Documents. Note the capital “D”. Linux is case sensitive, which means you have to enter the exact name including correct capitalisation. The cd command stands for change directory. Now enter: pwd again to view the directory path. It will display /home/pi/ Documents. Enter: ls to view the files inside the Documents directory.
Step 5 – How do you get back up to the pi directory? By using a command “cd ..”. In Linux two dots means the directory above, also known as the parent directory. Incidentally, a single dot “.” is used for the same directory. You never use “cd .” to switch to the same directory but it’s worth knowing because some commands need you to specify the current directory.
Step 6 – The “ls” and “cd” commands can also be used with more complex paths. Enter: ls Documents/Pictures to view the contents of a Pictures directory inside your Documents directory. You can switch to this directory using cd Documents/Pictures; use cd ../.. to move back up two parent directories.
Absolute Vs Relative Paths
It is important to know the difference between the working directory, root directory and home. There are also two types of path: Absolute and Relative. These are easier to understand than they sound. Let’s take a look…
Step 1 – By default, commands like “ls” use the working directory. This is the current directory that you’re looking at and is set to your home directory by default (/users/pi). Using “pwd” (Print Working Directory) lets you know what the working directory is, and using “cd” changes the working directory.
Step 2 – The root directory is always ‘/’. Entering: ls / lists the contents of root, and entering: cd / switches to the root directory. This is important because there is a difference between “ls Documents/Pictures” and “ls /Documents/Pictures”. The first command lists the contents of the Pictures directory in Documents inside the working directory (which, if you are in the home directory, will work).
Step 3 – The second command (“ls /Documents/Pictures”) attempts to list the content of Pictures in a directory called Documents inside the root directory (because the path started with ‘/’, which is root). There is typically no Documents directory in root, so you will get a “No such file or directory” error. Starting a path with ‘/’ is known as an “absolute path”, while starting without the ‘/’ is known as a “relative path” because it is relative to your working directory.
Step 4 – There is also an absolute path shortcut to your user directory, and that is the tilde “~” character. Entering: ls ~ always lists the contents of your home directory, while “cd ~” moves straight to your home directory, no matter what your working directory is. You can also use this shortcut wherever you are: enter: ls ~/Documents/Pictures to display the contents of the Pictures.
The guide above is a sample of the content in this high-quality guidebook. Our guidebooks are all written by experts, and designed to help anyone learn more about the tech around them. If you would like to see more guides like this, and continue to expand your knowledge of the subject, click the image to see more information and viewing options.