The Raspberry Pi is an awesome physical computing platform. Hook it up to an HDMI monitor, add a keyboard/mouse and you’ll have a nice, albeit-lightweight, desktop computing device. It has a handful of general-purpose (digital) input/output pins for gathering data and controlling output gizmos. With a little imagination, the Pi adds smarts to all kinds of physical computing projects.
The Pi runs on Linux and uses the text-based command-line as the default user interface. You can execute any program, control every process, and perform any task from the command-line, with the proper permissions and good sense, of course.
Many Pi users are intimidated by the prospect of having to use the command-line though. It’s unfamiliar and lay-people see it as “not user-friendly.” Ignore all that. It’s not hard to learn with some guidance and practice. I strongly recommend off-the-shelf hackers invest in learning the command-line since it is a fundamental, time-saving skill for working on Linux-based systems.
The command-line is accessed through a terminal. Both are old-school, fast and effective.
A terminal is nothing more than a text window, either hardware or virtual, for the Linux command-line.
In my early days at Purdue University, we used physical hardware terminals to connect to the Unix operating system running on the university machines. These old cathode-ray tube (CRT) monitor boxes had glowing-green text and a keyboard at the bottom. They sat on long tables, in dimly lit basement rooms of various gigantic campus buildings. You’d sit on a folding chair at an open terminal, log in using your assigned Unix user name/password pair then call up the venerable vi text editor to type in your code. You’d compile and run your program then hopefully watch your output text scroll up the screen as it executed. It was always an accomplishment if the program compiled. Sometimes the program even worked correctly.
The modern Linux terminal window is just like the monitors back at school, except you view it on a desktop display. You can open as many terminal windows as you like, although I rarely use more than five or ten at a time. Linux, like Unix, is a multitasking, multi-user operating system. You can run lots of separate processes, with multiple users if you like. Typically, it will just be one user (you) running multiples processes, on the Pi.
I’ve used terminals and the command-line for a very long time.
Buddy-Up to the Command-Line with a Terminal
The first thing to do is actually open a terminal. On a running Raspberry Pi desktop, click the terminal icon (tiny black window with a “>_” symbols) and a new terminal window will pop up. Click the maximize symbol (the tiny square box at the upper right top of the window) to expand the terminal to nearly full screen. You can also grab a corner and left-click it to drag the window to a larger size.
Notice the command line prompt. My Raspberry Pi, running Raspbian Linux version 9 (stretch) shows a “pi@hedley:~$” prompt. The pi part is the user and hedley is the host (machine) name. The ~ indicates that we are at the home directory of the pi user. A directory is just a branch in the tree structure of the file system. Let’s explore that topic further, shall we.
Your First Command-Line Programs
ls (“list”) is a basic command-line program that gives you a list of files in the current directory. If you type “ls” at the command-line, followed by the “Enter” key, you’ll get a simple printout like the following.
You can also add command-line options to make the command behave differently. Type an “ls –help” to get help for the ls command. Or, you might type an “ls -l” to show a long listing of the files. Options are shown under help.
Along the left side are the file permissions. A “-” in the first column indicates a regular file, while a “d” is listing a directory. The numbers in the 5th column indicate the file size in bytes, followed by columns showing the creation/last change date of the file or directory. The right column is the file or directory name. I use different flavors of ls all the time.
cd (“change directory”) is a command that changes your file directory. Just like with ls, if you type a “cd –help” you’ll get help for the cd command.
Remember those directories that showed up with the ls -l command? You can traverse down into a directory by simply typing cd and the directory name. For example “cd sketchbook” will move you down into the sketchbook directory. You can use “cd ..” to move back up to the directory above where ever you are currently. If you were in the /home/pi directory and cd’d down into sketchbook. “cd ..” will return you to the /home/pi directory. Just typing “cd” by itself will teleport you to your home directory, typically /home/pi. Use the “pwd” command to find out your present working directory.
While there are a few options I usually don’t use any with cd.
Our last command-line program for this session is cat (“concatenate”). cat prints a file to the terminal. Say you have a text file named rob.txt containing a short off-the-shelf hacker article. I could type “cat rob.txt” to see the sentences of the story. If it is a large file and quickly scrolls past the bottom of the screen you can always grab the window slider, on the right side of the terminal, to move up and down through the file. There aren’t many options for cat. A quick “cat” and the file name is what I use.
When you are finished with a terminal window, simply type “exit” at the command line and it will go poof. Simple as that.
No Fear Tech
Worrying about the command-line under Linux certainly shouldn’t keep you from inventing the next big physical computing gadget with a Raspberry Pi. After all, you are a fearless off-the-shelf hacker. Tech doesn’t intimidate you!
While it’s definitely possible to mess things up, from the terminal, the risk is pretty low. I’ll share a few personal mishaps in a future article. If you know about what to watch out for, you’ll have more confidence in what to expect as you gain experience using the command-line.
Catch Dr. Torq’s Off-The-Shelf Hacker column, each Saturday, only on The New Stack! Contact him directly for consulting, speaking appearances and commissioned projects at email@example.com or 407-718-3274.