You are on page 1of 58

Robotics Practicals

Linux

Wouter Caarls, Berk Calli, Dick de Ridder & Tom Hoeksma


Contents

1 Introduction 7
1.1 Brief history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.2 Free Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.3 Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.4 Flavors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

2 Installation 11
2.1 Installing using the Windows installer . . . . . . . . . . . . . . . 11
2.2 Installing from CD . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.3 Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

3 Graphical User Interface 15


3.1 Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.2 Package management . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.3 The file system . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.4 Getting help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

4 The Shell 21
4.1 Terminals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4.2 Basic commands . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4.3 The manual pages . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.4 Moving about the file system . . . . . . . . . . . . . . . . . . . . 25
4.5 Inspecting files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.6 Network connections . . . . . . . . . . . . . . . . . . . . . . . . . 28
4.7 Writing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

5 Files and directories 31


5.1 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
5.2 Wildcards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5.3 Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5.4 File permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
5.5 User groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
5.6 Hidden files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
5.7 Finding files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
CONTENTS

5.8 Searching files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37


5.9 Copying, moving and removing files . . . . . . . . . . . . . . . . 38
5.10 Making and removing directories . . . . . . . . . . . . . . . . . . 40
5.11 Editing files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

6 Advanced shell commands 43


6.1 Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
6.2 Pipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
6.3 The command line interface . . . . . . . . . . . . . . . . . . . . . 46
6.4 History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
6.5 Shell scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
6.6 Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
6.7 Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
6.8 Environment variables . . . . . . . . . . . . . . . . . . . . . . . . 50

7 Processes 51
7.1 Inspecting processes . . . . . . . . . . . . . . . . . . . . . . . . . 51
7.2 Foreground and background . . . . . . . . . . . . . . . . . . . . . 52
7.3 Killing processes . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
7.4 Nice! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
7.5 Suspending and restarting terminal sessions . . . . . . . . . . . . 55

8 Exercises 57

4
Foreword

This manual is part of the Robotics Practicals lab course. In the Robotics
Practicals, the aim is to familiarize yourself with the software tools that are
necessary to program the robots at the Delft Biorobotics Lab (DBL). The lab
courses are very much hands-on; you will be able to practice everything you
learn immediately by doing the exercises that are contained in the text. Be-
cause the aim is to understand the tooling instead of a particular application,
we specifically encourage you to explore and try to combine the things you’ve
learned in new and hopefully exciting ways!
Although we will concentrate on skills that are applicable to any Linux distri-
bution, some parts (mostly the installation and package management sections)
are specific to Ubuntu, which we will be using as an example. The manual
was written using version 16.04 LTS, and although we expect little to change
in the near future, it is still advisable to use this version (which should remain
available for some time).
At the end of this course, which should take roughly 10 hours of self-study,
you will be asked to write some shell scripts as a final exercise. The other
exercises are not part of the report, although we advise you to keep notes for
discussion with the supervisor during the examination.
Some notation: anything you should see on your screen is shown in Courier.
All commands in Linux are case sensitive, which means that there is a difference
between a small “a” and a capital “A”. This also holds for file names. Anything
you should type in is shown in Courier too, but it’s  underlined as well. Keys
you should press are typed in boxes, for example Enter .

Enjoy!

Wouter Caarls, Rio de Janeiro


August 2017
Chapter 1

Introduction

A Linux distribution such as Ubuntu is an operating system. Operating systems


are the system software that manage the way applications and users interact
with the computer hardware. In large part, this means they provide the abstrac-
tions necessary for applications to run smoothly. Basic things, such as making
a file available under a name (instead of starting at sector #187,374), but also
being able to run multiple applications at the same time on a single processor,
or even multiple users without hindering each other.
Probably the operating system you are most familiar with is some flavor of
Microsoft Windows, but many more exist. While current versions of Windows
are very capable as a Desktop OS, many believe that it is lacking in other areas,
particularly for use in embedded systems such as robots. As you can imagine,
an OS for word processing and playing games has rather different requirements
than one for running a robot, which often doesn’t even have a display!
For robotics, Linux is the most popular OS, but it didn’t arise from nothing.
Its origins date back more than forty years to the Unix operating system, and
even now many things that you will learn in this course can be applied to other
members of the (rather large) Unix family.

1.1 Brief history


In the 1960s, computers couldn’t really talk to each other. Even computers
by the same manufacturer were often incompatible. Furthermore, the operating
systems that ran on these machines often only did limited things, such as reading
and writing paper punch cards. All programming had to be done in machine
language, which is a tedious job.
In 1969, a few of the researchers at AT&T Bell Labs in the US (Dennis
Ritchie, Ken Thompson and some others) created an operating system for the
DEC PDP-7, a cheap computer (at the time) which had just 4 kilobytes of
CHAPTER 1. INTRODUCTION

memory1 . So, they put together a very simple basis for an operating system2 .
It consisted of little more than a shell for typing in commands, a mechanism for
starting and stopping programs, and a mechanism for the programs to talk to
the shell. The researchers called their toy operating system Unix as a pun on
MULTICS, a predecessor that was too big and complex to run on the PDP-7.
In 1973, Dennis Ritchie and another researcher, Brian Kernighan, developed
a new computer programming language called C (as a successor of B). Unix was
translated to C, and after that it really took off. Before C, if you wanted to run
Unix on a new computer, you had to completely re-program it in the machine
language of that computer. Now, all you had to do was to build an assembler
for the C-compiler, re-compile Unix and you had a working operating system.
This is called portability.
As Unix became more popular, researchers at universities around the world
started using it. Unix was distributed in source form, the original C code.
This meant that the researchers could repair bugs3 , change programs or even
add functions to the operating system themselves. They would send back these
changes, and over the years Unix quickly grew to contain a large set of com-
mands. One especially important branch of Unix was made at Berkeley Univer-
sity in California; they put in the important TCP/IP networking code, which is
the protocol computers use to talk to each other on the Internet. This branch
is still alive today in the FreeBSD, NetBSD and OpenBSD operating systems.
The networking code even ended up in Windows 2000.
While the code of Unix was available to its users, it still required an expen-
sive license. In 1983, Richard Stallman, a computer scientist at MIT, announced
the GNU project (for “GNU’s Not Unix”), which would provide a complete
reimplementation of Unix as free software. Although the project created a C
compiler, text editor and many of the required utilities and libraries, it lacked
a stable kernel, the part of the OS that manages how different programs work
together. This triggered the Finnish computer science student Linus Torvalds
in 1991 to create Linux, which is a free operating system kernel, originally for
the popular Intel 386 processor, and includes the GNU C compiler and utilities.
Many people contributed, and the combined GNU/Linux4 OS is now the most
popular free operating system available, coming in a wealth of different versions
(called distributions).

1 A kilobyte contains 1024 bytes (characters), a megabyte contains 1024 kilobytes, a giga-

byte contains 1024 megabytes, a terabyte... you get the picture.


2 This also explains why many of the commands have such silly names as ls instead of

listfiles, or fsck instead of filesystemcheck; this was done to save memory in the early
days.
3 Slang for small (or large) errors in a program, which programmers used to say was due to

bugs crawling around in the computer (see http://www.tuxedo.org/~esr/jargon/).


4 There is some debate about the proper name of the system, as the Linux kernel is only a

small part. Stallman likes to call it GNU/Linux, but that would seem to diminish the hard
work of the thousands of other developers that have contributed tools and applications.

8
1.2. FREE SOFTWARE

1.2 Free Software


Linux started as free alternative to Unix. The Free Software Foundation (FSF,
founded by Richard Stallman) defines Free Software as follows:
Free software is a matter of the users’ freedom to run, copy, dis-
tribute, study, change and improve the software. More precisely, it
means that the program’s users have the four essential freedoms:

ˆ The freedom to run the program, for any purpose (freedom 0).
ˆ The freedom to study how the program works, and change it
to make it do what you wish (freedom 1). Access to the source
code is a precondition for this.
ˆ The freedom to redistribute copies so you can help your neigh-
bor (freedom 2).
ˆ The freedom to distribute copies of your modified versions to
others (freedom 3). By doing this you can give the whole com-
munity a chance to benefit from your changes. Access to the
source code is a precondition for this.

This kind of freedom is often called free-as-in-speech in contrast to free-as-


in-beer, because it is the user who is free. Ed Felten, a professor at Princeton,
effectively coined it as the Freedom To Tinker. While many people simply like
the fact that they don’t have to pay for their software, the FSF sees the use of
free software as
[. . . ] to make a political and ethical choice asserting the right to
learn, and share what we learn with others.
Essentially, this makes free software a lot like academics, building on the work
of others to create new innovations. Bigger free software projects, such as the
Linux kernel, can have many thousands of contributors working together, often
in a “Bazaar” style of loosely organized chaos5 .

1.3 Architecture
As hinted at before, an operating system is not a single piece of software. The
kernel is the central part, determining when each program or other system
process can run, and making sure they can communicate while not corrupting
each other’s memory. Drivers talk to the hardware, such as a keyboard, display
or hard drive. They can be part of the kernel (which is then called monolithic),
or be separate system processes, in which case the kernel is a microkernel 6 .
5 Linus Torvalds once likened managing Linux development to “herding cats”. See also Eric

S. Raymond’s book “The Cathedral & The Bazaar”.


6 For more details on the pros and cons, see Andrew Tanenbaum’s essay at http://www.

cs.vu.nl/~ast/reliable-os/. Tanenbaum, a computer science professor at VU Amsterdam,


develops Minix, which inspired Linux.

9
CHAPTER 1. INTRODUCTION

System utilities are the programs necessary for the most basic functionalities,
such as a shell, commands to move about the file system, create new files,
compile programs, etc.
Most Linux distributions also provide a graphical user interface that allows
programs to draw in windows on the screen, called the X Window System,
or X for short. X only provides drawing and pointing facilities, but does not
specify how things such as window borders should be drawn. That is the task
of the window manager, of which there are many, some of which even especially
designed to be used without a mouse!7 . A desktop environment then integrates
a window manager with a suite of applications that share a common interface,
such as a calculator, text editor, email client, etc.
As you can see, Linux is actually a combination of many different programs
working together. This is a part of the Unix philosophy: that programs should
do one thing, and do it well. The idea is that each tool is a black box to the
user – he doesn’t have to understand how it works – so you’d better make the
behaviour of the black box as transparent as possible. By combining the tools
in different ways, almost anything is possible.

1.4 Flavors
Linux runs on many different hardware platforms, from dishwashers to DVD
players, mobile phones8 , webservers and PCs. Because each platform has its
own requirements – even PC users all have different wants and needs – many
distributions are available. They differ in the drivers, desktop environments and
applications that are provided, and in the way new applications can be installed
and configured. Some popular ones are:
ˆ Ubuntu, which stresses the desktop user experience.

ˆ Debian, which is popular for servers.

ˆ Red Hat Enterprise Linux, a commercial variant that offers service and
support.
ˆ Slackware, for advanced users and those that want to learn the inner
workings instead of the graphical veneer.

ˆ Buildroot, for embedded systems where memory space is limited.

At the DBL, most systems run Ubuntu because of its user-friendlyness, good
community support and advanced package management (which it shares with
Debian, on which it is based). Where this manual is distribution-specific, it will
therefore be based on Ubuntu.

7 Unsurprisingly, one of them is called “ratpoison”.


8 Android, Google’s smartphone OS, is basically just another Linux distribution.

10
Chapter 2

Installation

Before you can start learning how Linux works, you will need to install it on your
own computer. Fortunately, this is very easy, but there are a number of ways you
can do it. For this course, we recommend using the Windows installer1 , which
is a regular Windows program that creates a file containing the entire Ubuntu
drive. It also modifies the Windows startup program (called bootloader ) such
that when you turn your computer on, you can select the Ubuntu operating
system as well as Windows.
Of course, this requires that the computer already has Windows installed,
which is not the case for robots! We will therefore also describe how to install
Ubuntu from a CD. However, if you would like to do this on your own computer,
be sure to back up all your data first! The CD installer will need to make some
changes to your hard disk that may cause problems in rare cases.

2.1 Installing using the Windows installer


To install Ubuntu using the Windows installer you will first need to download
it.

Exercise 2.1 Go to the Ubuntu website at http://www.ubuntu.com and find


the Windows installer (go to “Download Ubuntu” and then see the section
under “If you’re running Windows”). Download it – the file will be called
wubi.exe.

The installer will need to know some information, such as the username and
password you want to use. For the installation size, we will need at least 8 GB;
the rest can be left as is.

1 If you plan to use Linux more often, the CD installation is a better choice
CHAPTER 2. INSTALLATION

2.1.1 Passwords
Use a strong password, as someone logging in to your Ubuntu installation will be
able to access all of your Windows files as well! A strong password is best derived
from a sentence by taking the first letter of each word. For example, if you liked
the Hitchhiker’s Guide to the Galaxy, you might turn the sentence “Time is an
illusion. Lunchtime doubly so.” into “TiaILDs”. Better yet, exchange some
letters for numbers and other characters, like “T=aI.L2s”. Never use words
that can be found in a dictionary, even if you replace letters with numbers or
add numbers to the beginning or the end. There are specialized programs for
guessing these kinds of passwords.
In Linux, like most systems, passwords are stored using a one-way hash.
This means that the computer calculates a number (called hash value) based on
the password, but that the password cannot2 be reconstructed from this hash
value. When you enter your password, the hash is compared to the stored value,
and you are allowed access if they match. Therefore, no one on a Linux system,
not even the administrator, knows your password.
Exercise 2.2 Follow the instructions on the website by running wubi.exe and
providing it with the information it requests. Then let it reboot your
computer and choose Ubuntu from the list of operating systems.
optional

2.2 Installing from CD


If you decided to install from CD (or a USB disk, for that matter), you will
need to download and write an image to that disk. The Ubuntu download page
has all the relevant information. However, at this point it is helpful to know
something about the organisation of your hard disk into partitions.

2.2.1 Partitions
A partition is a part of your hard disk that is seen by the operating system as
a single drive, such as C:. The files in a partition are organized in a certain
way, called the file system. Linux and Windows use different file systems, and
therefore have to be installed in different partitions. Most likely, Windows is
now occupying your entire hard disk, so while installing Ubuntu you will need
to resize its partition and create a new Linux partition in the empty space. If
the disk is empty, you can simply select the option for Ubuntu to erase and use
the entire disk.
Exercise 2.3 Go to the Ubuntu website at http://www.ubuntu.com, select
“Download Ubuntu”, and follow the instructions. You will need a blank
CD or USB disk.
2 It is not impossible, but very, very time consuming.

12
2.3. NETWORKING

end optional

2.3 Networking
After rebooting your computer, you will see the Ubuntu login screen. Select
your username, enter your password, and you’ll be presented with the desktop
environment. At this point, it would be helpful to get your network up and
running. Some of the exercises will require you to find information on the
Internet, so make sure that it is working correctly! If you get stuck, please
contact your supervisor.
As there are so many different computers and laptops available, we cannot
explain how to set up all of them. However, in most cases a simple procedure
will make it work. The easiest is a wired network connection to your Internet
router. This should work out of the box, simply by having it connected when
you start your computer. Wireless networks are a bit more tricky, and require
some setup.
optional

Exercise 2.4 Set up your wireless network connection. Click the icon with the
two arrows on the top right-hand side of the screen, and select eduroam
from the available wireless networks. Select PAP as the Inner authentication
protocol, use your netid@tudelft.nl as username and enter your pass-
word. Then click Connect. You can ignore the security warning that
follows.
end optional

13
Chapter 3

Graphical User Interface

As mentioned in section 1.3, the graphical user interface is not a required part
of a Linux distribution. The X Windows system, window manager and desktop
environment are all just building on top of a core kernel and a shell for executing
commands. We will come back to the shell in chapter 4, but for now we will
start with the window system, which is probably the most familiar to you.
By default, Ubuntu comes with the Unity desktop environment, and uses
the Compiz window manager. It includes a lot of applications that you would
expect, including a complete office suite, LibreOffice. They are all accessible via
the Launcher on the left of the screen. In particular, the top icon, called the
Dash, allows you to quickly start applications by typing the first few letters. A
lot of effort has been put into making these programs work together, and look
similar. As conforming to a single standard is not the normal behavior of the
free-spirited Free Software community we described in section 1.2, this is quite
an achievement!

Exercise 3.1 Take a few minutes to explore the available programs by right-
clicking the Dash and selecting Applications. Try the disk usage an-
alyzer. Which folder uses the most space? Start a text editor, write a
sentence and save your file under the name myfile.txt.

As the history of Unix and Linux is primarily grounded in academic net-


works, many Internet-enabled applications are available, such as the Firefox
browser, Thunderbird email client, file transfer clients, and social network inte-
gration.
Of course you’ll want to keep your email (or instant messenger, etc.) open
while working on your exercises. Because displays are expensive, almost all
window managers support multiple virtual desktops, between which you can
switch easily. Let’s configure that now.

Exercise 3.2 Go to the system settings menu (gear icon on the top right, then
System Settings...). Then choose Appearance and select the Behavior
CHAPTER 3. GRAPHICAL USER INTERFACE

tab. Enable the Enable workspaces option. An extra icon appears in the
Launcher that allows you to switch between four virtual screens.

Exercise 3.3 Click on the second workspace and start a text editor. Then go
back to the first workspace using the Ctrl +Alt +← shortcut and start
the Calculator application. Right-click on the title bar and set Always
on Visible Workspace. Now return to the second workspace. What
happens?

3.1 Configuration
Unity provides some more options to change the system to your liking through
the System Settings/Personal icons. Remember: Linux developers are a
fussy lot, so almost anything you can conceive is possible, though not all through
Unity – we will come back to this later.

Exercise 3.4 Customize the appearance of your desktop by choosing a different


background picture. While you’re at it, remove the Amazon icon from the
Lancher by right-clicking and selecting Unlock from Launcher.

3.1.1 Root privileges


Up until now, you’ve been working under your own user name. Many people can
be working on the system in such a way (even at the same time, if they connect
over the network!), without obstructing each other. But sometimes you will need
to make changes to the system itself, and for this you need administrator rights.
In Unix, the administrator account is called root. In the past, this account
had a separate password and you would log in with it like any other. Because
this was considered a security risk, now you only acquire root privileges for a
brief period while making the change. You have to type in your own password
to get them.

3.2 Package management


When you were looking through the menus before, you saw that by default,
Ubuntu comes with a single application for all the common tasks you can do.
How very much unlike the free software spirit! Of course, a great many appli-
cations are available for each task, but the Ubuntu people have selected only a
small subset suitable for regular users. All other software can be installed using
the package manager, which in Ubuntu is called the Ubuntu Software Center.

Exercise 3.5 Use the Software Center (The shopping bag icon in the Launcher)
to find out how many web browsers you can install.

A package contains all the files that belong to a specific piece of software.
But remember that people like to build on each other’s work. To avoid having

16
3.3. THE FILE SYSTEM

to install many copies of the same basic library, it comes in its own package, on
which the other packages depend. Thus, when you install an application, the
package manager not only downloads and installs that application’s package, but
also all its dependencies (and their dependencies, etc.). The Software Center
hides all this complexity.
Note that you need root privileges to install new software, so if you’re work-
ing on a university computer you will have to ask an administrator nicely.1 .

3.3 The file system


If you paid attention during exercise 3.1, you might have noticed that all of files
are organized in folders (in Unix we call them directories) such as /usr/sbin
and /usr/share. Unlike Windows, where program files are stored per appli-
cation, in Unix they are stored according to their use: all binaries go in one
directory, all documentation files in another, etc. A general list of what goes
where can be found in table 3.1.
The most important directory for you is your home directory, in /home/username
(note that in Unix, subdirectories are separated by forward slashes!). This and
the temporary directory /tmp are the only places you are allowed to place your
files2 . This is especially important in multiuser systems, where you want to
avoid others screwing up your data. It is quite common for each user to install
his or her private software in the same way as the /usr directory hierarchy, so
a /home/username/bin, /home/username/share, etc.
Remember that Unix originated in an academic networked environment.
To avoid having to install the same software on all computers, everything was
often placed on a single server. No matter which computer you logged in to,
you always had access to the same files and programs. This is what required the
strict separation into essential (/bin, locally installed) and shareable (/usr/bin,
can be on the network) program files, and local program (/var) and user (/home)
data. Nowadays the separation is less strict, and is more about managed (/usr)
and unmanaged (/usr/local) packages. If program files are placed on the
network, it is usually restricted to the /usr/local hierarchy.
The easiest way to navigate the file system is using the File Browser,
started with the file cabinet icon in the Launcher menu. It works much like
you are used to with Windows. Note that if you installed Ubuntu using WUBI,
the Windows drive you installed it on is available under /host.

Exercise 3.6 Take a while to browse through the file system: go to Computer
in the left-hand list to see the full directory hierarchy. Try to navigate to
the text file you saved in exercise 3.1. Also try to read the administrator’s
files (use table 3.1 to see where they are). What happens?
1 For enlightenment on what can happen if you don’t ask nicely, read the BOFH (Bastard

Operator From Hell) series at http://bofh.ntk.net/.


2 More about file system permissions in section 5.4

17
CHAPTER 3. GRAPHICAL USER INTERFACE

.
|-- bin Essential user command binaries.
|-- boot Static files of the boot loader.
|-- dev Device files, for talking to hardware.
|-- etc Host-specific system configuration.
| \-- rc.d Startup configuration
|-- home Where you hang your hat. User directories.
|-- lib Essential shared libraries and kernel modules.
|-- media Removeable media (CD-ROM, USB, etc.).
|-- mnt Temporarily mounted (network) filesystem.
|-- opt Add-on application software packages.
|-- proc Kernel and process information (virtual).
|-- root Home directory for administrator.
|-- sbin System binaries.
|-- srv Data for services (WWW, FTP, etc.).
|-- sys Kernel and device information (virtual).
|-- tmp Temporary files.
|-- usr Shareable, read-only data.
| |-- bin Most user commands.
| |-- include Header files included by C programs.
| |-- lib Libraries.
| |-- local Local hierarchy, not managed by OS.
| |-- sbin Non-vital system binaries.
| |-- share Architecture-independent data.
| | |-- doc Miscellaneous documentation.
| | `-- man Online manuals.
| `-- src Source code.
`-- var Variable data files.
|-- lock Lock files.
|-- log Log files and directories.
|-- mail User mailbox files.
|-- run Data relevant to running processes.
`-- spool Application spool data (printer, mail, etc.).

Table 3.1: Some important directories in the Linux filesystem.

18
3.4. GETTING HELP

3.3.1 Mount points


You might have noticed that there are no drives in Unix. Instead, every file
system (partition, USB disk, network share) is mounted at a particular point in
the directory hierarchy. This makes it easy for e.g. /usr to contain data from a
local hard drive, while /home is located on a file server. Ubuntu places the most
commonly mounted file systems (CD-ROMs, USB disks, and other partitions
on the local hard drive) in a special /media directory.

Exercise 3.7 Connect a USB disk to the computer, and find out where it’s
mounted.

3.3.2 Virtual file systems


Files are very important in Unix, so much so that things that aren’t really files
are presented like them anyway. One example are the device files in the /dev
directory: things like a mouse or sound card have their own files that programs
can read from or write to. They’re not human-readable, however. /proc, on
the other hand, is comprehensible: it is a virtual file system, created by the
kernel to give status information. You can navigate it like any other directory
hierarchy, but every numbered directory corresponds to a running program.

Exercise 3.8 Open the cpuinfo file on the /proc filesystem. What does it say
about your processor?

3.4 Getting help


There will be points at which you will be stuck trying to do something under
Linux. You know it’s possible (which is a safe bet, since so much software is
available), but not how, or you get an error message that you don’t understand.
For the shell there is good documentation, though mostly written by program-
mers for programmers; this system is explained in section 4.3. GUI applications,
however, are not so uniform. There are a number of places you can go for help:

ˆ The application’s own help files, often accessible through a Help menu or

the F1 key.

ˆ The application’s home page, most easily found using the Ubuntu Software
Center by locating the package and clicking More Info.

ˆ The Ubuntu Destop Guide, under Ubuntu Help... in from the gear-icon
in the top right.

ˆ The Ubuntu Community support (such as web forums) at http://www.


ubuntu.com/support/community.

ˆ The Ubuntu bug tracking system at https://bugs.launchpad.net/ubuntu/.

19
CHAPTER 3. GRAPHICAL USER INTERFACE

Most often, however, it is easiest to do a Google search with some proper


keywords. While you’ve all used Google before, it takes some practice to find
the proper keywords. Adding Linux or Ubuntu (if you think the problem might
be Ubuntu-specific) helps, as does searching for exact error phrases (enclose it
in double quotes ("").

Exercise 3.9 Find out what you should do if you lose your password.

20
Chapter 4

The Shell

Up until now we’ve been working in a graphical environment. This is good


enough for regular desktop use, but if you want to do more interesting things
(like programming robots!) you’ll have to be familiar with the shell, the text
interface that everything else is built on. From now on, we will assume that you
can find out how to do things with the GUI on your own, and concentrate on
using the shell.

4.1 Terminals
The basic element of any Unix system is the terminal. In the 1960s people had
to talk to computers using paper punch cards; you would write your program,
then create a stack of cards with small gaps in it in certain places and feed them
to the computer. The computer would then do its job (if you didn’t make any
errors) and spit out a number of punch cards containing the answer. You would
then have to translate the gaps in the cards back to English.
Of course, this makes it pretty hard to use your computer interactively.
Therefore, terminals were invented. In the beginning, these were just keyboards.
You could type one line of commands and enter it into the computer. Again,
the computer would do it’s job, and spit out the answers on a line printer.
Later, the line printer was replaced by a screen. You type your commands, the
computer calculates and prints the answer on the screen.
Nowadays, Unix still uses the idea of a terminal at its basis. However, we
don’t need large screen-and-keyboards any more. Mostly, we use a terminal
emulator (just called Terminal in the Dash) to type our commands.  However,

on
 a Linux system
 you
 can  also open
 6 virtual
 terminals
 by pressing Ctrl -Alt -
F1  through Ctrl -Alt -F6  (use Ctrl -Alt -F7  to get back to X Windows).
This is useful if you are unable to log in using X windows, for example when
your disk is full.
CHAPTER 4. THE SHELL

4.2 Basic commands


Now open a terminal emulator from the Dash. When you open a terminal, it
runs a shell program. The shell interprets your commands and instructs the
kernel to start other programs. The default shell in Ubuntu is called bash, the
Bourne again shell 1 . It provides the basic commands for starting and managing
other programs. As said before, Unix contains many small, simple commands
that do one thing and do it well. However, not all of them are equally easy to
use. Sometimes the programmers have put in so many options that it’s hard to
know which program switch (option) gives what effect.

4.2.1 date
A very useful command is date, which without arguments tells you what date
it is:

wcaarls@vbox:~$ date
Fri Jul 16 14:04:02 CEST 2010

The output is fairly self-explanatory, except perhaps the CEST part: this indi-
cates it’s Central European Standard Time.

Exercise 4.1 One particulary interesting option of date is -d. The option tells
the program to expect some more input, which specifies which date to use.
You can use this option to tell date not to report what time it is today,
but also in the future and the past. You can even instruct it in more or
less plain English:

wcaarls@vbox:~$ date -d "tomorrow"


Sat Jul 17 14:04:13 CEST 2010
wcaarls@vbox:~$ date -d "yesterday-1 week"
Thu Jul 8 14:04:18 CEST 2010

Use the date command to find out what day it will be in one year, in 13
months and 3 days, and 17 days ago.

This example immediately shows you that what seems like an ordinary little
command can actually be quite powerful. Each program usually has a number
of arguments (characters or numbers following the program name, separated by
spaces) which you can use to instruct the program to do something else than it
does normally (by default). Arguments can be options like -d above, followed
by the actual value for the option, or switches, single arguments which tell the
program to do something.

Exercise 4.2 Find out what switches date accepts by using the --help switch.
1 A pun on an earlier shell called the Bourne shell. Computer programmers aren’t very

good at puns.

22
4.3. THE MANUAL PAGES

4.2.2 cal
With date, you can can specify relative dates. However, when you want to
know on what date the last Sunday of this month will fall, a calendar is much
more useful. In Unix, the cal command can be used to find out such things:

wcaarls@vbox:~$ cal
July 2010
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

Exercise 4.3 The first argument cal takes is the year. Try cal 2000 and
cal 2525. If you specify two arguments, the program understands the
first argument to be the month and the second one to be the year.
Find out what day December 31, 7351 is.

In Unix help pages, optional arguments are often indicated by square brack-
ets, like [this]. For the cal command, the syntax then is:

cal [[month] year]

which tells you that you can specify either nothing, or just the year, or the
month followed by the year.

Exercise 4.4 What would happen if you would give the year first, then the
month? Try this: cal 2010 12.

4.3 The manual pages


You’ve already encountered one way of finding out what arguments a command
takes: by specifying --help. However, not all programs understand this com-
mand. Instead, in Unix there’s an online collection of manuals. Each command
has its own manual page, which you can call up with the command man.
When using the man command to find out what the cal command does and
what arguments it takes, you will see something like this:

wcaarls@vbox:~$ man cal


CAL(1) BSD General Commands Manual CAL(1)

NAME
cal, ncal displays a calendar and the date of Easter

SYNOPSIS

23
CHAPTER 4. THE SHELL

cal [-hjy] [[month] year]


cal [-hj] -m month [year]
ncal [-hjJpwy3] [-s country_code] [[month] year]
ncal [-hJeo] [year]

DESCRIPTION
The cal utility displays a simple calendar in traditional
format and ncal offers an alternative layout, more options

and so on. To scroll back and forth in the manual page, you can use Space or
     
Enter , Page Up /Page Down and ↑ /↓ . To leave the man page, press q . You
can also
 easily search through it by first hitting /  followed by the text  and
then Enter . Repeating the same search can be done by simply pressing n .

Exercise 4.5 According to the manual page, when did the Gregorian Refor-
mation occur in Great Britain? Find the option that prints the switching
dates for other countries to see when this was done in the Portugal. Note
that this option is only accepted by the ncal variant of the command!

Manual pages often tell you the name of the command, how to use it (syn-
opsis), a detailed description of what it is, when the command was made and by
whom, where to find more information or related commands, and the copyright.
Sometimes, the man page is so brutally honest that it even gives information
on the bugs still left in the command.
The SYNOPSIS section details different ways of calling the command, each
with different options. Parts of the command line that must be typed as written
are in bold, while placeholder arguments (like month in the cal example) are
underlined. These are often detailed in the DESCRIPTION section. As said
before, required arguments are given as is, while
 optional ones are enclosed in
[brackets]. When keystrokes are given, Ctrl -C is shortened to ˆC2 .

Exercise 4.6 Find the BUGS section of the bash manual page to see what the
authors think the biggest problem with it is.

Note that in the cal man page, the top line says CAL(1). This means that
cal is found in Section 1 of the online manual. In total, there are 8 main manual
sections:
1. User commands
2. System calls
3. Subroutines
4. Devices
5. File formats
2
 
Alt and the more exotic Windows keys are represented as META and SUPER, respec-
tively.

24
4.4. MOVING ABOUT THE FILE SYSTEM

6. Games

7. Miscellaneous

8. System adminstration

Fortunately, you don’t have to know which section a manual page is in; it will
be found automatically by man. You will only have to specify the section (using
man n [term], where n is the section number) if there’s more than one entry
for the term you’re looking for.
Manual pages are among the most important things you need to learn about
Unix. From now on, as little explicit help on commands will be given as possible;
you know where to find more information if you need to...

Exercise 4.7 Find out more about the man command.

Two other useful manual commands are whatis and apropos. With whatis,
you get a one-line description of a command; the argument must match the com-
mand exactly. The apropos command also shows descriptions of commands
which match your argument partially, which makes it easy to search for a com-
mand of which you don’t exactly know the name.

Exercise 4.8 Find out in which sections manual pages reside on passwd, using
whatis. Look at both sections 1 and 5 of the passwd man pages; what
is the difference? Now use apropos on the term passwd. What do you
notice?

4.4 Moving about the file system


You’ve explored the Linux file system using the graphical user interface in sec-
tion 3.3. Of course, you can also do this using the shell. We’ll first have to
become a bit more specific about how we represent files and directories.
The naming of directories and files is as follows:

1. a single / at the beginning denotes the root directory;

2. a name followed by a / indicates a directory;

3. a name not followed by a / indicates a file.

For example, the command date is stored in /usr/bin/date. There is a


subdirectory below the root directory called usr. This directory itself has a
subdirectory called bin, and in this directory there is a file called date. We call
/usr/bin/date the path to the command date; it’s the path you have to take
through the tree to get to the sequence of bytes called date.
The path to date is an example of an absolute path. No matter where you
currently are in the tree, you will first have to return to the root (/) and from
there go to /usr/bin to find date. It’s also possible to have a relative path.

25
CHAPTER 4. THE SHELL

Say you are currently in the directory /usr, then it’s not necessary to go all the
way back to the root. Instead, you can simply specify that date is in the bin
subdirectory of the directory you’re in. The relative pathname then would be
bin/date. Note that there’s no / at the beginning of a relative path.
The relative path above only refered to a file “downward” in the tree, away
from the root. However, it’s also possible to refer to higher directories. The
first directory above the one you’re currently in is called the parent directory
and can be referenced by ... For example, say we are in /usr, then the path
to the root directory could be written as ... If we’re in /usr/bin, the path to
the root directory is ../...

Exercise 4.9 Starting in /usr/bin, where do you think you will end up if you
go to ../../usr? And if you go to ../bin/../../usr/bin/../bin/?
Where do you think you end up if you go to ../../../?

Note that we have introduced a directory we “are in” now. This is called
the current working directory. By using commands to change this, you will be
able to walk around in the filesystem tree. Unix has a number of commands to
do this. The first is used to find out where you are:

wcaarls@vbox:~$ pwd
/home/c unx001

The command is short for print working directory. If you log in, you start
in your home directory. This is the directory in the filesystem which is yours to
do with what you want: you can create subdirectories and files as you please.
This is certainly not the case in other parts of the filesystem! A “shorthand”
notation for the home directory is a tilde, “~”. (this also explains why there’s
a tilde in your prompt).
To change directories, you can use the cd command (short for change direc-
tory). It simply takes as argument the place you want to go, e.g.:

wcaarls@vbox:~$ cd /usr/bin

or

wcaarls@vbox:~$ cd ..

Exercise 4.10 Use the cd command to verify the answers you gave to exer-
cise 4.9.

Two final useful properties of cd are:

ˆ cd without any options will take you back to your home directory;

ˆ cd - will take you back to the directory you were last.

Exercise 4.11 Type:

26
4.5. INSPECTING FILES

cd /bin
cd /usr/bin
cd -

Where should you now be? Verify this using pwd. Go back to your home
directory by typing:

cd

To have a look at your directory, use the command ls (short for “listfiles”):
wcaarls@vbox:~$ ls
This will print all files and directories in the directory you’re currently in. You
can’t see any files and directories above or below.
ls is probably one of the commands with the largest number of options in
all of Unix. The most important one lets you specify another directory to look
in:
wcaarls@vbox:~$ ls /bin
Now ls shows you which files are present in the directory /bin, rather than the
directory you’re currently in.

Exercise 4.12 Inspect the ls man page and try some of the options, such as
-lS and -lt.

4.5 Inspecting files


If you type in:
wcaarls@vbox:~$ ls
you will see there are a number of files in your home directory. You might be
starting to wonder what’s in them. To find out, you can use the cat command
(for catalogue), e.g.:
wcaarls@vbox:~$ cat myfile.txt
As you can see, cat just spits out the contents of a file. If the file is longer than
can fit on your screen, you will not be able to see the first part.
You could have a look at just the first or last few lines of a file. Try:
wcaarls@vbox:~$ head /usr/share/common-licenses/GPL
and
wcaarls@vbox:~$ tail /usr/share/common-licenses/GPL
A better solution is to use a program called less:

27
CHAPTER 4. THE SHELL

wcaarls@vbox:~$ less /usr/share/common-licenses/GPL

If this seems familiar from when you looked at man pages, it is. In fact, man uses
less to display its output! The same navigation options described in section 4.3 
therefore apply here as well. A few other ones are jumping to the top using g 
 
and to the bottom using G . Pressing h will give you an overview of all options
of less.

Exercise 4.13 Try this:

wcaarls@vbox:~$ less /usr/share/common-licenses/GPL

and search for the kind of WARRANTY you get on Ubuntu.

4.6 Network connections


Up until now, you have been using the shell to interact with your own com-
puter. However, as we discussed before, Unix originated in a networked en-
vironment. Traditionally, Unix networks consisted of a small number of very
powerful servers and a number of not-so-powerful workstations. In fact, in the
very first Unix networks there usually was just one server3 . Users connected to
that server using dumb terminals, which were little more than a keyboard and
a text-based screen. However, as computer power increased and got cheaper, it
became feasible to buy computers which could do much more and place one on
each user’s desk. In fact, in some networks the workstations are now more pow-
erful than the servers. Nowadays, Unix servers are integrated in your wireless
router and media player, and more importantly: your robot!
Provided you have the necessary permissions, you can log on to any other
Unix system over the network. Nowadays, the network connections are made
over the internet, using the TCP/IP protocol. Every computer has a unique IP
address that identifies it. Originally, you would start a remote terminal session
using the telnet command, but as telnet copies everything you type and see
transparantly over the network (even your password), it is not very secure. In
contrast, ssh encrypts both the password and data, and also verifies that you’re
talking to the right computer. Of course, if you’ve never connected to the
computer before, it’s hard to know whether it’s the right one. The correct way
is to ask the administrator for the host’s fingerprint and check that it matches
the one you see4 .

Exercise 4.14 Log in to a remote server, such as one of the mobile robots.
Use ssh to connect to it, at IP address 192.168.0.201. The robots have
username pi with password robots1234.
3 Something like an HP 1000, with a whopping 32 kilobytes of memory!
4 No one uses the correct way.

28
4.7. WRITING

wcaarls@vbox:~$ ssh pi@192.168.0.201


The authenticity of host '192.168.0.201 (192.168.0.201)' can't be established.
ECDSA key fingerprint is SHA256:e/EEUIu6njn64S/LKG5PBa0o6uzEZaWPYbCPRGGNdsA.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.201' (ECDSA) to the list of known hosts.
pi@192.168.0.201's password:

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent


permitted by applicable law.
Last login: Wed Jun 21 06:17:09 2017
xhost: unable to open display ""
pi@dex:~ $

As you can see, you get a shell prompt just as if you were working on your
own computer. It helpfully identifies itself as dex to avoid confusion.
You can use all the commands that you have learned on remote computers
as well. Additionally, you can see what other people who are logged on to the
same computer are doing. To find out who is currently around, you can use the
command
wcaarls@vbox:~$ who
It shows you who is logged in, on which terminal (pts, from pseudo-terminal
slave), and since when. For example:
11:15:51 up 4:58, 3 users, load average: 0.02, 0.03, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
pi tty1 06:17 4:58m 0.31s 0.29s -bash
pi :0 :0 06:17 ?xdm? 2:36 0.18s /usr/bin/lxsession -s LXDE-pi -e
pi pts/0 192.168.0.101 11:14 0.00s 0.30s 0.03s w
You can learn quite a lot from this. For example, pi logged on as a local user
a few hours before we did, and our IP address is 192.168.0.101.
If you ever get confused as to who you are yourself, you can try the command
wcaarls@vbox:~$ who am i
Exercise 4.15 Try the who command. Also try the w command. What are the
differences? What do you think the IDLE field in the output of w means?

4.7 Writing
There are a number of Unix commands which allow you to interact with other
users. However, as you’re probably the only user logged in at the moment, we

29
CHAPTER 4. THE SHELL

will have to “fake” another user. You can do this by logging in again using a
different terminal emulator.
Exercise 4.16 Open a second virtual terminal and log in to the same server
again. Use who to find out how many users there are at the moment.
Remember the name of the second terminal (for example, pts/1).
The simplest command is write. This sends messages to other users.
Exercise 4.17 Return to your first virtual terminal and type:

wcaarls@vbox:~$ write pi pts/1

replacing, of course, pi by your login name and pts/1 by the name of your
second terminal.
 The system will now ask for input; just type
 a message

and press Enter . If you want to stop entering lines, press Ctrl -D .
Now look at the second terminal and see whether your message has arrived.
Note how it prints your name and terminal, so this feature sadly can’t be
used to send fake messages...
Note that writing is a bit intrusive, as the message will be printed on the
other user’s screen without warning and will overwrite anything that’s there.
Fortunately, there’s no risk that the text you enter will actually be used as
input for a program, but still it can be annoying. If you want to avoid being
written to, you can enter:
wcaarls@vbox:~$ mesg n
so that users trying to write to you will see the message
write: rsplx001 has messages disabled on pts/1
Exercise 4.18 Try disabling messages on your second virtual terminal, and
writing to it from the first virtual terminal again.
You can enable messaging again by entering mesg y.
Writing can be useful when you want to send quick one-liners such as
There's cake at the 10.30 coffee break
or
Get out of there, the building's on fire!
but less useful for having conversations. To really talk to another user, use the
talk command, with the same arguments as write. The user you want to talk
to will receive a message saying that there’s someone who wants to talk to him.
He will then have to respond by running talk himself. Next, you will see a split
screen in which you can type in the top half, and the other user can type in the
bottom half. As you can imagine, talk usage has somewhat declined since the
advent of Whatsapp and other instant messenging apps, to the point that it is
no longer installed by default.

30
Chapter 5

Files and directories

Now that you know how to move around the file system, it’s time to learn a bit
more about files and directories: how to get more information on them, how to
create them and how to remove them.

5.1 Files
A very useful option to the ls command you saw before is -l, which tells ls to
give more information on each file or directory:

wcaarls@vbox:~$ ls -l /bin
total 6632
-rwxr-xr-x 1 root root 818232 2010-04-19 03:51 bash
-rwxr-xr-x 3 root root 30200 2010-02-08 11:54 bunzip2
-rwxr-xr-x 1 root root 1269432 2010-04-22 22:04 busybox
-rwxr-xr-x 3 root root 30200 2010-02-08 11:54 bzcat
lrwxrwxrwx 1 root root 6 2010-07-07 11:48 bzcmp -> bzdiff
-rwxr-xr-x 1 root root 2140 2010-02-08 11:54 bzdiff
lrwxrwxrwx 1 root root 6 2010-07-07 11:48 bzegrep -> bzgrep
-rwxr-xr-x 1 root root 4874 2010-02-08 11:54 bzexe
(etc.)

First of all, it shows you how many blocks the entire directory takes up on
the filesystem. A block does not have the same size on all machines, but on
Linux PCs it’s usually one kilobyte (1024 bytes). The directory /bin seems to
take up 6632 blocks, or roughly 6.6 megabytes.
Next, for each file, ls -l now shows the file type, the first character of the
string on the left. The file type indicates whether the file is a plain file (indicated
by -), a link (l, see below) or a directory (d). There are some other file types,
like b (block device) and c (character device), but you will not encounter these
regularly, so don’t worry about them.
CHAPTER 5. FILES AND DIRECTORIES

Some other important information given by ls -l is the user and group of


the file (here: user root, group root); the date and time of creation; and, of
course, the filename.

5.2 Wildcards
Above, you used ls to list files. However, often you want to limit the number
of files you see, for example when you know that you’re looking for a filename
containing a certain word. If this is the case, you can call ls with wildcards.
The most important wilcdards are:

ˆ * will match any piece of filename. For example:

ls * : list all files in the current directory;


ls my* : list all file names starting with my;
ls *.txt : list all file names ending with .txt (including the .);
ls /etc/*.conf : list all files in the /bin directory ending with .conf.
ls */* : list all files in all immediate subdirectories.

ˆ ? will match a single character in a filename. For example:

ls myfile?.txt : lists myfile0.txt, myfile1.txt, myfileq.txt, etc.

ˆ [abZ] will match the letter a, or the letter b, or the letter Z. For example:

ls myfile[abc].txt lists myfilea.txt, myfileb.txt and myfilec.txt.

ˆ [a-z] matches all letters in the range a - z, e.g.:

myfile[A-K].txt lists myfileA.txt ... myfileK.txt.

You can always use wildcards in the Unix shell when you have to specify a
filename.

Exercise 5.1 List all files in /usr/bin starting with a z. Also list all files in
/usr/bin that contain the string text in their name.

5.3 Links
A link (indicated by an l in the output of ls -l) is not a file, but a pointer to
a file. It can also be a pointer to a directory. Links can be useful for when you
want to have a file with changing content but a fixed name. For example, you
could create files news 01 01 2010, news 02 01 2010 and so on, and then have a
link news today which always points to the latest file. These links can be either
hard or soft (also called symbolic). A hard link means that there is actually one
physical file with two names; if you throw away the original, the other link still

32
5.4. FILE PERMISSIONS

points to the same content. A soft link is different; you can remove the link
without affecting the file itself, but if you remove the original, the link is left
dangling. Soft links are probably the only ones you will be using.
In the directory listing of /usr/bin above, you can see that bzcmp is a link
to bzdiff. That means that if you run the program bzcmp, really the program
bzdiff is run. This is shown in the filename part (bzcmp -> bzdiff).
You can create links using the ln command (for link).

Exercise 5.2 Try:

wcaarls@vbox:~$ cd
wcaarls@vbox:~$ ln -s /etc/passwd passwords

This will create a symbolic link in your home directory which points to
the password file in the directory /etc. You can now use this link as if it
were the password file itself:

wcaarls@vbox:~$ less passwords

After you’re done looking through the password file, remove the link:

wcaarls@vbox:~$ rm passwords

Fortunately, this will only remove the link, not the real password file...
You if you don’t specify a destination, ln will create a link in the current
directory with the same name as the original file. Try:

wcaarls@vbox:~$ ln -s /usr/share/common-licenses/GPL

5.4 File permissions


Next to the file type, ls -l shows the file permissions, the row of r’s, w’s and
x’s. This list really consists of three groups of three characters each. The first
three characters contains the permissions for the user, the owner of the file; the
second three are for the group, the group of the file; and the last three are for
others, everybody else. The idea of a user group will be explained below.
The three permission flags are:
ˆ r: if this flag is present, the file can be read from;
ˆ w: if this flag is present, the file can be written to;
ˆ x: if this flag is present for a file, the file can be executed; if it’s present
for a directory, the directory contents can be read.
This might all sound a bit cryptic, so here are a couple of examples. These
are the permissions you will encounter most:

33
CHAPTER 5. FILES AND DIRECTORIES

rw-r--r-- myfile.txt means that the owner of myfile.txt can read and
write the file, but people in the same group and others can just read the
file. This is usually the default for text files: you allow people to see the
contents of the file but not to change them.
rwxr-xr-x myprogram means that everybody can execute the program myprogram,
but only the owner can overwrite it. Again, this is often the default for
programs.
rwxr-xr-x mydirectory means that everybody can see which files reside in
mydirectory, but only the owner of the directory can write new files to
that directory.

Exercise 5.3 Try to list the files in the administrator’s home directory (see
table 3.1). Find out with ls -l why this doesn’t work. Now try again,
prefixing your command with sudo . Explain why this second method
works by studying the sudo manual page1 .

You can change file permissions with the chmod command. The command,
in its simplest form, looks like this:
chmod ugo+rwx somefile
This call of chmod gives permission to read (r), write (w) and execute (x)
somefile to the user (u) of the file, its group (g) and others (o). Of course, you
can specifiy subsets, e.g.:
chmod u+rx somefile
The + means “add permission”. It’s also possible to remove permissions, using
-. For example, to make sure you are the only one able to read somefile:
chmod go-r somefile

Exercise 5.4 Change the permissions on your file myfile.txt so that you
yourself (the user) cannot read it anymore. Verify this using cat or less.
End by changing the permissions back so that you can read it again.

5.5 User groups


As discussed above, you can assign permissions for the user of a file, the group
of a file and others. The output of ls -l shows the user and group to which
the files belong. In the listing of /usr/bin above, all files are owned by user
root, who is in group root.
You might wonder what a group exactly is. Groups were made to make
system administration easier. Say you run a company in which three project
teams work on different projects. You would like the members of each team to
1 Some sources <http://xkcd.com/149/> suggest this works in other situations as well.

34
5.6. HIDDEN FILES

be able to read and write the team’s files, but members of one team shouldn’t be
able to read another team’s files. The group mechanism makes this very easy,
by putting users into three different groups and setting the file permissions
correctly.
To see what groups you belong to, you can (for example) use id:
Exercise 5.5 Try this:

wcaarls@vbox:~$ id

It should print your user id (that is, your user number), your group id and
all groups you belong to.
If you belong to more than one group, you can decide which group your file
should have. The command chgrp can be used to assign a new group to the
file, for example:

chgrp othergroup myfile.txt

You are often part of many groups, such as the people allowed to use the
modem (dialout) or that can administer the printer (lpadmin). The role of
groups for sharing files with different people has been largely replaced by other
mechanisms.

5.6 Hidden files


To avoid cluttering the screen whenever you type ls, the convention is that all
files starting with a single . (for example, .bashrc) are hidden files. You will
not see them if you just type ls or ls -l. If you do want to see them, you will
have to use ls -a or ls -la. Try this in your home directory.

5.7 Finding files


Now that you know how you can inspect files in a directory and what the output
of ls -l means, it’s time to learn how to search for specific files. There are a
large number of files on a typical Unix system, so finding that one program you
want can be hard. Some useful commands are:

ls -R : using ls with the -R (for recursive) option will show you all files in all
subdirectories of the directory you’re listing. This can quickly give a lot
of information!
Exercise 5.6 Try this in your home directory:
wcaarls@vbox:~$ ls -R

What do you think ls -lR will do? Try it.

35
CHAPTER 5. FILES AND DIRECTORIES

which : the output of which program is the full path to the program program.
Use this if you want to find out which version of a certain program you’re
actually using.

whereis : does more or less the same as which, but it only looks for programs
in the standard places where Unix stores its programs. It will not find
programs you made yourself. When it finds a program, it also tries to
print the location of the man page.

Exercise 5.7 Use which and whereis to find out where the ls, which
and whereis commands are located.

find : the Swiss army knife of search tools. This command has a lot of options.
The most often used one is -name, for example:

find / -name "*.jpg"

finds all files below the root directory / which end in .jpg

Exercise 5.8 Find all files below /usr/share which end in .txt. Also
find all files below /etc.

Other useful options of find allow you to just select files newer or older
than a certain date, files of certain types, sizes, files with certain permis-
sions, files belonging to a certain user or group, etc. See man find for the
complete overview. For example:

find . -size +80k -ctime +100

finds all files below the current directory larger than 80 kilobytes which
are older than 100 days.

locate : finds a file regardless of where it is on the system. Instead of actually


searching the disk, it consults a database that is updated daily. This is
much faster, but won’t show recently added files.

file : shows information on file types. Just by the name, it’s usually very
hard to judge what a file contains. It might be a program, some text,
some program source code, a figure, an image, a sound etc. The file
command tries to give an accurate description of the contents of a file.

Exercise 5.9 Try this in your home directory:


wcaarls@vbox:~$ file *

36
5.8. SEARCHING FILES

You can use file to find out whether you can view a file on the screen or
not. File types you can show on the screen are: ASCII text, C program
text, Bourne shell script text, perl commands text etc. File types you
cannot view this easy are: data, directory, symbolic link to ..., executable,
etc.

5.8 Searching files


Besides looking for particular files, you may also want to find files containing
particular pieces of text. In this case, the command to use is grep. This
command takes as parameters a string to search for and one or more files:

wcaarls@vbox:~$ grep "19" myfile.txt

The search string can be just a piece of text – here it’s 19. It’s a good idea to
put it between quotes ("), otherwise the shell might interpret a space to mean
it’s a new parameter.
However, you can also supply grep with a regular expression. This is an
expression matching various strings according to a number of rules. Regular
expressions use so-called meta characters:

ˆ "^string" matches string only when it is located at the start of a line.


ˆ "string$" matches string only when it is located at the end of the line.
ˆ "." matches any character.
ˆ "[abc]" matches an a, b or c.
ˆ "[0123456789]" or "[0-9]" matches any single digit number.
ˆ "[^abc]" matches anything but an a, b or c.
ˆ "[abc]?" matches zero or one occurences of any of the characters a, b or
c.
ˆ "[abc]*" matches zero or more occurences of any of the characters a, b
or c.
ˆ "[abc]+" matches one or more occurences of any of the characters a, b or
c.
ˆ "[abc]{n}" matches n occurences of any of the characters a, b or c.

To search for any of the characters used as meta characters, you can put a \
in front. For example, to look for some text between square brackets, use grep
"\[.*\]".

Exercise 5.10 Use grep to find the following expressions in ~/GPL. In each
case, try to predict what will be the output:

37
CHAPTER 5. FILES AND DIRECTORIES

ˆ "[A-Z]"
ˆ " [A-Z] "
ˆ " [A-Z][a-z]*"
ˆ "U[a-z]* "
ˆ "U[a-z]\{3\} "

Note that in the last sequence, the { and }-signs are “escaped” by putting
a \ in front of them; otherwise, the shell would interpret the { and } and just
pass "U[a-z]3 " to grep. More about this later, in section 6.3.
It is also possible to search in multiple files, simply by using a wildcard for
the filename, such as

grep "42" *.txt

Exercise 5.11 Find sentences containing the word Yoyodyne in all licenses in
/usr/share/common-licenses.

We can combine finding files with searching in them using a clever find
option, -exec. This executes a program for every file it finds. If we execute
grep (with the -H option to print the filename), we can search all found files:

wcaarls@vbox:~$ find /bin -exec grep -H "# Copyright" \{} \;


/bin/zmore:# Copyright (C) 2001, 2002, 2007 Free Software Foundation
/bin/zmore:# Copyright (C) 1992, 1993 Jean-loup Gailly

(etc.)
In this line, \{} is the name of the file found (passed as an argument to
grep), while \; closes the command started by -exec.

5.9 Copying, moving and removing files


5.9.1 Copying
To copy files, you can use the command cp. It’s quite simple:

cp originalfile copiedfile

will create a file copiedfile (if it’s not already there) and copy the contents
of originalfile into it. You can specify more than one file to be copied, but
then the last name you specify should be a directory, for example:

cp file1 file2 /tmp

Exercise 5.12 Copy your file myfile.txt to anotherfile.txt. Copy both


files to /tmp. Verify this using ls /tmp.

38
5.9. COPYING, MOVING AND REMOVING FILES

If the file you copy to already exists, cp will ask you for confirmation before
it overwrites it. Be careful!
You can also specify entire directories to be copied, using the -R (recursive)
option:
cp -R somedirectory anotherdirectory
This will create a directory somedirectory in the directory anotherdirectory
and copy all files and directories below somedirectory there.
It can be very useful to transfer files from one computer to another. Instead
of just cp, the command that accomplishes this is called scp, which securely
copies files over the network. It works similar to the cp command, but needs
some more information about server. A typical invocation looks like this:

wcaarls@vbox:~$ scp originalfile user@host:copiedfile

where user is your username on the remote server, and host is its hostname
or IP address. As with cp, you can specify a directory as the copiedfile, and
the -r option works as well. As a convenience, you can omit copiedfile, in
which case the original will be placed in your home directory on the server.

Exercise 5.13 Copy myfile.txt to the rsplinux server and verify that it ar-
rived by logging in remotely and using the cat command. Also try to
copy a file (say, /etc/passwd) from the server to your computer. You’ll
need to do this locally, because the server doesn’t allow any outgoing con-
nections; in this case, the user@host: specification needs to be given for
the originalfile instead of the copiedfile.

5.9.2 Moving
Another option is to rename or move the file. In Unix, both are the same: if
you move a file to another directory, all you do is give it another name. The mv
command moves files and can be called just like cp.

Exercise 5.14 In the previous exercise, you created a copy of myfile.txt


called anotherfile.txt. Rename this file to renamedfile.txt using mv.
Next, move it to /tmp. Verify that it’s really gone from your current
directory and moved to /tmp using ls and ls /tmp.

You can also move entire directories at once: if you mv somedirectory


anotherdirectory, then somedirectory and all files it contains will be placed
below anotherdirectory.

5.9.3 Removing
Finally, you will often want to remove files once you don’t need them anymore.
The rm command does this for you. Be careful: if you remove a file in Unix,
there’s no recycle bin, so there’s no way of getting it back!

39
CHAPTER 5. FILES AND DIRECTORIES

Exercise 5.15 Use rm to remove the file you just moved, /tmp/renamedfile.txt.
Verify using ls /tmp.

Like cp, you can use rm recursively: rm -R. Be very careful with this
option! You can throw away things you didn’t really want to2 .

5.9.4 Common options


The cp, mv and rm commands have two options in common:

ˆ -i: if specified, always ask for confirmation when overwriting or removing


files;

ˆ -f: if specified, never ask for confirmation when overwriting or removing


files.

Especially the -f option can be useful, but also very dangerous, as you can
imagine...

5.10 Making and removing directories


Like files, you will also want to create and remove directories. Creating direc-
tories is done by mkdir somedirectory (for make directory), removing them
is done by rmdir somedirectory. Note that rmdir will only remove a direc-
tory when it’s completely empty, so if you’re really sure that a directory (say,
somedirectory) and all its contents can be removed, you can also use rm -R
somedirectory).

Exercise 5.16 Create a directory newdir. Now copy myfile.txt into it. Try
to remove the directory with rmdir. If that doesn’t work, try to remove
everything at once using rm -R. Be careful!

Exercise 5.17 Create a backup directory and store copies of all text files in
your home directory there (ending in .txt).

5.11 Editing files


We talked a lot about files, but you have so far only looked at files that already
existed. The easiest way to create a new file is of course to use a text editor
from the GUI, such as gedit. However, sometimes this won’t be available (for
example, if you’re working over a network connection). In Ubuntu, the easiest
to learn text-mode editor that’s always available is called nano3 . It helpfully
prints a list of commands at the bottom of the screen.
2 A particularly nasty example is rm -Rf backup /*, which instead of removing all backup

files, removes all files in the root directory.


3 The text editor that is always available is called vi, but is not easy to learn.

40
5.11. EDITING FILES

Exercise 5.18 Use nano to create a new file containing the text “I swallowed
a bug.” Save it under the name river.txt.
 Note
 that ^A is a standard
way of describing the keyboard shortcut Ctrl -A .

41
Chapter 6

Advanced shell commands

Now that you’ve learned how to run basic commands, it’s time to go a bit deeper
into one of the basic elements of Unix: redirection and pipes.

6.1 Redirection
You will have noticed that most Unix programs, such as date, cal, find etc.
have ASCII text as output. Normally this output, called the standard output,
is written to the screen. However, using the > redirection symbol in the shell,
it’s also possible to write the output to a file. For example:
wcaarls@vbox:~$ date > the date.txt
We will say the standard output of date has been redirected to the file the date.txt.
Exercise 6.1 Try the redirection above, and verify with less that things worked
well. Remove the file after you’re done.
In the example above, the file the date.txt is overwritten. Sometimes, you
might want to append text to a certain file. In that case, use the >> redirection
symbol:
wcaarls@vbox:~$ date >> date_list.txt
Standard output is one of the three standard files that most Unix commands
work with. These three are:
ˆ Standard input
ˆ Standard error
ˆ Standard output
Standard error is used to write error messages to. If you don’t specify anything,
these will also be written to the screen. However, you can also redirect it to go
together with the standard output, using >& (or >>& to append).
CHAPTER 6. ADVANCED SHELL COMMANDS

Program 1 Program 2 Program 3

Input file Temp file Temp file Output file

(a)

Input file Program 1 Program 2 Program 3 Output file

(b)

Figure 6.1: Connecting programs: (a) with redirection, (b) with pipes.

Exercise 6.2 Try date -d "yesteryear" > date.txt and inspect the out-
put. Now remove date.txt and try again with the >& notation. After
you’re done, remove date.txt.

Standard input is a very useful feature. For example, you used the notation
less myfile.txt to view the file myfile.txt. However, to feed myfile.txt
to less as standard input, you can use the < redirection sign:

wcaarls@vbox:~$ less < myfile.txt

Exercise 6.3 Of course, you can use both standard input and output at the
same time, e.g.:

wcaarls@vbox:~$ wc < myfile.txt > myfile.stats

Have a look at the man page for wc, it’s a nifty utility. Verify the results
in myfile.stats and remove the file.

6.2 Pipes
While redirection is useful, it has a serious limitation, as illustrated in fig-
ure 6.1 (a). If you want to apply a sequence of operations to a text file, you
would have to create a number of intermediary files. For example, take the
command wc -l, which counts the number of lines in a file. To find out how
many people are logged on at the moment, you might do this:

wcaarls@vbox:~$ who > tmp.txt


wcaarls@vbox:~$ wc -l tmp.txt
wcaarls@vbox:~$ rm tmp.txt

44
6.2. PIPES

It works, but it’s a bit clumsy. To solve this problem, you can connect the
standard output of one command directly to the standard input of another
command using pipes. Pipes (symbol |) work in memory, so no intermediate
files are created. Figure 6.1 (b) illustrates this. The previous example becomes:
wcaarls@vbox:~$ who | wc -l

or, if you would like to store the number of people logged on,
wcaarls@vbox:~$ who | wc -l > count.txt
Some more examples:

ˆ ls -l /usr/bin | less allows you to view the output of ls one screen


at a time.
ˆ ls -l /usr/bin | wc -l counts the number of files in /usr/bin.

ˆ cat /etc/group | cut -d":" -f4 | grep "wcaarls" | wc -l


displays the number of groups the user wcaarls is a member of (have a
look at the man page of cut).
At some point in the pipe, you might want to “siphon off” the data to see
intermediate results, or maybe store them. The command that allows you to
do this is tee (for T-connection). If tee is called with the -a option, it will
append rather than overwrite a file. For example:
cat /etc/passwd | cut -d":" -f1 | tee -a result.txt | grep "d$".
This line will append to an intermediate file called result.txt. What do you
think this file will contain? Verify.
Pipes are what makes the Unix shell very versatile and flexible. We already
discussed the Unix philosophy: make lots of tools, each of which does one
thing and does it well. Pipes form the glue between these tools. Finally, when
discussing pipes, it’s useful to mention cat. This command was introduced in
section 4.5, but it’s a bit more versatile than you thought:
ˆ you can supply a number of file names, which cat will show after another.
For example, try:

wcaarls@vbox:~$ cat myfile*txt

ˆ if you don’t specify a file, cat will use standard input. This is useful for
quickly creating small text files. Try:

wcaarls@vbox:~$ cat > short.txt

You will notice that you don’t get a prompt. Instead,everything


 you type
is stored in short.txt. To stop entering text, press Ctrl -D .

45
CHAPTER 6. ADVANCED SHELL COMMANDS

ˆ the cat command is often used as the beginning of a pipe, for example:

wcaarls@vbox:~$ cat /etc/passwd | grep "false" | sort | less

Try this line. What do you think it does?

6.3 The command line interface


You’ve already used the command line interface quite a bit, so here are simply
some remarks:

ˆ If you specify arguments to commands, protect them using quotes. Pro-


tection means that you do not want the shell to start interpreting meta-
characters. There are two types of quotes. The double quotes (e.g. grep
"some thing" file) just protect against simple meta-characters, such as
space. The single quotes protect quite a bit more. For example, try the
difference between:

wcaarls@vbox:~$ man bash | grep "!!"

and

wcaarls@vbox:~$ man bash | grep '!!'

(the ! metacharacter means has to do with the command history; you


will learn about the history below).

ˆ Even with single quotes, the shell still tries to interpret some meta-characters.
You will have to “escape” these by prepending a \.

ˆ You can use the output of a command as a parameter by using the “back-
tick notation”, i.e. using the single backward quotes.

Exercise 6.4 For example, try this:


wcaarls@vbox:~$ wc -l `find . -name "*.txt"`

What do you think this does? Try it out. Note that wc -l $(find
. -name "*.txt") does the same thing.

Note the difference between backticks and pipes: while a pipe uses a
command’s output as input for another command, the backticks use at
as an argument.

46
6.4. HISTORY

ˆ If you want to run more than one command, but there is no need for pipes,
use a semi-colon. For example,

wcaarls@vbox:~$ cd
wcaarls@vbox:~$ ls

is the same as:

wcaarls@vbox:~$ cd; ls

ˆ If you want to group two commands together, use parentheses. For exam-
ple, try the difference between:

wcaarls@vbox:~$ (cd /bin; pwd); ls

and

wcaarls@vbox:~$ cd /bin; pwd; ls

ˆ Most shells feature command completion. In bash, for example, you just

enter part of a file name and press the Tab  key, the shell will find the
full filename. If there is more than one file that starts with the same
character(s), it will show a list of matches.
Note that the shell tries to find out when you’re looking for a program
or a general file. For example, it will not complete myfil, but it will
complete cat myfil to cat myfile.txt. It even knows how to complete
the options of many programs!

6.4 History
A useful part of any shell is the history. The shell keeps a list of all lines you
typed in since the shell was started. Try it:

wcaarls@vbox:~$ history
 
The easiest way of using the history is moving through it using ↑ and ↓ .
You can also repeat specific lines by using the ! shell built-in command, like
this:

ˆ !! will repeat the last line.

ˆ !13 will repeat line 13.

47
CHAPTER 6. ADVANCED SHELL COMMANDS

ˆ !-5 will repeat the line you entered 5 lines ago.


ˆ !mo will repeat the last command you entered that started with the string
mo.
 
A helpful shortcut is Ctrl -R , which allows you to search in the history.
Just start typing the beginning of a command,
 and it will complete to the last
command that matches. Hitting Ctrl -R again cycles through all matches.

Exercise 6.5 Play around with the history commands.

6.5 Shell scripting


Although the shell looks like a place where you can just type in commands,
it’s actually much more than that. Besides being a command line interface,
it’s also a command language interpreter. This means that you can glue to-
gether commands using simple programming language constructs and work on
variables.
You can create text files containing lists of commands for the shell to process
(as if you typed them in). Such files are called shell scripts, and they are very
useful as they allow you to quickly automate things.
Exercise 6.6 Using nano, create a text file list containing the following text:

ls -l

Now leave the editor and enter:

wcaarls@vbox:~$ sh list

This tells the command interpreter you are currently running, bash, to
start another interpreter (sh) and have it interpret the script list.
It can be done even easier. Re-open the file list and type this in as the
first line of the file:

#!/bin/sh

Leave nano, writing the file to disk. Now change the permissions on list
so that you can execute it (see section 5.4). Next, run ./list:

wcaarls@vbox:~$ list

The line starting with #!1 tells the shell that it will have to use /bin/sh
to run the rest of list.
Now try this:
1 Pronounce: hashbang or shebang. Yes, I know.

48
6.6. PATHS

wcaarls@vbox:~$ ./list /usr/bin

What does it do? And why?

To change this behaviour, we can use the arguments the user specifies in the
shell script. These arguments are called $1 ... $2, where $1 is the first and so
on. You can also get a list of all arguments using $*. Finally, the command
name itself (here, list) is stored in $0.

Exercise 6.7 Re-write your shell script list to contain the following:

#!/bin/sh

ls -l $* | less

Now try again to use ./list to list the files in /usr/bin.

6.6 Paths
You probably noticed that we’ve been calling your shell script using ./list,
not just list. This is necessary because the shell doesn’t automatically search
the current working directory for commands: it is not in the path variable. The
shell uses many variables to control its behaviour (which will be discussed in
more detail in section 6.8). The variable PATH contains a list of directories the
shell will search for programs. It will search the directories in order, so if you
create your own ls script file and put it in a directory before /bin, it will be
used instead of the original system version of ls.

Exercise 6.8 Look at your PATH shell variable using the env command (it can
be helpful to search for PATH using a pipe to grep, see section 6.2).

Exercise 6.9 Have a look at the .profile file in your home directory (this file
is executed every time you start a shell). See how it automatically adds a
$HOME/bin directory to your path if it exists (HOME is the variable that
stores your home directory). Create a ~/bin directory and move your
script there. Now make the shell reread the .profile file using . .profile
and try running list again.

Exercise 6.10 Now create a script cdl in ~/bin which:

ˆ changes the directory to the directory specified as an argument;


ˆ shows a list of files (including permissions) and pipes the output
through less.

Exercise 6.11 A difficult question: why should .profile not contain a line
#!/bin/sh ?

49
CHAPTER 6. ADVANCED SHELL COMMANDS

6.7 Aliases
A very useful property of the shell is aliasing. This means that you can create
your own “short hand” notation for commands you often use. An alias has the
form
alias name='command'
in which name is the short hand you want to give the command.
To get an overview of all active aliases, you can just enter alias without
arguments. To get rid of aliases, use unalias.
Exercise 6.12 Create an alias for find . -name; call it f and try it using
f "*.txt". Also, put this line in your .profile file, so you can use it
whenever you log on.

6.8 Environment variables


Above, you already had a look at shell scripts. These scripts can do more or less
everything you can do with any programming language (although sometimes in
a somewhat complicated way). One of the most useful things is that you can
use variables (also called environment variables). Some common environment
variables are:
ˆ HOME is the environment variable which contains your home directory;
ˆ PATH contains a list of directories the shell will search for programs;
ˆ TERM shows you what terminal type you are working on.
You can get a list of environment variables by entering env, and create or
change environment variables by entering <variable>=<contents>. Some of
the environment variables contain useful information, such as USER, and PWD
etc.
To use variables in your scripts, you can access their contents by putting a
$ in front of them. For example, try this:
wcaarls@vbox:~$ MYNAME="A. User"
Now you can use the contents in the following way:
wcaarls@vbox:~$ echo "My name is $MYNAME"
This is probably also your first encounter with the very useful utility echo,
which just prints everything you put behind it to the screen.

Exercise 6.13 Create an executable shell script ~/bin/aboutme that prints


your username and home directory.

Just like aliases, environment variables are lost when you logout unless you
put them in your .profile file.

50
Chapter 7

Processes

As said before, Unix is a multi-tasking operating system, like many operating


systems nowadays (examples of single-tasking operating systems are Apple’s
MacOS and Microsoft DOS, both of which are very old). Multi-tasking means
that more than one program (or process) can run – seemingly – at the same
time. However, as there are usually more processes than processors, what really
happens is that each process runs for a little while, say 20 milliseconds, and
then another process is run, in turn.
First, we’ll discuss how you can view your own processes and learn more
about them. Next, it is shown how you can control your processes.

7.1 Inspecting processes


The most useful command to inspect running processes is ps, for process status.
Try this:

wcaarls@vbox:~$ ps

The output should look like this:

PID TTY TIME CMD


1736 pts/0 00:00:00 bash
2211 pts/0 00:00:00 ps

There are two processes in your current shell: bash (the shell you are cur-
rently running) and ps itself. The first column of the output shows the process
identification number (PID). Each process has a unique number. The next col-
umn shows in which terminal this process is running; in this case it’s terminal 1.
Next, ps shows how much time has been used by the process so far. As usually
these commands run very quickly, here it’s 00:00:00, or less than 0.5 seconds.
Finally, ps shows the command name of the process.
To get more information, specify the option u to ps. Try this. You will get
a lot of (cryptic) information, such as the PID (process identification number)
CHAPTER 7. PROCESSES

of the processes and the size in memory (RSS) in kilobytes. Also interesting is
the STAT field, which shows the status of the process: S for “sleeping”, R for
“running” and so on. Note that “sleeping” means “currently not running on
the processor”; it might be running in another 20 milliseconds or so!
The ps command has a lot of options. Besides u, a useful one is a, which
also shows you the processes of other uses Try this:

wcaarls@vbox:~$ ps au

You will see that root is running a lot of getty processes – these are the virtual
terminals you encountered in section 4.1. A very useful combination of options
is aux (look at the man page of ps to find out what this does):

wcaarls@vbox:~$ ps aux | less

The first process you should see in this list, the one with PID 1, is the “mother
of all processes”, /sbin/init. As each process can only be started by another
process, the Unix system starts by creating this special process. All it does is
create other processes based on initialisation files. This means there’s a hierar-
chy of processes: each process (except init) has a parent process and may have
child processes.
To get a view of this hierarchy, try this:

wcaarls@vbox:~$ pstree

Have a look at what level the process pstree runs at.

Exercise 7.1 From your current shell, start another shell by entering bash.
Do this twice more. Use ps and pstree again to inspect the processes.
Do they confirm what you think happened? Exit the spurious shells by
typing exit three times.

7.2 Foreground and background


You will have noticed that when you start a process from the shell, it will not
prompt you again until the process is finished. In some cases, you might want to
start a process but return to the prompt immediately. This is especially useful
for programs that run for a long time, such as long calculations.

Exercise 7.2 To start a process in the background, put an ampersand (&) be-
hind the command name. For example, try:

wcaarls@vbox:~$ openssl speed >& speed.log &

The output should be something like:

[1] 2350

52
7.3. KILLING PROCESSES

which means that the shell has started the first background process ([1])
and it has process number 2350. The openssl speed command measures
your processor’s competence at cryptographic tasks.
Remember that the >& speed.log means that all output should be written
to the file speed.log instead of the screen. This is important; if a process
is running in the background and it writes to the screen, it will mess up the
foreground programs.
Exercise 7.3 To find out whether your process is still running, use ps. You
can however also use jobs, which shows the current background processes:

wcaarls@vbox:~$ jobs
[1]+ Running openssl speed &>speed.log &

You can move the process back to the foreground using fg <job>. In
this case, if you enter fg 1 (or just fg), your process will return to the
foreground, and you will not get your prompt back.
 
To suspend a process, press Ctrl -Z while it’s running. You will get the
prompt back, and again you can decide whether you want to run it in the
foreground (fg) or you want to run it further in the background (bg). Try
suspending your openssl-process and moving it to the background.
Remember: per terminal, there can be only one process in the foreground at
the same time!

7.3 Killing processes


Now you know how you can move processes to the background and the fore-
ground, you might wonder how you can stop processes. The simplest solution,
when the process is running in the foreground, is to press the “interrupt” key,
 
Ctrl -C .
Exercise 7.4 Move your openssl process back to the foreground  (or start a
new one if it’s finished already), and stop it using Ctrl -C .
Another option is to use kill. You can use kill in two ways:
ˆ kill <PID> will stop the process with the specified process identification
number PID;
ˆ kill %<job> will stop the process running as job number job in the cur-
rent shell.
Exercise 7.5 Have a look at the man page of the command sleep. What does
it do?
Start the process sleep 300 & and look at the job number return (e.g.,
[1]). Stop it using the job number, e.g. kill %1. Start it again, and now
kill it using the PID.

53
CHAPTER 7. PROCESSES

Actually, the kill command sounds far worse than it is. It really just sends
a signal to a process asking it nicely to please do whatever is necessary to stop,
such as saving files, cleaning up etc. Sometimes however, programs are crashed
and will not respond to the nice request. In that case, you can force such a
runaway process to die by using kill -9 <PID> or kill -9 %<job>.

7.4 Nice!
It was already discussed how processes are run in turn, each receiving a small
amount of time, to give the user the impression all processes are running at the
same time. To get a nice, interactive overview of all these processes, you can
use the top command:

wcaarls@vbox:~$ top

which you can leave by pressing q .
But how does Unix decide which process to run at any given time? The
basic idea is to run each process as often as each other process. However, some
processes should have a higher priority, such as processes which regulate hard
disk access, network transport, the clock etc.
To allow processes to have higher priority, each process has a value called
nice. The nicer processes are, the less priority they get. The default value for
each process is 0; the nicest level is 19, and the level with the highest priority
is -20. To prevent normal users from attaching too high a priority to their own
processes, you can only assign “nicer” levels to your processes; that is, the nice
level can only be increased. Only the system administrator can give processes
a lower nice level and thus a higher priority.

Exercise 7.6 Start three long-running processes like this:

wcaarls@vbox:~$ openssl speed >& /dev/null &


wcaarls@vbox:~$ nice -n 10 openssl speed >& /dev/null &
wcaarls@vbox:~$ nice -n 15 openssl speed >& /dev/null &

Note the /dev/null device: everything you write to it is thrown away,


which can sometimes be very useful to get rid of unwanted output.
Now inspect how much time each process gets on the processor, using top.
Finally, kill all three processes using killall openssl (what does this
do? Have a look at the man page.).

Besides starting processes with nice, you can also use renice to change the

nice level of a running process, or press r while in top.
It’s often important to be nice, especially if you’re working with a large
number of people on a single machine.

54
7.5. SUSPENDING AND RESTARTING TERMINAL SESSIONS

7.5 Suspending and restarting terminal sessions


You now know how you can suspend processes, make them run in the back-
ground, and bring them back to the foreground. Unfortunately, this does not
work across multiple terminal sessions. This may be important when you are
running an interactive process across the network, and you want to access it
from two different computers (say, at work and at home). The screen com-
mand helps you do this.

Exercise 7.7 Log in to rsplinux (see section 4.6 if you forgot how). Start
screen by running

wcaarls@vbox:~$ screen

Skip through the copyright notice. You will see a new prompt on an empty
screen. Start our favorite command

wcaarls@vbox:~$ openssl speed

Now close the terminal.


Open a new terminal, log in to rsplinux again, and confirm that you’re
running a screen session

wcaarls@vbox:~$ screen -list

you should see something like

There is a screen on:


4471.pts-3.TUD419056 (07/19/2010 11:30:12 AM) (Attached)
1 Socket in /var/run/screen/S-wcaarls.

You can now take over this session by typing

wcaarls@vbox:~$ screen -r

Notice how the program kept computing in your absence and how the
contents of the terminal are transferred (reattached ) to the new session.
If you forgot to close the first terminal, you would need to run screen
-dr, to first detach the previous session.

screen has someusefulkeyboard shortcuts, described in the manual pages.


They all start with Ctrl -A , followed by another key. Some frequently used
ones are:
  
ˆ Ctrl -A d detach session;
  
ˆ Ctrl -A \ quit session, killing all running commands;

55
CHAPTER 7. PROCESSES

  
ˆ Ctrl -A c create a new window;
   
ˆ Ctrl -A Ctrl -a switch to the last used window.
  
ˆ Ctrl -A n switch to the next window;
  
ˆ Ctrl -A p switch to the previous window.
Again, being able to use multiple windows in a single session is particularly
useful when working over a network.

Exercise 7.8 Create a new virtual terminal in your screen session and start
  
the nano editor. Now detach (Ctrl -A d ) and killall openssl. Reat-
   
tach (screen -r) and switch to the first window (Ctrl -A Ctrl -a ) to
check that openssl is really
 terminated.
 Then quit screen, killing all
running commands (Ctrl -A \ ).

Note that it’s quite easy to leave screen sessions lying around. Make sure
to use screen -list to see if you’ve forgotten to close any.

56
Chapter 8

Exercises

You can finish the course by performing two of the exercises below.

1. Write a script readnews that displays the current headlines on Globo.com:

ˆ use wget to download the RSS feed from http://g1.globo.com/dynamo/rss2.xml


ˆ use tr to replace < with newline characters (make sure you use ap-
propriate escape sequences)
ˆ use grep to only retain lines starting with "title"
ˆ use sed to remove the first part of each line up to title> (use the
’s/regexp/replacement/’ argument)

2. Create a script mycp <input file> <output file> that copies a file
<input file> to <output file> without using cp:

ˆ use ls -l to get information on the <input file>


ˆ remove double spaces using tr -s " "
ˆ get the word containing the amount of bytes in the file using cut -d"
" -fX, where X is the position of the file size in the line
ˆ use dd to do the actual copying (look at the bs option)

3. Make your shell display the amount of free space on the disk of the root
directory:

ˆ create a script mydf:


– call df
– use head and tail to find the line containing the root directory
– use tr -s " " to remove double spaces
– use cut -d" " -fX to get just the field containing the amount
of free space, where X is the position of that field
CHAPTER 8. EXERCISES

ˆ place mydf in your ~/bin directory and make sure you can execute
it from anywhere on the file system
ˆ use the PROMPT COMMAND environment variable to execute mydf every
time you get a new prompt (see man bash)
4. Write a script killme that kills all your processes:

ˆ use ps -aux to get a list of all processes


ˆ use grep to find the ones belonging to you
ˆ use tr -s " " to remove double spaces
ˆ use cut -d" " -fX to find the process number, where X is the posi-
tion of the process number field
ˆ supply this output to kill -9

58

You might also like