Working with the Command Line
In This Chapter
In addition to Raspbian’s graphic interface, a lot of what you do in this book requires you to type commands into your Raspberry Pi. You do this through the command line interface. This is essentially a blank screen with a text cursor where you use your keyboard to type in commands that make your Raspberry Pi do specific actions.
For many people, working with the command line can be strange or even a bit intimidating at first. It can feel archaic to type in commands to a computer these days, but in reality, it’s a helpful task in getting to know how your Raspberry Pi works. And when you get the hang of it, you’ll find that the command line is actually a lot easier to use than a graphic interface. It’s also far faster because you can just type in one line to make something happen instead of clicking a bunch of checkboxes.
It’s really important to remember that the command line is nothing to be scared of. At its core, it works just like your mouse, but instead of clicking on something, you type the command, information, etc., with your keyboard. It’s going to feel a little strange and confusing initially, so be sure you practice a bit and take the time to familiarize yourself with all the settings.
Throughout this chapter, I cover some of the most common commands, show you how to move files around, explain how to download files, and more. First, though, we have to get a solid understanding of what the command line is and how it works.
Getting to Know the Command Line
The command line interface enables you to interact with your Raspberry Pi by typing in specific commands. You’re essentially keying instructions for the operating system. The command line was the original way people interacted with computers before graphical user interfaces like Windows came around.
It might seem like an antiquated way of working on such a high-tech device as a computer, but the command line is used more than you might think. It’s especially useful for programmers and coders because it provides access to the finer details of a computer. Even casual users can make use of the command line to explore various settings in their operating system.
Linux makes heavy use of the command line, and some distributions are more reliant on it than others. Raspbian gives you two direct ways to access the command line: by exiting the graphical user interface or through a command line shell called LXTerminal. (A shell is a program that runs inside a graphical user interface but still gives you access to the command line.) This isn’t specific to Linux, though. You can find these types of shells on both Windows and OS X.
PI POINTER
You can download any Linux shell program you want to use on your Raspberry Pi. LXTerminal is included with Raspbian, but it’s not your only option. Other popular terminal shells include Terminator (gnometerminator.blogspot.com/p/introduction.html), GNOME Terminal (wiki.gnome.org/Apps/Terminal), and Konsole (konsole.kde.org).
Although it’s a bit complicated at first, the beauty of the command line lies in its simplicity. Because the Raspberry Pi doesn’t have a lot of resources, the command line is actually easier to use than the graphic interface. We use it quite a bit throughout this book, so you’ll get the hang of it pretty quickly.
Linux works a bit differently from other operating systems, so even just basic use requires the command line. The graphical interface in Raspbian is fine, but it doesn’t give you access to everything you need.
Much of the basic work you need to do throughout the lessons in this book requires using the command line. In fact, most projects you’ll do with your Raspberry Pi call for you to type in at least a few commands. Even the simplest stuff, like setting up a Wi-Fi adapter, is easier to do from the command line than the graphical interface.
In addition to the Pi Store, you can download files using the command line. In the projects in this book, you learn how to compile programs, edit text files, check for updates, set up your hardware, and even create a few small programs yourself.
Accessing the Command Line in Raspbian
You can access the command line directly on your Raspberry Pi with Raspbian in one of two ways: through a shell program or by exiting the graphical interface. Either method works well.
In Raspbian, you use a shell program called LXTerminal. Here’s how to open it:
You can also exit the Raspbian interface completely to get to the command line. Here’s how:
When you get to the command line, you’re greeted with a black screen with the following prompt:
pi@raspberrypi ~ $
This is the command prompt. It shows the host name, raspberrypi
, and the username, pi
, of your Raspberry Pi.
Basic Command Line Tools You Need to Know
Unlike with a graphical interface, with the command line, you can’t blindly click around on things until you find what you’re looking for. Instead, to get anything done, you have to type in very specific commands. You’ll need to get used to this because mistyping even just a single character incorrectly can cause the entire line to produce an error.
PI POINTER
To help you avoid problematic typos, the longer sections of commands and code you need to type in throughout the lessons in this book are available at idiotsguides.com. See the book’s introduction for more information.
A variety of commands exist for the command line and do all kinds of interesting things. But for now, let’s cover a few of the basics so you can get an understanding of how the command line works and what you’ll use it for.
Sudo
Sudo
, which stands for superuser do, is one of the most important commands you can type into the command line. This gives you administrative access to your Raspberry Pi and allows you to run as a root user. This means that whatever changes you make will also affect any other users who have accounts on that Raspberry Pi.
DEFINITION
Sudo
stands for superuser do and means you’re running a command as a root user. This is essentially the same as running a command as an administrator. As a superuser, you can access files that wouldn’t normally be available to all users and delete system files you might need, so be careful when you run anything as sudo
.
If you need to do anything permanent in the system—like move around hidden files, download software to specific folders, or even compile software—you need to run those commands as a superuser. To do this, start your command with sudo
before typing anything else.
Sudo
comes with its own set of cautions. For example, you’re permanently altering files, and if you do something wrong, it’ll mess up everything on your Raspberry Pi. Don’t let this scare you though. Remember, part of the beauty of the Raspberry Pi is the SD card that houses the operating system. If something goes wrong, it’s very easy to start again from scratch.
Navigating Files and Folders from the Command Line
With Linux, even basic use requires a little extra work than other operating systems might require. Let’s start by learning a few commands you can use to navigate folders on your Raspberry Pi.
For starters, at the command line, type the following:
pwd
This stands for present working directory. When you type it in, you should get the output /home/pi
. This is the directory you’re currently in. When you run commands, you’re often executing them from the directory you’re presently in.
Don’t worry if you accidentally run a command from the wrong folder. You can move files around pretty easily, thanks to the cp
(copy) and mv
(move) commands. So if you want to move a file from one folder to another, you’d type something like the following:
mv file.txt /username
This moves the file.txt
file to the /username
directory.
If you want to delete a file, use the rm
command. To delete the file you just moved, type this:
rm file.txt
This works with more than just files. You can move and delete directories the same way. Likewise, you can create a new directory with the mkdir
command. For example:
mkdir newdirectory
This command makes a new directory called newdirectory
.
To see exactly what’s in a directory at any point, use the ls
command to list the contents of the directory:
ls -la
The -la
command is called a flag. Flags modify the command you’re trying to run. In this case, l
results in a list display, and a
shows all the files.
You also can navigate to different folders using the command line using cd
. For example, if you want to navigate to the newdirectory
folder you just created, type this:
cd newdirectory
This changes the current directory to the new directory. To get back to the first directory, type in cd
.
Downloading and Installing Programs from the Command Line
We briefly explored how to install programs from the command line in the last chapter, but let’s take a closer look at what exactly you’re doing when you’re typing in those commands.
Unlike on Windows or Mac computers, Linux downloads programs as a package. This means that if you download a program, at the same time, you also download any other software you might need to run it. To download software, use the apt-get
command. Apt stands for advanced package tool. This downloads all the necessary software. For example, if you want to download the office suite LibreOffice, type in this:
sudo apt-get install libreoffice
Hit Enter, and the software will download and install.
PI POINTER
If you’re not sure the exact name of the software you want to install, you can search for it using the apt-cache search option. Just type in apt-cache search
at the command line followed by the type of software you’re looking for. This works even if you’re looking for something specific and just want to be sure you download the right software. For example, apt-cache search spotify
returns the exact download name for the Spotify music software.
You can also use this command to update all the software on your Raspberry Pi at once using the get upgrade
command. You’ll first need to see if any updates are available. To do this, type the following:
sudo apt-get update
You’ll get an output saying whether or not updates are available. If updates are available, type:
sudo apt-get upgrade
This command updates all the software on your Raspberry Pi to the newest version, including any updates to Raspbian itself.
You also can delete software this way using the remove
command. Let’s delete the version of LibreOffice we just downloaded but keep the configuration settings:
sudo apt-get remove libreoffice
When you’re downloading, installing, and removing software, you have to use the sudo
command because you need to be an administrator to install software on your system.
Other Common Commands
You’ll likely never need to use the bulk of the commands available for Linux, but here are a few more that will prove helpful as you work through the various tutorials in this book, and as you start to work on your own projects:
man
: Short for manual. If you type man
before any other command, you can learn exactly what that command does and how to properly use it.
help
: Similar to man
, help
can teach you how a command is used. Just type it in before any other command.
tar
/zip
/gzip
: These three commands extract or archive files. You’ll often need to do this after downloading a software suite.
cal
/date
: Need to get a quick look at the month’s calendar? Type cal
at any point. Prefer to get a look at the exact date? Type date
.
top
: When you type the top
command, you get a list of the programs currently running on your system. This can be helpful when you’re troubleshooting.
Ctrl+C: If you tap Ctrl+C on your keyboard at any time, you’ll stop the current process. This is handy if you make a mistake or if something is taking too long.
Essentially, if you can do a task with the mouse, you can probably do it from the command line. Conservatively, there are hundreds of different commands that do hundreds of different things. What’s covered in this chapter is plenty to get you through this book.
Editing Text Files from the Command Line
The command line isn’t just about moving around files, though. You can also edit files from the command line. Typically, you’ll only do this with TXT (text) files, but it’s also useful for editing several kinds of settings.
You can edit files with the nano
command.
Loading a Text File
To load a text file, use the nano
command. This brings up Linux’s text editor program.
If you want to manually edit the configuration file raspi-config created, for example, type the following:
sudo nano /boot/config.txt
You’re greeted with a text file with all the configuration settings (and more) you changed in raspi-config in Chapter 8. You can navigate this text editor with the arrow keys on your keyboard.
PI POINTER
You’ll notice that many lines in the config.txt file start with a #. This means they’re commented out. When you want to leave a comment in a text file like this, you can start a line with a # to tell the system not to run that command. If you delete the #, it will try to load that line.
Navigating, Editing, and Saving Files
Navigating around a text file is easy with your keyboard’s arrow keys. This changes the location of the cursor, and you can type anywhere the cursor is. To delete any text, use the Delete key on your keyboard.
A handful of keyboard shortcuts are available in the editor, as shown in the following table.
These are the basic commands you’ll need to know to use the text editor. You’ll use these often to edit configuration files when you’re troubleshooting or adding small lines of code to programs.
Get to know, and feel comfortable with, the command line. Remember, it’s not all that different from a text editor you’d find on Windows or Mac. With the command line, you just don’t have a mouse cursor to move around.
The Least You Need to Know