In this article by Edward Snajder, the author of Raspberry Pi Zero Cookbook, we pick up from having our operating system installed and our Raspberry Pi Zero on our home network. We can now dive into some basic Linux commands. You will find knowing these commands useful any time you are working on a Linux machine. In this article, we'll start prepping with some Linux recipes:
(For more resources related to this topic, see here.)
If you aren’t already a Linux or Mac user, getting around the filesystem can seem pretty alien at first. Truly, if you’ve only used Windows Explorer, this is going to seem like a strange, alien process. Once you start getting the hang of things, though, you’ll find that getting around the Linux filesystem is easy and fun.
The only thing you need to get started is a client connection to your Raspberry Pi Zero. I like to use SSH, but you can certainly connect using the serial connection or a terminal in X Windows.
pi@rpz14101:~$ pwd
/home/pi
This tells me I’m in the /home/pi directory, which is the default home directory for the pi user. Generally, every user you create should get a /home/username directory to keep their own files in. This can be done automatically with user creation and the adduser command.
pi@rpz14101:~$ ls
Desktop Downloads Pictures python_games share Videos
Documents Music Public Scratch Templates
pi@rpz14101:~$ sudo ls /opt/
cookbook.share pigpio sonic-pi vc
minecraft-pi share testsudo.deleteme Wolfram
ls -ltrh
pi@rpz14101:~$ ls -ltrh /opt/
total 513M
drwxr-xr-x 7 root root 4.0K May 27 04:11 vc
drwxr-xr-x 3 root root 4.0K May 27 04:32 Wolfram
drwxr-xr-x 3 root root 4.0K May 27 04:34 pigpio
drwxr-xr-x 4 root root 4.0K May 27 04:36 minecraft-pi
drwxr-xr-x 5 root root 4.0K May 27 04:36 sonic-pi
-rw-r--r-- 1 root root 0 Jul 4 13:41 testsudo.deleteme
drwxr-xr-x 2 root root 4.0K Jul 9 13:05 share
-rwxr-xr-x 1 root root 512M Jul 24 17:53 cookbook.share
pi@rpz14101:~$ ls -ltrh /usr/lib/ | tail -5
lrwxrwxrwx 1 root root 22 May 27 04:40 libwiringPiDev.so -> libwiringPiDev.so.2.32
drwxr-xr-x 2 root root 4.0K Jun 5 10:38 samba
drwxr-xr-x 3 root root 4.0K Jul 4 22:48 pppd
drwxr-xr-x 65 root root 60K Jul 24 15:48 arm-linux-gnueabihf
drwxr-xr-x 2 root root 4.0K Jul 24 15:48 tmpfiles.d
pi@rpz14101:~$ ls -lSrh /usr/lib/ | tail -5
-rw-r--r-- 1 root root 2.8M Sep 17 2014 libmozjs185.so.1.0.0
-rw-r--r-- 1 root root 2.8M Sep 30 2014 libqscintilla2.so.11.3.0
-rw-r--r-- 1 root root 2.9M Jun 5 2014 libcmis-0.4.so.4.0.1
-rw-r--r-- 1 root root 3.4M Jun 12 2015 libv8.so.3.14.5
-rw-r--r-- 1 root root 5.1M Aug 18 2014 libmwaw-0.3.so.3.0.1
pi@rpz14101:~$ tree -d -L 2 /opt/
/opt/
├── minecraft-pi
│ ├── api
│ └── data
├── pigpio
│ └── cgi
├── share
├── sonic-pi
│ ├── app
│ ├── bin
│ └── etc
├── vc
│ ├── bin
│ ├── include
│ ├── lib
│ ├── sbin
│ └── src
└── Wolfram
└── WolframEngine
pi@rpz14101:~$ find /opt/sonic-pi/ -name *cowbell*
/opt/sonic-pi/etc/samples/drum_cowbell.flac
When looking for anything with cowbell in the filename, the find command returns the exactly location of anything that matches. There are tons of options for using the find command; start with find –help, and then try man find when you want to get really deep.
We don’t see the file with cowbell in the name like we did using find, but we find every file that contains cowbell inside of it. The -r flag tells grep to delve into subdirectories, and -i tells it to ignore cases with cowbells (so Cowbell and cowbell are both found, as shown in the screenshot).
There are a lot of different text editors to use on a Linux system from the command line. The program vi is the Ubuntu default, and the program you will find installed on pretty much any Linux system. Emacs is another popular editor, and lots of Linux users get quite passionate about which one is better. My preference is vim, which is generally known as vi improved. The nano text editor is another one that is commonly installed on Linux distros, and it is one of the most lightweight editors available.
For this recipe, we will work with vi, since that’s definitely going to be installed on your system. If you want to try out vim, you can install it using this:
sudo apt-get vim
cd /home/pi/share
touch ch3_touchfile.txt
If you use the ls command from the previous directory, you can see that the size of the file is 0. You can also display the contents of the file with the cat command, which will return nothing in this case. The touch command is a great way to test whether you have permissions to create files in a specific directory.
vi ch3_vifile.txt
This will open the vi editor with a blank file named ch3_vifile.txt:
Using vi or vim (or Emacs) for the first time is completely different from using something like OpenOffice or Microsoft Word. Vi works in two modes: insert (or edit) and command. Once you learn how to use command mode, vi becomes a very efficient editor for working on scripts in bash or Python. Edit mode, more or less, is the mode where you can type and edit text like a regular WYSIWYG editor. There are books written on becoming a power user of vi, well beyond the scope of this book. Getting a handle on the basics is the best place to start:
pi@rpz14101:~$ cat ch3_vifile.txt
Hello from the Raspberry Pi Zero Cookbook!
pi@rpz14101:~$ ls -ltrh *.txt
-rw-r--r-- 1 pi pi 43 Jul 25 11:23 ch3_vifile.txt
-rw-r--r-- 1 pi pi 0 Jul 25 11:24 ch3_touchfile.txt
File Permissions
|
Number of links
|
Owner:Group
|
Size
|
Modification Date
|
File Name
|
-rw-r--r--
|
1
|
pi:pi
|
43
|
Jul 25 11:23
|
ch3_vifile.txt
|
pi@rpz14101:~ $ ls -ltrh *.txt
-rw-r--r-- 1 pi pi 0 Jul 25 13:28 ch3_touchfile.txt
-rwx------ 1 pi pi 43 Jul 25 13:28 ch3_vifile.txt
pi@rpz14101:~ $ cat ch3_vifile.txt
Hello from the Raspberry Pi Zero Cookbook!
pi@rpz14101:~ $ sudo chown rpz:rpz ch3_vifile.txt
pi@rpz14101:~ $ cat ch3_vifile.txt
cat: ch3_vifile.txt: Permission denied
pi@rpz14101:~ $ sudo cat ch3_vifile.txt
Hello from the Raspberry Pi Zero Cookbook!
pi@rpz14101:~ $ sudo chown rpz:pi ch3_vifile.txt
pi@rpz14101:~ $ cat ch3_vifile.txt
cat: ch3_vifile.txt: Permission denied
pi@rpz14101:~ $ sudo chmod 750 ch3_vifile.txt
pi@rpz14101:~ $ cat ch3_vifile.txt
Hello from the Raspberry Pi Zero Cookbook!
pi@rpz14101:~ $ sudo chown root:root ch3_vifile.txt
pi@rpz14101:~ $ cat ch3_vifile.txt
cat: ch3_vifile.txt: Permission denied
pi@rpz14101:~ $ sudo chmod 755 ch3_vifile.txt
pi@rpz14101:~ $ cat ch3_vifile.txt
Hello from the Raspberry Pi Zero Cookbook!
The chmod values are documented very well, and with a little practice, you can get your file permissions and ownership set up in a way that is both secure and easy to work with.
A common activity on any filesystem is the practice of copying and moving files, and even directories, from one place to another. You might do it to make a backup copy of something, or you might decide that the contents should live in a more appropriate location. This recipe will explore how to manipulate files in the Raspbian system.
If you are still in your terminal from the last recipe, we are going to use the same files from the previous recipe. We should have the ownership back to pi:pi; if not, run the following:
sudo chown pi:pi /home/pi/*.txt
pi@rpz14101:~$ mkdir /home/pi/share/ch3
pi@rpz14101:~$ ls -ltrh /home/pi/share/
total 4.0K
-rw-r--r-- 1 pi pi 0 Jul 24 15:56 helloNetwork.yes
drwxr-xr-x 2 pi pi 4.0K Jul 25 13:06 ch3
pi@rpz14101:~$ mkdir /home/pi/share/ch3/nested/folders
mkdir: cannot create directory ‘/home/pi/share/ch3/nested/folders’: No such file or directory
pi@rpz14101:~$ mkdir -p /home/pi/share/ch3/nested/folders
pi@rpz14101:~$ tree /home/pi/share
/home/pi/share
├── ch3
│ └── nested
│ └── folders
└── helloNetwork.yes
3 directories, 1 file
cp /home/pi/ch3_vifile.txt /home/pi/share/ch3/ch3_vifile.txt.copy
We can copy files as well as directories and their contents as long as you have enough disk space.
mv /home/pi/ch3_vifile.txt /home/pi/share/ch3/ch3_vifile.txt.moved
mv /home/pi/ch3_touchfile.txt /home/pi/share/ch3/ch3_touchfile.txt
pi@rpz14101:~$ tree /home/pi/share/
/home/pi/share/
├── ch3
│ ├── ch3_touchfile.txt
│ ├── ch3_vifile.txt.copy
│ ├── ch3_vifile.txt.moved
│ └── nested
│ └── folders
└── helloNetwork.yes
3 directories, 4 files
We’ve installed a few programs throughout the book so far, but have yet to delve into the apt-get command and the family of software-installation utilities. Now, we will learn how to install and uninstall any program available for Raspbian as well as how to search for new software and run updates.
Stay in your terminal window, and get ready to install some applications!
sudo apt-get install <packagename>
pi@rpz14101:~ $ sudo apt-cache search matrix | grep "The Matrix"
cmatrix - simulates the display from "The Matrix"
wmmatrix - View The Matrix in a Window Maker dock application
sudo apt-cache search “The Matrix”
pi@rpz14101:~ $ `
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
cmatrix-xfont
The following NEW packages will be installed:
cmatrix
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 16.2 kB of archives.
After this operation, 27.6 kB of additional disk space will be used.
Get:1 http://mirrordirector.raspbian.org/raspbian/ jessie/main cmatrix armhf 1.2a-5 [16.2 kB]
Fetched 16.2 kB in 1s (15.3 kB/s)
Selecting previously unselected package cmatrix.
(Reading database ... 121906 files and directories currently installed.)
Preparing to unpack .../cmatrix_1.2a-5_armhf.deb ...
Unpacking cmatrix (1.2a-5) ...
Processing triggers for man-db (2.7.0.2-5) ...
Setting up cmatrix (1.2a-5) ...
cmatrix -s -b
There are literally thousands of software packages available to install in the repositories of our awesome open source communities. Pretty much anything you think a computer should be able to do, someone, or a group of people, has worked on a solution and pushed it out to the repositories. We will be using apt-get a lot throughout this cookbook; it is one of the commands you’ll find yourself using all the time as you get more interested in Raspberry Pis and the Linux operating system.
pi@rpz14101:~ $ sudo apt-get update
Get:1 http://archive.raspberrypi.org jessie InRelease [13.2 kB]
Get:2 http://mirrordirector.raspbian.org jessie InRelease [14.9 kB]
Get:3 http://archive.raspberrypi.org jessie/main armhf Packages [144 kB]
Get:4 http://mirrordirector.raspbian.org jessie/main armhf Packages [8,981 kB]
Hit http://archive.raspberrypi.org jessie/ui armhf Packages
Ign http://archive.raspberrypi.org jessie/main Translation-en_GB
Get:5 http://mirrordirector.raspbian.org jessie/contrib armhf Packages [37.5 kB]
Ign http://archive.raspberrypi.org jessie/main Translation-en
Get:6 http://mirrordirector.raspbian.org jessie/non-free armhf Packages [70.3 kB]
Ign http://archive.raspberrypi.org jessie/ui Translation-en_GB
Ign http://archive.raspberrypi.org jessie/ui Translation-en
Get:7 http://mirrordirector.raspbian.org jessie/rpi armhf Packages [1,356 B]
Ign http://mirrordirector.raspbian.org jessie/contrib Translation-en_GB
Ign http://mirrordirector.raspbian.org jessie/contrib Translation-en
Ign http://mirrordirector.raspbian.org jessie/main Translation-en_GB
Ign http://mirrordirector.raspbian.org jessie/main Translation-en
Ign http://mirrordirector.raspbian.org jessie/non-free Translation-en_GB
Ign http://mirrordirector.raspbian.org jessie/non-free Translation-en
Ign http://mirrordirector.raspbian.org jessie/rpi Translation-en_GB
Ign http://mirrordirector.raspbian.org jessie/rpi Translation-en
Fetched 9,263 kB in 34s (272 kB/s)
Reading package lists... Done
pi@rpz14101:~ $ sudo apt-get upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following packages will be upgraded:
dpkg-dev gir1.2-gdkpixbuf-2.0 initramfs-tools libavcodec56 libavformat56 libavresample2
libavutil54 libdevmapper-event1.02.1 libdevmapper1.02.1 libdpkg-perl
python-picamera python3-picamera raspberrypi-kernel raspberrypi-net-mods ssh tzdata xarchiver
40 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 57.0 MB of archives.
After this operation, 415 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.raspberrypi.org/debian/ jessie/main nodered armhf 0.14.5 [5,578 kB]
…
Adding 'diversion of /boot/overlays/w1-gpio.dtbo to /usr/share/rpikernelhack/overlays/w1-gpio.dtbo by rpikernelhack'
…
run-parts: executing /etc/kernel/postrm.d/initramfs-tools 4.4.11-v7+ /boot/kernel7.img
Preparing to unpack .../raspberrypi-net-mods_1.2.3_armhf.deb ...
Unpacking raspberrypi-net-mods (1.2.3) over (1.2.2) ...
Processing triggers for man-db (2.7.0.2-5) ...
…
Setting up libssl1.0.0:armhf (1.0.1t-1+deb8u2) ...
Setting up libxml2:armhf (2.9.1+dfsg1-5+deb8u2) ...
…
Removing 'diversion of /boot/overlays/w1-gpio-pullup.dtbo to /usr/share/rpikernelhack/overlays/w1-gpio-pullup.dtbo by rpikernelhack'
…
Setting up raspberrypi-net-mods (1.2.3) ...
Modified /etc/network/interfaces detected. Leaving unchanged and writing new file as interfaces.new.
Processing triggers for libc-bin (2.19-18+deb8u4) ...
Processing triggers for initramfs-tools (0.120+deb8u2) ...
You don’t really have to understand the details of what’s going on during the upgrade, and it will let you know if there were any problems at the end (and often what to do to fix them). Regularly updating and upgrading will keep all of your software current with all of the latest bug fixes and security patches.
The PiPackages utility makes it very easy to find software when you only have a general idea of what you are looking for. While you can do the same things with the apt commands, if you are browsing, this is a little easier on the eyes.
In this article, we looked at the basic file manipulation functionalities of Linux on a Raspberry Pi. We saw how to navigate the filesystem and create, edit, rename, copy, and move files and folders. We also saw how to change ownership of a file and install and uninstall programs.
Further resources on this subject: