Professional Documents
Culture Documents
Linux
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
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
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!
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-
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
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
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.
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
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.
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.
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.
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.
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.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
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.
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.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 .
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
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.).
18
3.4. GETTING HELP
Exercise 3.7 Connect a USB disk to the computer, and find out where it’s
mounted.
Exercise 3.8 Open the cpuinfo file on the /proc filesystem. What does it say
about your processor?
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.
19
CHAPTER 3. GRAPHICAL USER INTERFACE
Exercise 3.9 Find out what you should do if you lose your password.
20
Chapter 4
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.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:
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:
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.
NAME
cal, ncal displays a calendar and the date of Easter
SYNOPSIS
23
CHAPTER 4. THE SHELL
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...
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?
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.
cd without any options will take you back to your home directory;
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.
27
CHAPTER 4. THE SHELL
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.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
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.
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:
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
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
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:
[abZ] will match the letter a, or the letter b, or the letter Z. For example:
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).
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:
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
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.
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:
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.
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
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:
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:
finds all files below the current directory larger than 80 kilobytes which
are older than 100 days.
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.
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.
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:
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
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:
(etc.)
In this line, \{} is the name of the file found (passed as an argument to
grep), while \; closes the command started by -exec.
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:
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:
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.
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 .
Especially the -f option can be useful, but also very dangerous, as you can
imagine...
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).
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
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
(a)
(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:
Exercise 6.3 Of course, you can use both standard input and output at the
same time, e.g.:
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:
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:
if you don’t specify a file, cat will use standard input. This is useful for
quickly creating small text files. Try:
45
CHAPTER 6. ADVANCED SHELL COMMANDS
the cat command is often used as the beginning of a pipe, for example:
and
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.
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
wcaarls@vbox:~$ cd; ls
If you want to group two commands together, use parentheses. For exam-
ple, try the difference between:
and
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:
47
CHAPTER 6. ADVANCED SHELL COMMANDS
ls -l
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
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
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.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.
Just like aliases, environment variables are lost when you logout unless you
put them in your .profile file.
50
Chapter 7
Processes
wcaarls@vbox:~$ 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):
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
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.
Exercise 7.2 To start a process in the background, put an ampersand (&) be-
hind the command name. For example, try:
[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!
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.
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
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:~$ 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.
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.
2. Create a script mycp <input file> <output file> that copies a file
<input file> to <output file> without using cp:
3. Make your shell display the amount of free space on the disk of the root
directory:
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:
58