If you want to find out all the parameters for a particular command,
you can read the user manual with the man command (or you can
often use the --help option):
pi@raspberrypi ~ $ man curl
pi@raspberrypi ~ $ rm --help
To make a new directory, use mkdir. To bundle all of the files in a
directory into a single file, use the tar command, originally created
for tape archives.
You’ll find a lot of bundles of files or source code are distributed as
tar files, and they’re usually also compressed using the gzip com-
mand. Try this:
pi@raspberrypi ~ $ mkdir myDir
pi@raspberrypi ~ $ cdmy Dir
pi@raspberrypi ~ $ touch foo bar baz
pi@raspberrypi ~ $ cd ..
pi@raspberrypi ~ $ tar -cf myDir.tar myDir
pi@raspberrypi ~ $ gzip myDir.tar
You’ll now have a .tar.gz archive of that directory that can be distrib-
uted via email or the internet.
More Linux Commands
One of the reasons that Linux (and Unix) is so successful is that the
main design goal was to build a very complicated system out of small,
simple modular parts that can be chained together. You’ll need to
know a little bit about two pieces of this puzzle:
pipes
and
redirection
.
Pipes are simply a way of chaining two programs together so the out-
put of one can serve as the input to another. All Linux programs can
read data from
standard input
(often referred to as
stdin
), write data
to
standard output
(
stdout
), and throw error messages to
standard
error
(
stderr
). A pipe lets you hook up stdout from one program to
stdin of another (Figure 2-4). Use the | operator, as in this example:
pi@raspberrypi ~ $ ls -la | less
In the above example, the output of the ls command is sent to
the input of the less program, which prints data one screenful at
a time. (Press q to exit the
less program.)
Getting Around Linux on the Raspberry Pi 39
GSW_RASPI_4ED_FIN.indd 39GSW_RASPI_4ED_FIN.indd 39 10/28/21 10:54 AM10/28/21 10:54 AM
Figure 2-4. Pipes are a way of chaining smaller programs together
to accomplish bigger tasks.
Now (for something a little more out there) try:
pi@raspberrypi ~ $ sudo cat/boot/kernel.img | aplay
You may want to turn the volume down a bit first; this command
reads the kernel image and spits all of the 1s and 0s at the audio-
player. That’s what your kernel sounds like!
Amusing Digression
sudo is a command that stands for “super user do”. It is a way for an
ordinary computer user, like you, to temporarily gain super user
powers on a Linux system. This gives you the ability to change the
system greatly, which can do a lot of damage if you’re not careful.
Treat sudo with respect.
In some of the examples later in the book, we’ll also be using
redi-
rection
, where a command is executed and the stdout output can
be sent to a file. As you’ll see later, many things in Linux are treated
like ordinary files (such as the Pi’s general-purpose input/output
40 Getting Started with Raspberry Pi
GSW_RASPI_4ED_FIN.indd 40GSW_RASPI_4ED_FIN.indd 40 10/28/21 10:54 AM10/28/21 10:54 AM
pins), so redirection can be quite handy. To redirect output from
a program, use the > operator:
pi@raspberrypi ~ $ ls > directoryListing.txt
Special Control Keys
In addition to the keys for auto complete (Tab) and command
history (up arrow) previously mentioned, there are a few other-
special control keys you’ll need in the shell. Here are a few:
Ctrl-C
Kills the running program. May not work with some interac-
tive programs such as text editors.
Ctrl-D
Exits the shell. You must type this at the command prompt by
itself (don’t type anything after the $ before hitting Ctrl-D).
Ctrl-A
Moves the cursor to the beginning of the line.
Ctrl-E
Moves the cursor to the end of the line.
There are others, but these are the core keyboard shortcuts
you’ll use every day.
Sometimes you’ll want to display the contents of a file on the
screen. If it’s a text file and you want to read it one screen at a time,
use less:
pi@raspberrypi ~ $ ls >ob.txt
pi@raspberrypi ~ $ less ob.txt
(Another option for reading one screen at a time is more (I know,
I know...). Most power users use less, however, as it’s a bit more
functional than more (less is more than more...?).
If you want to just dump the entire contents of a file to standard
output, use cat (short for concatenate). This can be handy when
Getting Around Linux on the Raspberry Pi 41
GSW_RASPI_4ED_FIN.indd 41GSW_RASPI_4ED_FIN.indd 41 10/28/21 10:54 AM10/28/21 10:54 AM
you want to feed a file into another program or redirect it some-
where.
For example, this is the equivalent of copying one file to another
with a new name (the second line concatenates the two files first):
pi@raspberrypi ~ $ ls >wibble.txt
pi@raspberrypi ~ $ cat wibble.txt > wobble.txt
pi@raspberrypi ~ $ cat wibble.txt wobble.txt > wubble.txt
To look at just the last few lines of a file (such as the most recent
entry in a log file), use tail (to see the beginning, use head). If you
are searching for a string in one or more files, use the venerable
program grep:
pi@raspberrypi ~ $ grep Puzzle */*
grep is a powerful tool because of the rich language of
regular
expressions
that was developed for it. Regular expressions can be
a bit difficult to read—going into detail of how regular expressions
work could fill an entire book—and maybe a major factor in whatev-
er reputation Linux has for being opaque to newcomers.
Processes
Even when the Pi is “doing nothing”, its operating system is very
busy, maintaining the file system, monitoring the network connec-
tion, and many other tasks to keep your Pi humming. Every pro-
gram on the Pi runs as a separate process; at any particular point,
you’ll have dozens of processes running. When you first bootup,
about 75 processes will start, each one handling a different task
or service. To see all these processes, run the top program, which
will also display CPU and memory usage. top will show you the pro-
cesses using the most resources; use the ps command to list all the
processes and their ID numbers (see Figure 2-5).
42 Getting Started with Raspberry Pi
GSW_RASPI_4ED_FIN.indd 42GSW_RASPI_4ED_FIN.indd 42 10/28/21 10:54 AM10/28/21 10:54 AM
Figure 2-5. The top command shows all the processes running, as
well as CPU and memory usage.
Try:
pi@raspberrypi ~ $ ps aux | less
Sometimes you may want to kill a rogue or unresponsive process.
To do that, use ps to find its ID, then use kill to stop it:
pi@raspberrypi ~ $ kill 95689
In the case of some system processes, you won’t have permission
to kill it (though you can circumvent this with sudo, covered in the
next section).
Sudo and Permissions
Linux is a multiuser operating system; the general rule is that
everyone owns their own files and can create, modify, and delete
them within their own space on the filesystem. The root (or super)
user can change any file in the filesystem, which is why it is good
practice to not login as root on a day-to-day basis. There is a saying
among Linux users: “Only noobs log in as root.
There are some tools like sudo (su–peruser–do”) which allow users
to act like superusers for performing tasks, like installing software
Getting Around Linux on the Raspberry Pi 43
GSW_RASPI_4ED_FIN.indd 43GSW_RASPI_4ED_FIN.indd 43 10/28/21 10:54 AM10/28/21 10:54 AM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset