Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon

Programming with Linux

Save for later
  • 20 min read
  • 02 Jan 2017

article-image

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.)

  • Navigating a filesystem and viewing and searching the contents of a directory
  • Creating a new file, editing it in an editor, and changing ownership
  • Renaming and copying/moving the file/folder into a new directory
  • Installing and uninstalling a program

Navigating a filesystem and viewing and searching the contents of a directory

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.

Getting ready

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.

How to do it…

  1. If you want to find out where you are in the filesystem, use pwd:
    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.

  2. To look at the contents of the directory you are in, use the ls command:
    pi@rpz14101:~$ ls
    Desktop    Downloads  Pictures  python_games  share      Videos
    Documents  Music      Public    Scratch       Templates

  3. To look in another directory, simply specify the directory you want to list (you may need to use sudo depending on where you are looking):
    pi@rpz14101:~$ sudo ls /opt/
    cookbook.share  pigpio  sonic-pi           vc
    minecraft-pi    share   testsudo.deleteme  Wolfram

  4. This is a nice quick summary of what files are in the directory, but you will usually want a little more information about the files and directories. The ls command has a ton of options, all of which can be displayed with ls –help and explained in more detail with man ls. Some of the best ones to know are as follows:
    • -a show all files (regular and hidden)
    • -l show long format (more file information, in columns)
    • -h human readable (turns bytes into MB or GB as appropriate)
    • -t or -tr order in time order, or reverse time order

  5. My typical command when I start looking in a directory is this one:
    ls -ltrh

  6. This produces all non-hidden files, with human-readable sizes, in column format, and the newest file at the bottom:
    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

  7. One last trick: if you need this format but there are a lot of files in the directory you are searching, you will see a ton of text scroll by. Maybe you just need the most recent or largest files? We can do this with a pipe (|) and the tail command. Let’s take a directory with a lot of files, such as /usr/lib/. To list the five most recently modified files, I can pipe ls -ltrh to the tail command:
    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

  8. What about the five largest files? Instead of the t in -ltrh, I can use S:
    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

  9. A little creative piping and you can find exactly the file you are looking for. If not, another great tool for exploring the filesystem is tree. This gives a pseudo-graphical tree that shows how the files are structured in the system. It produces a lot of text, especially if you have it print an entire directory tree. If just looking into directory structures, you can use tree with the -d flag for directories only. The -L flag will reduce how deep you dive into nested directories:
    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

  10. Last, we will look at a couple of searching utilities, find and grep. The find command is a powerful function that finds files in whatever directories you specify. It is great for trying to find that mystery piece of software that installed itself in an odd place or the needle-in-a-haystack file in a directory that contains hundreds of files. For example, if I were to run tree in the /opt/sonic-pi/ directory, it would run on for several seconds, and thousands of files would shoot by. I, however, am only interested in finding files with cowbell in the name. I can use the find command to look for it:
    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.

  11. The grep command can be used in a couple different ways when searching for files, and it is one of those commands you will find yourself using constantly while both loving and hating its awesome power. Let’s say you need to find something inside of a file—grep is the tool for you. It can also find things like find can, but generally, find is more efficient at finding filename patterns than grep is.
  12. If I use grep to look for cowbells in my sonic-pi directory, I’ll get a different, and more colorful, output:

    programming-linux-img-0

    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).

  13. As you use Linux more often, both find and grep become regularly used tools for administration and file management. This won’t be the last time you use them!

Creating a new file, editing it in an editor, and changing ownership

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.

Getting ready

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

How to do it…

  1. First we will go to our share directory:
    cd /home/pi/share

  2. Then, we will create an empty file using the touch command:
    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.

  3. You can also create a new file with the editor itself:
    vi ch3_vifile.txt

    This will open the vi editor with a blank file named ch3_vifile.txt:

    programming-linux-img-1

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:

  1. With the empty file, you can jump into edit mode by pressing the i or a keys. The editor will switch to insert mode, as shown by the -- INSERT -- in the bottom left of the screen. Then you can you start typing in your text:

    programming-linux-img-2

  2. To get out of insert mode, press the Esc key. The :w command will save the file, and the :q command will quit. You can combine them, so :wq saves the file and quits. You can verify that the contents were saved with the cat command:
    pi@rpz14101:~$ cat ch3_vifile.txt
    Hello from the Raspberry Pi Zero Cookbook!

  3. Let’s take another look at the ls command and some of the information the -l format includes. We will take a look at the files we’ve created so far in this recipe:
    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


    Unlock access to the largest independent learning library in Tech for FREE!
    Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
    Renews at €18.99/month. Cancel anytime




    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

  4. We can see that since we made the files as the pi user, the owner of the file and the group owner are pi. By default, when a new user is created, a group container is created as well, so root has a root group, user rpz has an rpz group, and so on. We can change the ownership settings of a file with the chown command. Be careful, since you can take away your own access, though you can always sudo your way back. The chmod command will change who is allowed to do what with a file. Let’s look at ownership changes and what impact they will have with a few examples:
    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.

Renaming and copying/moving the file/folder into a new directory

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.

Getting ready

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

How to do it…

  1. First, let’s make a new directory. We’ll put it under the /home/pi/share/ folder so it is accessible to other computers on your home network. To make a directory, use the mkdir command:
    pi@rpz14101:~$ mkdir /home/pi/share/ch3

  2. We can look at the new directory with the ls command:
    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

  3. A great flag to go with the mkdir command is -p. This will allow you to create directories and subdirectories in one command. Without it, if I try to create a subdirectory that doesn’t already exist, I’ll get an error:
    pi@rpz14101:~$ mkdir /home/pi/share/ch3/nested/folders
    mkdir: cannot create directory ‘/home/pi/share/ch3/nested/folders’: No such file or directory

  4. With the -p flag, it works without a problem:
    pi@rpz14101:~$ mkdir -p /home/pi/share/ch3/nested/folders

  5. The tree command shows the structure of our ch3 directory:
    pi@rpz14101:~$ tree /home/pi/share
    /home/pi/share
    ├── ch3
    │   └── nested
    │       └── folders
    └── helloNetwork.yes
    3 directories, 1 file

  6. Now, let’s move our files to our new ch3 directory. The copy and move commands—cp and mv, respectively—are the tools we will use. Copying a file from one place to another is as simple as indicating the file's source and destination. The following command will make a copy of vifile.txt and save it as vifile.txt.copy in the /home/pi/share/ch3/ directory:
    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.

  7. To move or rename a file, we use the mv command. This takes the file given in the source and moves it to the destination provided. As simple as the cp command, let’s move all of our files to the share directory:
    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

  8. If we look at the tree of our share directory, we will see everything nicely organized:
    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

Installing and uninstalling a program

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.

Getting ready

Stay in your terminal window, and get ready to install some applications!

How to do it…

  1. The apt-* commands are a suite of utilities that allow you to do various things with installed packages. To install a package, we use the apt-get tool, and the install command, like this:
    sudo apt-get install <packagename>

  2. Let’s install something cool—how about a Matrix screensaver? It is super easy and works great from the command line. To look for a package, we use the apt-cache search command. apt-cache is another tool in the apt-* family of utilities, and it checks the software database for matches.
  3. Running sudo apt-cache search matrix results tons of results! The word "matrix" is a little too popular for us computer and math nerds—we have matrixes everywhere! It would take forever to go through that list to find what we are looking for. Fortunately, we can take advantage of grep, which we touched on in an earlier recipe, to narrow down our results. One of the fun things about using Linux and the command line is the ways you can chain commands to do cool things:
    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

  4. That’s a bit more manageable! We could also have narrowed the list using this command:
    sudo apt-cache search “The Matrix”

  5. This returns fewer results than before, but a few more than the grep command. Whichever way you find it, we see that the cmatrix package is the one we are looking for. Installing is as simple as running this:
    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) ...

  6. After that, we are ready to go! Channel your inner Neo and run this command:
    cmatrix -s -b

  7. You should be in the Matrix!

    programming-linux-img-3

  8. Try it on your serial and SSH connections, and even in the terminal on VNC: you’ll notice differences in the rendering behavior.

    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.

  9. Running sudo apt-get update will check all repositories to see whether there are any version updates available. Here, you can see all of the locations it checks to see whether there is anything new for Raspbian:
    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

  10. After updating, apt-get upgrade will look at the versions of everything you have installed and upgrade anything to the latest version if there is one available. Depending on how many updates you have, this can take quite a while:
    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.

There’s more…

  1. You can also add and remove software from the GUI. If you log on to your Pi, either directly to a monitor or over VNC Server (a recipe we covered earlier), you can find the Add / Remove Software option under Menu | Preferences:

    programming-linux-img-4

    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.

  2. The utility provides categorizations so you don’t have to scroll through every package. Clicking on a package provides a detailed description:

    programming-linux-img-5

  3. Simply check the box and click on Apply or OK, and the software will be installed. Now you can install software on your Raspberry Pi Zero from the command line or GUI.

Summary

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.

Resources for Article:


Further resources on this subject: