Terminal Navigation - Raspberry Pi tutorial




Welcome to part 4 of the Raspberry Pi tutorial series. In this part, we're going to have a quick crash course for using the terminal. Most of our interactions with the Raspberry Pi will be via shell, since this is the simplest and most lightweight to keep your Pi accessible.

To begin, let's assume you've just logged in. The location that you will usually log in to will be the root directory for your user. In this case, our user is "pi," so we log in to /home/pi. We can confirm this by using the command: pwd (print working directory)

pi@raspberrypi:~ $ pwd
/home/pi

Often times, to denote the terminal, you will see people use the $ sign. Since, most of the time, people wont be using the same usernames and hostnames, this is the standard to denote that we're operating in the terminal, so, instead, if you googled how you get your current directory in linux, you'd probably see: $ pwd. This doesn't mean that you should actually type the $ sign, it's just meant to denote that you're in the terminal.

Next, many times you might see a "permission denied" error when trying to do something. For many tasks, you need to act as the super user, like the administrator account. The command to act as the super user is sudo, which is short for "super user do." An example of this was when we wanted to update and upgrade, we used sudo. Be careful when using sudo to create files...etc. The creator is the owner, so if you use sudo to create the file, it can only be further edited by the super user. Many files will require sudo to edit, however.

To move around the system, you can use cd, which stands for "change directory." At any time, we can use ~ to reference the current user's home. In our case ~ is short for /home/pi. We can change directories into there with $ cd ~.

We were already there, but, just in case you weren't, you are now!

Next, to discover the contents of the directory you are in, you can use ls to list directory contents:

$ ls

pi@raspberrypi:~ $ ls
Desktop  Documents  Downloads  Music  Pictures  Public  python_games  Templates  Videos

It wont always be the case, but often you will see different colors for different types of files. In our case, we just have directories, so they are all the same color.

Let's assume we want to make our own directory, to do this, we can use the mkdir command, short for make dir.

$ mkdir example

We can list the contents again:

$ ls

Seeing our new directory:

pi@raspberrypi:~ $ ls
Desktop  Documents  Downloads  example  Music  Pictures  Public  python_games  Templates  Videos

Then we can change directories into our new directory:

$ cd example

Maybe we don't want to be here. We can move backwards with: $ cd ..

pi@raspberrypi:~/example $ cd ..
pi@raspberrypi:~ $

We can also move back a few directories at a time: $cd ../../

pi@raspberrypi:~ $ cd ../../
pi@raspberrypi:/ $ ls
bin  boot  debian-binary  dev  etc  home  lib  lost+found  man  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

We're now in the root directory for the entire Raspberry Pi. We don't really want to me here doing things, we can make some big mistakes, let's get back to our user's home!

pi@raspberrypi:/ $ cd ~
pi@raspberrypi:~ $

Phew, we're safe.

Let's go into our example dir now:

$ cd example

Next, we can create a simple file with any of the built in editors. There are many to choose from, I think nano is the easiest, so I will use that:

$ nano test.py

Nano can be used to both create or edit files. If the file doesn't yet exist, it will be created. If it does exist, it will allow you to save a new one.

Let's just add: print("hi") to this file. When done, we can exit with control+x, then press y to save, and then enter to keep the name.

To run this file, we can do: $ python test.py

pi@raspberrypi:~/example $ python test.py
hi

Next, let's make another directory:

$ mkdir downloads

$ cd downloads

Let's talk about getting some common things. First, what if we want to grab a certain file from the internet. Let's use this image as an example: https://pythonprogramming.net/static/images/categories/3D-Matplotlib.png

We can use wget to download this file, like so:

wget https://pythonprogramming.net/static/images/categories/3D-Matplotlib.png

Rather than manually typing out that link, we can copy the link, then right click in the terminal to paste it out.

$ ls

pi@raspberrypi:~/example/downloads $ ls
3D-Matplotlib.png

Another useful tool is git. Let's make sure it is installed with $ sudo apt-get install git, but you should already have it on the Pi.

We can use this to clone Github repositories. For example, we can clone Google's python-fire:

pi@raspberrypi:~/example/downloads $ git clone https://github.com/google/python-fire
Cloning into 'python-fire'...
remote: Counting objects: 217, done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 217 (delta 1), reused 0 (delta 0), pack-reused 206
Receiving objects: 100% (217/217), 113.44 KiB | 0 bytes/s, done.
Resolving deltas: 100% (115/115), done.
Checking connectivity... done.
pi@raspberrypi:~/example/downloads $ ls
3D-Matplotlib.png  python-fire

Next, we can copy and move files. We'll start with a copy (cp):

pi@raspberrypi:~/example/downloads $ cp 3D-Matplotlib.png 3D-Matplotlib-2.png
pi@raspberrypi:~/example/downloads $ ls
3D-Matplotlib-2.png  3D-Matplotlib.png  python-fire

We can also move files with mv:

pi@raspberrypi:~/example/downloads $ mv 3D-Matplotlib-2.png ../3D-Matplotlib-2.png
pi@raspberrypi:~/example/downloads $ ls
3D-Matplotlib.png  python-fire

It's no longer here since we moved it to the parent directory:

pi@raspberrypi:~/example/downloads $ cd ../
pi@raspberrypi:~/example $ ls
3D-Matplotlib-2.png  downloads  test.py

Now let's talk about removing things. Keep in mind, there's no undo here. If you delete something, it's gone. To remove a file, we can use rm:

pi@raspberrypi:~/example $ rm 3D-Matplotlib-2.png
pi@raspberrypi:~/example $ ls
downloads  test.py

The image is now gone. Next, you can remove directories with rmdir, let's make an example:

pi@raspberrypi:~/example $ mkdir delme
pi@raspberrypi:~/example $ ls
delme  downloads  test.py
pi@raspberrypi:~/example $ rmdir delme
pi@raspberrypi:~/example $ ls
downloads  test.py

What if we try to remove the downloads directory?

pi@raspberrypi:~/example $ rmdir downloads/
rmdir: failed to remove ‘downloads/’: Directory not empty

For this reason, you will very rarely use rmdir. Instead, the way you typically delete directories is to remove recursively. To do this, you do rm -r, where the -r is an argument that stands for recursive:

pi@raspberrypi:~/example $ rm -r downloads/
rm: remove write-protected regular file ‘downloads/python-fire/.git/objects/pack/pack-4d32cc7660d49c1192b1fe6f9a1ce4918b6ff521.idx’?
rm: remove write-protected regular file ‘downloads/python-fire/.git/objects/pack/pack-4d32cc7660d49c1192b1fe6f9a1ce4918b6ff521.pack’?
rm: cannot remove ‘downloads/python-fire/.git/objects/pack’: Directory not empty

In this case, it looks like the files wont go without a fight:

pi@raspberrypi:~/example $ sudo rm -r downloads/
pi@raspberrypi:~/example $ ls
test.py

All gone!

Finally, just as a note, you will often see commands that have options. these are denoted with - or --. A single dash is used for single-character options, like ls -a we used before. The double dash is for a full word. The convention is that single-character options also have full ones. in the case of ls, it's ls --all.

pi@raspberrypi:~ $ cd ~
pi@raspberrypi:~ $ ls
Desktop  Documents  Downloads  example  Music  Pictures  Public  python_games  Templates  Videos
pi@raspberrypi:~ $ ls -a
.              .bash_logout  .config  Documents  .gconf           Music     .pki      python_games  .thumbnails  .Xauthority
..             .bashrc       .dbus    Downloads  .gstreamer-0.10  Pictures  .profile  Templates     Videos       .xsession-errors
.bash_history  .cache        Desktop  example    .local           .pip      Public    .themes       .vnc         .xsession-errors.old
pi@raspberrypi:~ $ ls --all
.              .bash_logout  .config  Documents  .gconf           Music     .pki      python_games  .thumbnails  .Xauthority
..             .bashrc       .dbus    Downloads  .gstreamer-0.10  Pictures  .profile  Templates     Videos       .xsession-errors
.bash_history  .cache        Desktop  example    .local           .pip      Public    .themes       .vnc         .xsession-errors.old

One more thing to note about the single-character options is that you can combine them. For example:

pi@raspberrypi:~ $ ls -al
total 120
drwxr-xr-x 23 pi   pi   4096 Mar 29 19:54 .
drwxr-xr-x  3 root root 4096 Mar  3 15:27 ..
-rw-------  1 pi   pi   1267 Mar 29 14:31 .bash_history
-rw-r--r--  1 pi   pi    220 Mar  3 15:27 .bash_logout
-rw-r--r--  1 pi   pi   3512 Mar  3 15:27 .bashrc
drwxr-xr-x  7 pi   pi   4096 Mar 29 15:09 .cache
drwx------ 10 pi   pi   4096 Mar 29 14:05 .config
drwx------  3 pi   pi   4096 Mar  3 16:24 .dbus
drwxr-xr-x  2 pi   pi   4096 Mar 29 15:10 Desktop
drwxr-xr-x  5 pi   pi   4096 Mar  3 15:55 Documents
drwxr-xr-x  2 pi   pi   4096 Mar  3 16:24 Downloads
drwxr-xr-x  2 pi   pi   4096 Mar 29 22:35 example
drwx------  2 pi   pi   4096 Mar 29 14:05 .gconf
drwxr-xr-x  2 pi   pi   4096 Mar  3 16:25 .gstreamer-0.10
drwxr-xr-x  3 pi   pi   4096 Mar  3 15:55 .local
drwxr-xr-x  2 pi   pi   4096 Mar  3 16:24 Music
drwxr-xr-x  2 pi   pi   4096 Mar  3 16:24 Pictures
drwxr-xr-x  2 pi   pi   4096 Mar 29 02:29 .pip
drwx------  3 pi   pi   4096 Mar 29 14:05 .pki
-rw-r--r--  1 pi   pi    675 Mar  3 15:27 .profile
drwxr-xr-x  2 pi   pi   4096 Mar  3 16:24 Public
drwxr-xr-x  2 pi   pi   4096 Mar  3 15:55 python_games
drwxr-xr-x  2 pi   pi   4096 Mar  3 16:24 Templates
drwxr-xr-x  3 pi   pi   4096 Mar  3 16:24 .themes
drwx------  4 pi   pi   4096 Mar 29 14:02 .thumbnails
drwxr-xr-x  2 pi   pi   4096 Mar  3 16:24 Videos
drwx------  3 pi   pi   4096 Mar 29 15:03 .vnc
-rw-------  1 pi   pi     56 Mar 29 14:31 .Xauthority
-rw-------  1 pi   pi   1369 Mar 29 15:09 .xsession-errors
-rw-------  1 pi   pi    532 Mar 29 14:07 .xsession-errors.old

But hmm. what is this l? If you want to know more about the command you read online, or even if you want to see what else you can do, you can use the --help flag.

pi@raspberrypi:~ $ ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

Mandatory arguments to long options are mandatory for short options too.
  -a, --all                  do not ignore entries starting with .
  -A, --almost-all           do not list implied . and ..
      --author               with -l, print the author of each file
  -b, --escape               print C-style escapes for nongraphic characters
      --block-size=SIZE      scale sizes by SIZE before printing them; e.g.,
                               '--block-size=M' prints sizes in units of
                               1,048,576 bytes; see SIZE format below
...

As already done, apt-get update and apt-get upgrade are used to actually update the packages on our Raspberry Pi. The update updates the package lists, and the upgrade actually installs the newer versions of the packages.

Finally, we can restart and shutdown our Raspberry Pi with:

Shutdown: sudo shutdown -h now

Restart: sudo shutdown -r now or sudo reboot

This was by no means a full tutorial on the terminal, but hopefully it got you at least a little more familiar. In the next tutorial, we're going to begin introducing sensors, starting with the camera.

The next tutorial:





  • Introduction - Raspberry Pi tutorial
  • Remote Access with Raspberry Pi tutorial
  • Terminal Navigation - Raspberry Pi tutorial
  • Raspberry Pi Camera Module - Raspberry Pi tutorial
  • GPIO (General Purpose Input Output) Pins - Raspberry Pi tutorial
  • Input from a Sensor via GPIO - Raspberry Pi tutorial
  • Garage Stoplight GPIO Project - Raspberry Pi tutorial