The Raspbian Command Line, Part 1

The Raspberry Pi is a very useful tool for beginners to learn the basics of computer science, but one of the first things new users need to wrap their heads around is how to use the terminal. Since so many useful tutorials out there have readers using the Linux terminal, it’s important to have an understanding of how to make use of it.

How did we get here?

The roots of using typed words to communicate with our computer go back to the 19th century and the teletypwriter/teleprinter. These machines allowed people to communicate with each other over long distances via a telegraph wire. When the first computers were invented, these systems were used to interact with them. Soon, the teleprinters were abandoned in favor of screens and keyboards, but the method of typing commands remained.

When the first Unix operating system — the progenitor of Linux and Raspbian — was released in 1970, it used this same system of interacting with the computer, properly known as a command-line interface (CLI). This method of interacting with computers continued into the 1980s and 1990s when graphical user interfaces (GUIs) like Windows and Macintosh began to dominate the market.

So why are we still using the CLI if we have these easy to use GUIs, even on our Raspberry Pi? A good analogy would be using the CLI is kind of like speaking to the computer in its own language, while using a GUI is like speaking to the computer through an interpreter. By speaking the same language as our Raspberry Pi we can more effectively make it do what we want.

Starting with the simple stuff

Accessing the CLI of the Raspberry Pi is straight forward. We can either press Ctrl + Alt + t on the keyboard or click on the icon at the top left of the desktop.

We should see a window similar to the one below.

The first thing we see is our username. pi is the default username when Raspbian is installed. After the @ sign is our hostname. raspberrypi is the default hostname when Raspbian is installed. After the colon is our current location within the file structure. The tilde (~) indicates that we are in our home directory which defaults to /home/pi. The dollar sign ($) indicates two things. One is the particular type of CLI we are using. In this case we are using the Bourne-again shell, commonly known as bash (a shell is another name for CLI; each shell has slightly different commands and interacts with the computer differently). The second thing signified by the $ is the level of access we currently have: non-root (i.e. we can’t make major changes to the files). If we log into the root account, the $ will be changed to #.

The Bourne-again shell was developed by Brian Fox and released in 1988 as a free alternative to the Bourne shell which was developed by Stephen Bourne from Bell Labs in 1979. Bash, along with many other programs (including Linux), was developed as part of a movement to make free and open alternatives to the most popular Unix programs of the time. The Raspbian system on our Pi is made entirely of free software inspired by this movement.

Where are we now?

Let’s find out where we are. Type pwd into the terminal and press enter. pwd is an acronym for “print working directory.” This will show us where we are currently located within the file structure. The pwd command has a bit more functionality than this, but for now, this is all we need to know.

Now let’s take a look around. Type ls into the terminal. What will print onto our screens is a list of the files and directories arranged alphabetically in a table, top to bottom and left to right.

All I have in my home directory are other directories. By default, directories are displayed in blue in the Raspbian terminal. As we explore our Raspberry Pi we’ll find different types of files displayed in different colors such as pink for media files, green for executable files, and white for normal files.

It’s possible to modify the ls command to make it work a little differently. Type ls -l into the terminal. The -l is called an option. Many of the CLI commands have options that extend their functionality. In this case the -l tells the ls command to list the contents of the working directory in a long format, giving more details about the contents of our current directory.

Now we see each item in our current directory listed top to bottom along with a lot of new information. The first thing listed on each line is the type of file listed, in this case it’s a d for directory. The next triad of letters show the permissions the file creator has with the listed file or directory. The next triad shows the group permissions (each file or directory is assigned to a group for ownership). The final triad shows the usage rights for everybody else.

Understanding Permissions [click to expand]
FilesDirectories
r (read)can read file contentscan list folder contents
w (write)can change file contentscan create new files
x (execute)can run file (if it’s executable)can access directory and its files

The number next to the permissions is the number of links to the file or directory listed (we’re not going to worry about this too much right now). Next to this number is pi pi, these are the the name of the user who created the file or directory, and the group to which it is assigned. In this case the name of the user and the name of the group are the same. The next number is the size of the file or directory in bytes. Next is the date the file or directory was last modified. Finally, the name of the file or directory is listed.

The ls command is a very versatile one and we’ve barely scratched the surface of what it can do. For a more complete understanding of what ls can do, type ls --help into the terminal.

Where are we going?

The last command we’re going to look at, coupled with the previous two, is going to let us explore all of our Raspberry Pi. So far we’ve only explored one directory of our Pi but there’s so much more than our home directory.

To start, let’s go to the root directory, type cd / into the terminal. cd is short for “change directory” and the / is kind of a shortcut character to indicate the root directory. If we want to quickly return to our home directory we can use another shortcut by typing cd ~.

Now that we’re in the root directory let’s take a look around.

There are lots of interesting things going on here but for now we’re going to learn two more variations of the cd command before finishing this tutorial up. First, we can change to any one of the listed directories (let’s use /var as an example) by typing cd var into our terminal. Finally, unless we are in the root directory, we can use the command cd .. to move to the parent directory of our current directory.

Although we have covered only a fraction of the functionality of the Raspberry Pi terminal, with these three commands we have enough knowledge to get a complete view of the file structure. Don’t be afraid to explore. Next time we’ll take a look at how to manipulate all the enigmatic files we’ve come across.

One thought on “The Raspbian Command Line, Part 1

Comments are closed.