Unix Lab Course Manual
Unix Lab Course Manual
1 Introduction 6
1.1 What is Unix? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.2 The Unix timeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.3 The Unix philosophy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.4 So, what is Unix? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2 Lab hardware 10
2.1 The network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.2 Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.3 The terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3 Logging in 13
3.1 Logging in . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.2 Entering commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.3 Logging out . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.4 Remote logins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.5 Passwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
5 First steps 23
5.1 Some basic commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
5.2 The manual pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2
7 Files and directories 34
7.1 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
7.2 Wildcards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
7.3 Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
7.4 File permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
7.5 User groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
7.6 Hidden files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
7.7 Finding files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
7.8 Searching files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
7.9 Copying, moving and removing files . . . . . . . . . . . . . . . . . . . . . . 40
7.10 Making and removing directories . . . . . . . . . . . . . . . . . . . . . . . 42
10 The shell 54
10.1 The command line interface . . . . . . . . . . . . . . . . . . . . . . . . . . 54
10.2 History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
10.3 Different shells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
10.4 Shell scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
10.5 Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
10.6 Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
10.7 Shell and environment variables . . . . . . . . . . . . . . . . . . . . . . . . 59
10.8 The prompt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
10.9 User information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
11 Processes 61
11.1 Inspecting processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
11.2 Foreground and background . . . . . . . . . . . . . . . . . . . . . . . . . . 62
11.3 Killing processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
11.4 Core dumps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
11.5 Nice! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
12 X-Windows 66
12.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
12.2 Window managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
12.3 Xterms and other consoles . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
12.4 Remote programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
12.5 Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
14 Exercises 76
4
Foreword
This course has been written to be completely self-contained. It mixes pieces of text and
theory with examples and exercises you should perform yourself. Please read the text
carefully (it might contain some small things for you to do), and do the accompanying
exercises. Write down the answers for later discussion with the supervisor; however, it’s
not necessary to write a full report.
Although in principle most of the text and exercises should be applicable to any Linux
distribution, discussion of the network setup and applications is geared specifically to a
number of RedHat Linux 6.2 workstations installed in the CSP Lab Room. Furthermore,
the account created for you will contain a number of files for you to work on. Therefore,
it is strongly advised not to do this course at home without consulting the supervisor.
At the end of the course, you should be able to write a small shell script. This will be the
basis for the grade you receive.
Some notation: anything you should see on your screen is shown in Courier. All commands
in Unix 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 .
5
Chapter 1
Introduction
This first section contains an overview of what Unix is. You’re supposed to read it before
you start doing anything with Unix, because it will give you an idea of why Unix behaves
like it does. In section 3, you will really start using the computer.
6
project was really too large and it didn’t look like it would ever finish.
When a few of the researchers received a new, smaller computer, the PDP-7, they (Dennis
Ritchie, Ken Thompson and some others) realised that it would never be able to run
MULTICS, if only because it had just 4 kilobytes of memory1 . So, they put together a very
simple basis for an operating system2 . Although it was based on the idea of MULTICS,
it really consisted of no more than a shell, which allowed you to type 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.
In 1973, Dennis Ritchie and another researcher, Brian Kernighan, developed a new com-
puter programming language called C (as a successor of B). Unix was translated to C, and
after that it really took of. 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 us-
ing 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 commands. One especially important branch of Unix was
made at Berkeley University in California; they put in the important TCP/IP networking
code, which is the protocol computers use to talk to each other in the Internet. This
branch is still alive today in the FreeBSD, NetBSD and OpenBSD operating systems. The
networking code even ended up in Linux and Windows 2000.
In the beginning of the 1980s, computer companies such as Hewlett-Packard, IBM, NCR,
Sun and SGI also started shipping their computers with Unix installed. Although this
meant that Unix became even more widely used, it also lead to a large number of Unix-
like operating systems which were slightly incompatible. In the end, groups even started
court cases against each other as to which Unix could call itself Unix (the “Unix wars”).
Later, some versions of Unix were constructed which were not even based on the original
Unix source code anymore. Still, these operating systems (such as QNX, MINIX and
Linux) behaved like Unix. The lab course makes use of Linux, which will be discussed
below.
Finally, in the 1990s companies started working together in groups called X/Open, OSF
and later the Open Group. They decided to put together a number of specifications of
1
A kilobyte contains 1024 bytes (characters), a megabyte contains 1024 kilobytes, a gigabyte 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/).
7
User tools
Shell OS
Kernel
Hardware
what a Unix system should be able to do and how it should work. This lead to Unix
95, Unix 98 and the Single Unix Specification; not operating systems, but rules which all
Unix-like operating systems should follow so that they react in the same way. Although
we’re not quite there yet, today it’s reasonably simple to compile the same program on
SGI, Sun, IBM or HP machines.
8
everything had to be done through the graphical user interface shell (GUI). The Windows
series of operating systems has included more and more of the Macintosh ideas, to such
a point that it is now nearly impossible to run programs on a Windows machine without
having to use the graphical user interface shell.
In contrast, a basic element of Unix has always been to create parts of the operating
system that can work individually, that do only one thing, and do it well. The Unix
kernel still really does nothing more than talk to devices. The shell, although it’s very
powerful for advanced users, offers no graphical user interface. If you want one, you can
run a user tool called X-Windows, but this is not part of standard Unix.
Furthermore, all user tools are also simple and usually do just one thing. 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. For example,
the sort command does nothing else than take text input, sort it and putting it out. Even
the graphical user interface does nothing more than draw things on the screen; if you want
windows, you can run any of a number of window managers (which, in turn, just draw
windows).
Finally, you need a way to make the boxes talk to each other. In Unix, this is done using
text streams. Most user tools accept text streams as their input and give text as output.
You can then connect the tools using pipes, a very important mechanism which will be
discussed in more detail later.
9
Chapter 2
Lab hardware
10
Internet
csp00
switch
Figure 2.1: The CSP lab network. Note how workstations csp01 and csp02 talk to each
other through a network switch, and to the Internet through the server, csp00.
gigabytes of memory and terabytes (even petabytes) of disk. PCs simply are not ready yet
for such jobs.
2.2 Linux
In the 1980s, several groups tried to write Unix-like operating systems for the PC. These
PCs were still a bit clunky (a typical 1988 PC sported a 80286 processor with 1 megabyte
of memory and 30 megabytes of disk), so these Unices typically had to make concessions.
They were therefore not as stable as Unix users had become accustomed to.
In 1991, a Finnish computer science student called Linus Torvalds began working on a
small computer project which eventually grew into Linux, a full-fledged Unix-like operating
system for the PC which you have probably heard of2 . In the beginning, you couldn’t do
very much with Linux, because it’s only a kernel, which is the bit of the operating system
which performs all the basic functions but doesn’t do anything else (remember figure 1.1).
But, when people started packaging it with a set of tools developed for free by the Free
Software Foundation (FSF), the GNU tools3 , Linux really took off.
2
In fact, in his design Torvalds was influenced a great deal by MINIX, a Unix for the PC developed
under supervision of Andrew Tanenbaum, a famous professor of computer science based in Amsterdam.
3
GNU is a recursive acronym: it stands for GNU’s Not Unix. The “movement” was started in the
1980s by Richard Stallman, a computer scientist at MIT who feels that all software should be free (free
as in speech, not as in beer). His goal was to create a Unix-like operating system based entirely on
software everybody could freely distribute. The only thing he really lacked was a kernel, which is the
thing that Linus Torvalds wrote; and since Linux is free software too, his dream came true. Ironically, the
whole system is now known as Linux instead of GNU, so every now and then he complains that Linux
distributions should be called GNU/Linux. See http://www.gnu.org/.
11
At this moment, Linux is the fastest growing operating system for server PCs, also thanks
to the large set of free software you get with every distribution, such as the excellent Apache
web-server. The only thing still lacking is a good, unified user interface such as the one the
Windows family of operating systems has, although several people are working on that.
Note: if, after you have started your workstation, you see a graphical login
screen, press Ctrl -Alt -F1 first to go to a virtual terminal.
To shut down the workstation after you have finished, go back to the
graphical login screen by pressing Ctrl -Alt -F7 . Choose the System menu
and then Halt. Wait until the screen says Power down. Only then switch the
workstation off by pressing the power button on the front.
12
Chapter 3
Logging in
An important element of Unix is that it’s a multi-user operating system. That means that
several people can work on it, not just sequentially as in Windows 2000, but at the same
time. Therefore, you will have to tell the computer who you are when you start. This is
called logging in.
3.1 Logging in
If you’ve started up your workstation like it was explained in section 2.3, you should see
something like this:
Redhat release 6.2 (Zoot)
Kernel 2.4.6 on an i686
csp00 login:
The operating system, in this case RedHat Linux, announces itself with its version number
(6.2), the version number of the Linux kernel it’s running (2.4.6) and the processor it’s
running on (i686, an Intel Pentium III).
Then it prints its name. In Unix, each machine has a name. If the computer is connected
to the Internet, the name should be unique, so other machines know where to find it. So,
depending on which workstation you’re using, it will say csp00, csp01, csp02 etc. In the
remainder of this manual, it will be assumed you’re working on csp00. In the exercises,
substitute csp00 by the name of your workstation!
Of course, different Unix machines will give different responses. For example, the adminis-
trator might not want everybody who tries to log in find out which version of the operating
system is running, for security reasons. However, the response will look similar to this one.
Exercise 3.1 To log in, enter the login name you were given and press Enter . The system
13
will now ask you for your password to prove that you are who you claim to be (note
that it will not show what you type, to prevent people looking over you shoulders
from learning your password):
Password:
Type in the password and again press Enter . If you did something wrong, the system
will say so and give you another opportunity:
Login incorrect
login:
If all went well, the system will now let you in.
After you log in, the system will print some information first:
• optionally, the message of the day, in which the system administrator can put mes-
sages that might be important for some users, for example:
The power will be down on Sunday Aug 26 between 18.00 and 21.00.
Save all your work and log out before that time.
• optionally, a fortune cookie, a short joke with which the computer welcomes you.
Next, the system will show a shell prompt. This “prompts” you to enter some commands.
As your account has been prepared to start off using the TC-shell (command name: tcsh),
your prompt will look like this:
csp00~>
The prompt shows the machine you’re currently working on, the directory you’re working
in, and the > sign to indicate where you can start typing1 .
1
Other shell programs (such as the Bourne shell sh, the Bourne again shell bash or the C-shell csh)
will show different prompts. However, you can always change them to your liking; see section 10.3.
14
3.2 Entering commands
As you already know, the shell program you’re now working in is meant to be a layer
between you and the kernel. You type in commands, the shell figures out which user
program(s) to run and instructs the kernel to do so. In most of the exercises below, you’ll
be typing in commands yourself, so it’s good to know the following things:
• if you make a mistake in typing, use the left and right cursor keys (← and → ) to
go back and the Backspace or Delete keys to erase characters2 ;
• you can jump to the start of the line using Ctrl -A , and to the end of the line using
Ctrl -E ;
• you can delete everything from the position of the cursor to the beginning of the line
using Ctrl -W ;
• you can delete (kill) an entire line using Ctrl -U ;
• you’re always supposed to finish your input by giving Enter ;
• you can stop any command that’s running at the moment by typing Ctrl -C , the
interrupt key;
• when a program produces a lot of output, you can stop the output being drawn on
your screen by entering Ctrl -S , and resume it by pressing Ctrl -Q ;
• you can create a beep by entering the bell key, Ctrl -G , which is not often useful
but sometimes funny;
• when you’re entering input for some command (not on the command line!), you
should often end by typing Ctrl -D .
You can imagine that, because Ctrl -D also means logout in the shell (which will be
discussed in the next section), you should be careful in using it.
In the Linux virtual terminals, you can also scroll back – that is, look at earlier output –
by pressing Shift -Page Up . Scroll down again by pressing Shift -Page Down . However,
other terminals might not offer scroll-back, or might use different keys.
Exercise 3.2 Enter some text on the command line and play around with the key com-
binations shown above.
Press Ctrl -S , enter some text and press Ctrl -Q . Note how this can be confusing
– when you think your workstation doesn’t respond anymore, always try to press
Ctrl -Q before you start thinking about rebooting!
2
Unlike Windows, Unix usually treats Backspace and Delete as the same thing, removing the
character left of the cursor.
15
3.3 Logging out
Before you do anything else, it’s good to know how you can leave the system. If you go
away, you don’t want to leave your computer in the state it is now, because anybody can
come in and look at your files, messing with them or even change your password! Logging
out is done simply by entering:
csp00~> logout
or, at least in tcsh, by pressing Ctrl -D 3 .
The system will give a new login prompt, for the next user.
Exercise 3.3 Type in: rlogin csp0X, where X is a number between 0 and 2, and different
from that of the machine you’re working on. The computer will first assume your
login name is the same as the one you’re using now, so it will only ask for your
password; if you make a mistake, it will ask you for your login name as well.
If you get a login prompt, follow the procedure to log in. You’re now working on a
different machine! If you don’t get a login prompt, the machine might not be running
Linux at the moment; try another machine.
Log out of the machine to return to your local workstation.
3.5 Passwords
In a multi-user environment, especially if your workstation is connected to the Internet,
it’s extremely important to make sure nobody is allowed to mess with your files. There
are some basic mechanisms for making sure that people working on your system cannot do
this, which will be discussed later. On top of that, you certainly don’t want unauthorised
access to your workstation: hence the need for passwords.
Passwords can be changed using the passwd program4 . First, it’s good to think about
what your password should be.
3
Often, in manuals, you will find the notation ^D instead
of Ctrl -D . This is the same thing; the ^
is an abbreviation for pressing a key while holding down Ctrl .
4
On some networked systems, you might have to use yppasswd. Your system administrator will tell
you which program you should use.
16
Originally, passwords were very hard to crack, as the only way to do it is by brute force:
trying a large number of passwords to find out which is the right one. However, as comput-
ers have become much faster, it’s now possible to try a lot of passwords in a small amount
of time. Because many people choose ordinary words that can be found in the dictionary,
or names of friends, pets, cars etc., this means passwords can be very unsafe.
Therefore, it’s good practice to choose a hard-to-crack password. The password should
preferably include some non-alphabetic characters, for example !, # or $. A standard way
of creating a difficult password is to think of a sentence only you know and then take the
first letter of each word. For example, the string might be “This is a very secret password!”.
If you take the first letters, you get the password Tiavspw!. You can even replace some
letters by non-alphabetic equivalents, so you get T=1vspw!.
Exercise 3.4 Execute the passwd command. First, the program will ask you for your old
password to make sure it’s you. Then, it will ask you for the new password. Use
the rule above to create a hard-to-crack password. Again, you will have to enter it
blindly (it will not echo the password), so be careful. You have to enter the password
once more to make sure you didn’t make any typo’s.
Now log out and log in using the new password.
Note that you should try to remember your password well (but don’t write it down!). If
you forget it, you will have to ask the system administrator to help you out, and system
administrators often have better things to do.
Exercise 3.5 Log out and try to log in with a non-existing name, e.g roodkapje. The
system of course knows there’s no user roodkapje, but will still ask for a password.
Why do you think this is?
17
Chapter 4
Now that you know how to log in, and you have chosen a secure password, it’s time to
have a look around the system.
18
If you ever get confused as to who you are yourself, you can try the command
csp00~> who am i
Exercise 4.1 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?
Exercise 4.2 Use the finger command to find out about yourself:
csp00~> finger c unx001
in which, of course, you should replace c unx001 by your own login name.
Note that this gives quite a bit of information, among which your shell (tcsh),
whether you’ll accept messages (these will be discussed below) and what your plan
is. We’ll discuss plans later.
Also try to finger a user at another machine, for example on csp02:
csp00~> finger c unx001@csp02
Exercise 4.3 Press Ctrl -Alt -F2 to open a second virtual terminal. Log in. Use who
to find out how many users there are at the moment. Remember the name of the
second terminal (for example, ttyp1).
19
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:
csp00~> mesg n
so that users trying to write to you will see the message
write: c unx001 has messages disabled on ttyp1
Exercise 4.5 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. 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 entering talk c unx001@csp00.
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.
Note: the talk command currently does not work on csp00. If you are
working on csp00, skip this part, or try to use one of the other workstations.
As with other short message services, such as SMS and e-mail, there’s a bit of a protocol
(especially useful with slow connections): entering -o at the end of a sentence means
“over”, i.e. that the other user can start typing; entering o-o means “over and out”.
Exercise 4.6 Open a talk session between your first and second virtual terminal.
Once you’re tired of talking to yourself, log out of the second virtual terminal and
return to the first.
optional
Exercise 4.7 If someone else is logged on to your system, try to talk to him/her.
end optional
20
4.3 E-mail
As was already discussed in section 3.1, each user has a mailbox. Anyone on the system
can send e-mail messages to you, which will be stored in this box (the box itself is a file in
a special directory). Since you received a message that you have mail when you logged in,
there’s some mail waiting for you.
The simplest command to read your mail (but also the least user-friendly) is mail.
Exercise 4.8 Enter the command mail. You will now see a line describing how many
messages you have and how many still are unread. Below, for each message you will
see a line showing whether the message is new (N), unread (U, which means that
you’ve seen that it’s there but you haven’t read it yet) or read (M). It also shows the
sender, the date and time it was received and the subject of the message.
Note that the prompt has changed to:
&
This is because the mail program has its own mini-shell, in which you can enter
commands for the mail program itself. You can get a list of possible command by
entering ? at the prompt.
In mail, one of the messages is the active message, marked with a >. To read the
active message, enter t; to read another message, enter t n, where n is the message
number. Afterwards, message n is the active message. Try this out.
Try at least the following:
• With the ! command, you can perform a shell command from within mail.
For example, with !who, you can see whether the user who sent you a message
is logged on. You’ll be automatically returned to mail after the command
completes.
• Store message 1 in a file called mymail, using the command s 1 mymail. Then
leave the mail program using x and start it again. Is your mail still there?
• An automatic way of storing the messages you’ve read is by leaving the
program using q. This will store the messages in a file called mbox in
your home directory (about which you’ll learn more later). Try this.
Note: currently, using the q command on Linux gives a
segmentation violation, an irrecoverable error in Unix programs.
Therefore, you cannot test this at this moment.
• If you now start mail again, you’ll notice that all messages have gone.
The mail command also allows you to compose e-mail messages and send them, for exam-
ple, to your fellow users. However, in order to do this you’ll first need to learn how to edit
text files. We will get back to this later.
21
As said before, mail is the simplest command that allows you to read your e-mail. There
are some much more advanced and user-friendly programs, such as elm and mutt.
Exercise 4.9 Try elm and mutt. You can walk through the messages using the cursor
keys and read them by pressing Enter .
Look at the messages both programs print to find out how to leave them.
Under X-Windows, you can also use e-mail reading programs with a graphical user inter-
face. We will discuss these later.
22
Chapter 5
First steps
Now that you know how to type in commands and read mail when it comes in, it’s time to
learn some basic commands. 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.
5.1.1 date
A very useful command is date, which without arguments tells you what date it is:
csp00~> date
Wed Aug 29 20:53:42 CEST 2001
The output is fairly self-explanatory, except perhaps the CEST part: this indicates it’s
Central European Standard Time.
Exercise 5.1 One particulary interesting option of date is -d. The option tells the pro-
gram 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:
csp00~> date -d "tomorrow"
Thu Aug 30 20:56:05 CEST 2001
csp00~> date -d "yesterday-1 week"
Tue Aug 21 20:56:19 CEST 2001
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.
23
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 5.2 Find out what switches date accepts by using the --help switch.
5.1.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:
csp00~> cal
August 2001
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 5.3 The first argument cal takes is the year. Try cal 2000 and cal 2525.
Remember how to scroll back through the terminal (see page 15).
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 brackets, 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 5.4 What would happen if you would give the year first, then the month? Try
this: cal 2001 12.
24
5.2 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 command. 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.
Exercise 5.5 Use the man command to find out what the cal command does and what
arguments it takes. You will see something like this:
csp00~> man cal
CAL(1) System Reference Manual CAL(1)
NAME
cal - displays a calendar
SYNOPSIS
cal [-mjy] [month [year]]
DESCRIPTION
Cal displays a simple calendar. If arguments are not specified,
the current month is displayed. The options are as follows:
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 .
According to the manual page, when did the Gregorian Reformation occur? Use this
information to find out on what day the calendar skipped 10 days.
Manual pages often tell you the name of the command, how to use it (synopsis), 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.
Exercise 5.6 Take a look at the man page of the shell you are using, tcsh. Look for the
BUGS section by pressing / followed immediately by BUGS.
Scroll down a bit to find out what the t in tcsh stands for. Even lower is a list of
AUTHORS. Which Dutch person apparently made some ”Vi mode fixes”?
This last part of the man page shows you that some Unix commands really are the result
of a large number of programmers working together. For the tcsh, at the bottom of the
BUGS section, you can even find out where to send an e-mail if you think you’ve find a new
bug. The honesty and opennes of man pages is one of the results of Unix being developed
mainly by academics instead of commercial organisations.
25
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
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 command exactly.
The apropos command also shows descriptions of commands which match your argument
partially, which makes it easy to search for a command of which you don’t exactly know
the name.
Exercise 5.8 Find out in which sections manual pages reside on passwd, using whatis.
Look at both of the passwd man pages; what is the difference?
Now use apropos on the term passwd. What do you notice?
Look for programs to do with editing, using apropos edit.
Now that you’ve seen how a typical command works, and you know where to find help on
new commands, it is time to learn something about the basis of Unix, the file system.
26
Chapter 6
A file system usually is a way in which the computer organises commands and data on the
hard disk. The commands you have tried above all reside on a hard disk somewhere, so
when you type in (for example) date, the computer will have to know where to find the
bytes that belong to date, how many there are and how to read them into memory. Such
a sequence of bytes, together with information like it’s name, location on the hard disk
and size, is called a file.
In Unix however, the filesystem is more than that; it’s almost a way of life. In Unix, the
adagium is that “everything is a file”. We will discuss the filesystem below, how to walk
around in it, and why it’s useful to think about everything as a file.
27
Exercise 6.1 To get an idea of what the tree looks like, Linux has a nifty command called
tree. Try the following:
csp00~> tree -d /
Once you get tired of looking at the output, press Ctrl -C .
If all went well, you should have seen a very large list of directories. The most important
ones are shown in figure 6.1, with a description of what they contain.
The naming of directories and files is as follows:
Exercise 6.2 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 ../../../?
28
/
|-- bin Basic commands
|-- dev Device files
|-- etc Configuration files
| ‘-- rc.d Startup files
|-- home User directories
|-- lib System libraries
|-- lost+found Filesystem repair files
|-- opt Optional packages
|-- proc Kernel information
|-- root Administrator’s home directory
|-- sbin System commands
|-- tmp Temporary files
|-- usr User-level...
| |-- X11R6 X-Windows
| |-- bin Commands
| |-- doc Documentation
| |-- games Games
| |-- include C include files
| |-- info Additional documentation
| |-- lib Libraries
| |-- local Room for non-system commands and libraries
| |-- man Manual pages
| |-- sbin User system commands
| |-- share Shared files (text, graphics, etc.)
| ‘-- src C source files
‘-- var Storage room for...
|-- lib libraries
|-- lock locking files
|-- log log files
|-- run daemons
|-- spool printer, mail queues
‘-- tmp temporary files
29
6.2 Moving around
Above, you saw how the filesystem is organised. In order to store your files in subdirectories,
find commands etc. you need to be able to actually walk around in the filesystem tree.
Unix has a number of commands to do this.
The first may be the most important. Just as you used who am i to find out who you are,
you can use this command to find out where you are:
csp00~> 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 directory. It simply
takes as argument the place you want to go, e.g.:
csp00~> cd /usr/bin
or
csp00~> cd ..
Exercise 6.3 Use the cd command to verify the answers you gave to exercise 6.2.
• cd without any options will take you back to your home directory;
30
6.3 Looking around
To have a look at your directory, use the command ls (short for “list files”):
csp00~> 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:
csp00~> ls /bin
Now ls shows you which files are present in the directory /bin, rather than the directory
you’re currently in.
Exercise 6.5 Inspect the ls man page and try some of the options.
31
more called less2 , in which you can scroll forward using Space , back using w , jump to
the top using g and to the bottom using G . A useful facility of less is searching: type
/search-term and less will jump to the first occurence of search-term. Pressing n will
show you the next occurences.
Pressing h will give you an overview of all options of less.
6.5 Quota
As usually many people work on a single Unix system, the system administrator needs to
make sure no single person can abuse the system. One of the ways he can do this is by
using quota, or upper limits to the amount of disk space you can use.
To find out how much disk space is in use, you can use the df command (for disk free):
csp00~> df
The output should look like this:
Filesystem 1k-blocks Used Available Use% Mounted on
/dev/hda2 155609 85987 63196 58% /
You can see there are multiple file systems in use at the same time, covering different
directories. The first number shows the total number of 1024-byte blocks in the file system;
the next number shows how much is in use; and the last number shows how many blocks
may still be used.
However, this does not mean you can use all space left on the file system. To find out how
much room is left in your quota, type:
csp00~> quota
Note that on the CSP workstations, there are no quota, so you will get a message to that
effect. Usually, when you get close to your limit, you will receive messages from the system
warning you and giving you a couple of days to clean up some files.
A final useful command to find out how much disk space you’re actually using for which
files is du. Try:
csp00~> cd
csp00~> du
2
As in: less is more...
32
This will show you how many blocks you’re using in each subdirectory of the current
directory. Often the output of du -s * is more useful; this will summarise the usage for
all directories in the current directory.
33
Chapter 7
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.
7.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:
csp00~> ls -l /bin
total 6784
-rwxr-xr-x 1 root root 3192 Aug 14 13:31 arch
-rwxr-xr-x 1 root root 60592 Feb 3 2000 ash
-rwxr-xr-x 1 root root 263064 Feb 3 2000 ash.static
-rwxr-xr-x 1 root root 9968 Feb 3 2000 aumix-minimal
lrwxrwxrwx 1 root root 4 Aug 14 12:47 awk -> gawk
-rwxr-xr-x 1 root root 5756 Mar 7 2000 basename
-rwxr-xr-x 1 root root 310640 Nov 24 2000 bash
(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 6784 blocks, or roughly 6.8
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.
34
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.
7.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 7.1 List all files in /usr/bin starting with a q. Also list all files in /usr/bin
that contain the string text in their name.
7.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 2001,
news 02 01 2001 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
35
that there is actually one file with two names; if you throw away one of the names, you
throw away the other one as well. A soft link is less strict; you can remove the link without
changing the file itself. Soft links are probably the only ones you will be using.
In the directory listing of /usr/bin above, you can see that awk is a link to gawk. That
means that if you run the program awk, really the program gawk is run. This is shown in
the filename part (awk -> gawk).
You can create links using the ln command (for link).
• 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:
36
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.
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 7.3 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.
37
Exercise 7.4 Try this:
csp00~> 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
However, during this course you belong to just a single group.
ls -R : using ls with the -R (for recursive) option will show you all files in all subdirec-
tories of the directory you’re listing. This can quickly give a lot of information!
Exercise 7.5 Try this in your home directory:
csp00~> ls -R
What do you think ls -lR will do? Try it.
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 7.6 Use which and whereis to find out where the ls, which and whereis
commands are located.
38
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 7.7 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 permissions, 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.
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.
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.
39
• "^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.
• "[abc]" matches an 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 7.9 Use grep to find the following expressions in myfile.txt. In each case, try
to predict what will be the output:
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 10.1.
7.9.1 Copying
To copy files, you can use the command cp. It’s quite simple:
cp originalfile copiedfile
40
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 7.10 Copy your file myfile.txt to anotherfile.txt. Copy both files to /tmp.
Verify this using ls /tmp.
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.
7.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 7.11 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.
7.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!
Exercise 7.12 Use rm to remove the file you just moved, /tmp/renamedfile.txt. Verify
using ls /tmp.
41
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 to1 .
• -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...
Exercise 7.13 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 7.14 Create a backup directory and store copies of all text files in your home
directory there (ending in .txt).
We talked a lot about files, but you have so far only looked at files that already existed.
In the next section you will learn to create text files, using the editor that every Unix has
on board: vi.
1
A favourite example among system administrators is: rm -R /, which removes everything from the
file system.
42
Chapter 8
vi is the Unix text editor that most people love to hate. In the beginning, there was
only ed, a very user-unfriendly editor in which you could only work on one line at a time
(remember, these were the days of teletypes!). A follow-up to ed was ex, which already
improved things quite a bit. When people started using video displays, vi (short for visual
editor) was born, allowing people to work on an entire screen of text at the same time.
However, vi (vee-aai) still contains a lot of ex commands. Nowadays on Linux, a newer
version of vi is installed, which is called vim (vi improved). It still mostly behaves like
the original vi, though.
The big advantage of vi is that it’s installed almost anywhere. The disadvantages are that
it’s cryptic and that it has two modes (a text mode and a command mode), a mechanism
which nowadays is not used in other programs very often. Still, a lot of programs behave
like vi, so it’s useful to know at least something about it.
The basic modes and commands of vi are shown in figure 8.1. Keep this figure close in
the exercises below.
8.1 Editing
Let’s start by editing an already existing file:
csp00~> vi myfile.txt
You will see the contents of myfile.txt pop up, with the cursor in the top-left corner.
The bottom line contains some information: the filename and the number of lines (L) and
the number of characters (C) it contains.
The most important thing to learn about vi is that it has two modes:
• text mode, in which you can enter text and scroll around in the text;
43
command mode
vi commands : ex commands
Cursor: h j k l #G Add file: r
Scroll: [Ctrl−F] [Ctrl−B] Save: w
Status: [Ctrl−G] Redraw: [Ctrl−L] Exit if saved: q
Exit, no save: q!
Undo: u Repeat: . Search & replace:
Search: /... ?... n N [a]s/.../...
Search meta−characters:
^ Beginning of line \ Take next character literally
$ End of line [chars] Match any enclosed character
. Any character [^chars] Match any character not enclosed
Delete: x dw dd D Mark: m
Yank: yw yy Put: p P
Replace 1 char: r Join 2 lines: J
Replace mode: R Save & exit: ZZ
Insert... Delete/replace...
After... Before...
Char Line Char Line Char Word Line Line rest
a o i O s cw cc C a i c
[Esc]
text mode
• command mode, in which you can enter commands to remove and insert text,
search, save/load etc.
If you’re in text mode, press Esc to go to command mode. If you’re in command mode,
you will have to type a command such as i (insert text) to go back to text mode.
If you’re in doubt, always press Esc first to make sure you’re in command mode. In the
discussion below, we will always assume you are in command mode.
Exercise 8.1 You will now get to know some of the most often used vi commands by
“cleaning up” myfile.txt. First, some basics:
• Each time you press Ctrl -G , you get to see the status line, which can be quite
useful. Try it.
44
• You can move the cursor using the cursor keys or (for keyboards that lack cursor
keys) using h , j , k and l . Try this as well.
• H will bring your cursor to the top line, L to the bottom line.
• You can scroll backward using Ctrl -B and forward using Ctrl -F .
• To jump to a certain line, say line 20, type in 20G (in command mode, of course).
• The line “A history of Unix” is preceded by an empty line. Remove it using the
command dd, while your cursor is somewhere on that line.
• Remove two of the empty lines following “A history of Unix” by moving the
cursor to the first empty line and entering the command 2dd. In general, many
commands can be preceded by a number which says how often you would like
the command to be executed.
• Repeat the previous command, simply by pressing . .
• If all went well, you just removed too many lines. Don’t panic, there’s an option
for that: press u to undo the mistake.
• The comma after “An historical overview” should really be a full stop. If you
want to replace a single character, simply move the cursor over there and press
r followed by the new character. You will stay in command mode.
• On line 9, the word “devlopment” should be changed to “development”. To
insert the character “e” before the character “l”: move the cursor to “l”, enter
text mode by pressing i (for insert), type “e” and leave text mode again by
pressing Esc .
• A couple of lines below, remove the “-” between “user” and “friendly” by moving
the cursor there and pressing x . You will stay in command mode.
• The line following “1979” contains more than 80 characters, so it will not look
nice on a standard terminal. Make it look better by inserting Enter s and
pressing J (for join) in the right places.
Exercise 8.2
45
• Search for the line that contains the string “from B” using the command
/from B. This will jump to the first occurrence of the string “from B” after the
current cursor position (use ? instead of / to jump to the first occurrence of the
string before the current cursor position).
• The goal is now to add the string “the language” between “from” and “B”. In
order to do this, move the cursor to the space between “from” and “B” and
press a , for append. You will now enter text mode. Type in “the language ”
and go back to command mode by pressing Esc .
• In the line on 1979, add the following after “Unix”: “(version 7) ”.
• The second-to-last line contains an error, please add an “s” after “distribute”.
• Search for the word “Unix”. Press n to jump to the next occurence and N to
jump to the previous one. What happens when you reach the end of the file?
If you read the text, you might notice that the word PIPES is written in capital
letters in two places. It would look better as “pipes”, but changing everything by
hand would be annoying. It’s better to use search and replace.
• The command :s (note the :, which means “enter ex mode”) can be used for
searching and replacing text. If you simply type :s/PIPES/pipes, vi will search
“PIPES” and replace it by “pipes”, but only in the current line.
Luckily, you can prepend an address range for vi to work on. To specify the
entire text as range, use 1,$, where $ is a meta-character meaning “last line”.
The full search and replace command now becomes: :1,$s/PIPES/pipes. Try
this. Does it work?
• Replace all occurences of “Unix” by “UNIX”. What do you notice on the line
on 1970?
The string “Unix” is only replaced once on every line. To force vi to replace all
occurrences, there’s an extra option g (for global).
Press u to undo your last search and replace, and repeat it by entering:
:1,$s/Unix/UNIX/g.
46
• The simplest way to leave vi would be to enter ZZ, which means “write and
quit”. In ex-mode, you can do the same: just enter :wq (don’t do this yet!).
• If you would do this, you would overwrite the current file. If you want to prevent
your old file getting lost, you can also save the text you’ve edited to a new file.
Try this command: :w newfile.txt.
• Now you can safely leave vi using the command :q. If vi starts complaining
about unsaved changes, you can force the quit using :q!.
• Using address ranges, you can also save a part of the file. Edit myfile.txt with
vi and try: :1,$-10w myfile.part1.txt. What do you think it does? Verify
using less.
• The General Purpose Buffer (GPB): a single buffer, containing the most recently
deleted, changed or copied (yanked) text. The undo command u also uses the GPB.
• Temporary buffers, indicated by "1, "2, ... "9. These buffers contain the history of
the GPB; each time the GPB is renewed, its previous content is stored in "1, "1 is
shifted to "2 etc.
• Named buffers, which are operated on only by the user and whose contents are stored
throughout the editing session. If they are indicated by "a ... "z, their contents are
overwritten when you copy text into it; if indicated by "A ... "Z, the new text is
appended.
This may seem a bit complicated, but it’s not so bad. Remember that the General Purpose
Buffer (GPB) and temporary buffers store things automatically, useful for example for
retrieval of text you accidentally deleted. The named buffers can be used by you to work
on pieces of text.
Buffer names can be prepended to commands to tell vi to write text to or from buffers.
The three main commands are:
p or P: put (paste) text from a buffer after (p) or at (P) the current cursor position.
47
so that you can construct commands like these (don’t forget that all commands should be
followed by Enter ):
• 5yy copies 5 lines after the cursor position into the GPB.
• "a5yy copies 5 lines after the cursor position into named buffer "a.
• "q3dd cuts 3 lines before the cursor position into named buffer "q.
• p pastes the contents of the GPB after the current cursor position.
• "ap pastes the contents of named buffer "a after the current cursor position.
A final useful piece of knowledge is that you can label lines. In the examples above you had
to specify the number of lines to yank or delete, e.g. 5. However, often it’s easier to mark
a certain line with a label and then refer to that label. Labels are indicated by ’a ... ’z;
don’t confuse them with named buffers. Marking a line can be done using the command m
followed by a letter, e.g. mh marks a line with label ’h. You can then use these labels in
commands, for example:
• y’a copies lines from the cursor position up to the line marked by ’a into the GPB.
• "qd’a moves all lines from the cursor position up to the line marked by ’a into named
buffer "q.
Exercise 8.4 Using the commands above, you’re now ready to edit myfile2.txt:
48
use either :wq or :q!. As there is no such thing as a recycle bin in Unix, you can’t be too
careful!
Map commands can be stored in the file .exrc in your home directory. This file is read
each time vi is started. It’s an example of how you can customise programs.
A last helpful command is :help, which – at least in vim – shows you a very extensive
help text. Leave the help by entering :q.
These are some final exercises on myfile2.txt:
Exercise 8.5
• Replace all occurences of Unix to UNIX, but make use of the fact that the word
always starts with a u or U, always ends with an x or X and always contains 4
characters. Use the meta-characters for searching shown in figure 8.1. First try
to just search to make sure you have the right search string.
• By hand, correct the roughly 20 remaining mistakes in the text.
• Now write the file and leave vi.
emacs : a very extensive editor written and extendable in the LISP programming language.
Considered too bulky by some, you can also use it as a web-browser, file manager,
calculator etc.;
joe : a simpler editor which acts like the MS-DOS WordStar editor (and therefore like
TurboPascal and other DOS programs);
jed : “the programmer’s editor”, another extensive editor which can act like emacs, Word-
Star etc.;
mcedit : a clone of the MS-DOS Norton Editor, simple but useful, with menus;
pico : the editor that comes with the Pine mail program.
These are all text-based editors. Once you run a Graphical User Interface (GUI), there
are many more (user-friendly) editors to choose from. We will discuss these in the chapter
on X-Windows below.
49
Chapter 9
Now that you’ve learned how to create and manipulate files, it’s time to go a bit deeper
into one of the basic elements of Unix: redirection and pipes.
9.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:
csp00~> date > the date.txt
We will say the standard output of date has been redirected to the file the date.txt.
Exercise 9.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:
csp00~> 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
50
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).
Exercise 9.2 Try date -d "yesteryear" > date.txt and inspect the output. Now re-
move 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:
csp00~> less < myfile.txt
Exercise 9.3 Of course, you can use both standard input and output at the same time,
e.g.:
csp00~> 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.
Exercise 9.4 Write a small message using vi and store it in a file called mymsg.txt. Mail
it to yourself using the mail command (see page 21):
csp00~> mail c unx001 < mymsg.txt
Of course, replace c unx001 with your login name. After you’re done, remove
mymsg.txt.
9.2 Pipes
While redirection is useful, it has a serious limitation, as illustrated in figure 9.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:
csp00~> who > tmp.txt
csp00~> wc -l tmp.txt
csp00~> rm tmp.txt
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
51
Program 1 Program 2 Program 3
(a)
(b)
Figure 9.1: Connecting programs: (a) with redirection, (b) with pipes.
(symbol |) work in memory, so no intermediate files are created. Figure 9.1 (b) illustrates
this. The previous example becomes:
csp00~> who | wc -l
or, if you would like to store the number of people logged on,
csp00~> 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.
• cat /etc/passwd | cut -d":" -f1 | grep "^c unx" | wc -l counts the num-
ber of users whose name starts with c unx (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 al-
lows 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 "^c unx" | wc -l.
This line will append to an intermediate file called result.txt. What do you think this
file will contain? Verify.
Pipes are what makes Unix 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 6.4, but it’s a bit more versatile than you thought:
52
• you can supply a number of file names, which cat will show after another. For
example, try:
csp00~> 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:
csp00~> 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 .
• the cat command is often used as the beginning of a pipe, for example:
csp00~> cat /etc/passwd | grep "false" | sort | less
Try this line. What do you think it does?
• /dev/null: anything you write into this file is thrown away (it’s like a black hole);
• /dev/zero: when you read from this file, you will get an unlimited number of zero’s;
• /dev/random: when you read from this file, you will get random numbers.
Files influencing and reporting on the kernel are, in Linux, stored in /proc.
53
Chapter 10
The shell
In this section, some specific things about the shell will be discussed. Of course, you have
been using the shell all along this course, so you know the basics.
• If you specify arguments to commands, protect them using quotes. Protection 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:
csp00~> grep "${}" myfile.txt
and
csp00~> grep ´${}´ myfile.txt
(the $ metacharacter means “variable”; you will learn about variables 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 “backtick nota-
tion”, i.e. using the single backward quotes.
Exercise 10.1 For example, try this:
csp00~> wc -l ‘find . -name "*.txt"‘
What do you think this does? Try it out.
54
• If you want to run more than one command, but there is no need for pipes, use a
semi-colon. For example,
csp00~> cd
csp00~> ls
is the same as:
csp00~> cd; ls
• If you want to group two commands together, use parentheses. For example, try the
difference between:
csp00~> (cd /bin; pwd); ls
and
csp00~> cd /bin; pwd; ls
• Most shells feature command completion. In tcsh, ff 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 beep. In that case, you
can press Ctrl -D to find out which matches there are.
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 vi myfil to vi
myfile.txt.
10.2 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:
csp00~> history
You can now repeat lines by using the ! shell built-in command, like this:
You can even use ex-like substitution on the history lines. For example, say on line 23
you entered the command cp myfile.txt myfile1.txt, then !23:s/1/2/ will actually
perform cp myfile.txt myfile2.txt.
55
10.3 Different shells
The shell you have been using during this course is called tcsh. We already discussed you
can use other shells. The main difference between shells is the notation; most of them can
do more or less the same things.
Shells present on most Linux systems are:
• tcsh: the shell you have been using, an extended version of the C-shell, csh. The
tcsh and csh shells both have a notation which looks a lot like the C programming
language.
• bash: the Bourne again shell, an extended version of a very early shell, sh. In fact,
in Linux sh is just a link to bash.
• a number of shells derived from sh: the Korn shell (ksh), “a shell” (ash), the Z shell
(zsh), each with its own advantages.
You can try to run any of these by just calling the shell as a program:
If you like a particular shell very much, you can make it your default shell by calling chsh,
which works very similar to passwd. Don’t forget to enter the complete path to the shell,
e.g. /bin/bash.
Each shell has it’s own initialisation files. First, some system-wide initialisation files are
read from the /etc directory. These files can only be changed by the root user. For csh
and tcsh, the files are /etc/csh.login (which is read only once, when you log in) and
/etc/csh.cshrc (which is read each time you start a new shell). Have a look at these
files.
After the system-wide files, the shell executes your own specific initialisation files. These
are called .login and .cshrc and reside in your home directory. When you log out, a file
called .logout in your home directory will be executed, if it exists. Below, you will learn
how to change them to customise your environment.
56
You can create text files containing list 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 10.4 Using vi, create a text file l containing the following text:
ls -l
Now leave the editor and enter:
csp00~> sh l
This tells the command interpreter you are currently running, tcsh, to start another
interpreter (sh) and have it interpret the script l.
It can be done even easier. Re-open the file l in vi and type this in as the first line
of the file:
#!/bin/sh
Write the file to disk and leave vi. Now change the permissions on l so that you can
execute it (see section 7.4). Next, run l:
csp00~> l
Your tcsh now “understands” it will have to use /bin/sh to run the rest of l.
Now try this:
csp00~> l /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, l) is stored in $0.
ls -l $* | less
Now try again to use l to list the files in /usr/bin.
10.5 Paths
Exercise 10.6 Go to /usr/bin and try to run the l script you created in the previous
exercise. What happens?
57
The interpreter cannot find your script because the place it’s stored in is not in the path
variable. The shell uses many variables to control its behaviour (which will be discussed
in more detail in section 10.7). 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 10.7 Look at your path shell variable using the set command. Can you find a
directory in your path in which you are able to store your shell script l? Move l to
this directory, and try running it from /usr/bin.
Exercise 10.8 Have a look at your .cshrc and find out how you can add paths. Add a
path to ~/mybin. Leave the editor, create the directory ~/mybin and have the shell
re-read its .cshrc file using source .cshrc.
Now create a script cdl in ~/mybin which:
Exercise 10.9 A difficult question: why should .cshrc not contain a line #!/bin/sh ?
10.6 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 [args]
in which name is the short hand you want to give the command, optionally followed by
arguments.
To get an overview of all active aliases, you can just enter alias without arguments. To
get rid of aliases, use unalias.
Exercise 10.10 Create an alias for ls -la; call it lsl. Also, put this line in your .cshrc
file, so you can use it whenever you log on.
58
10.7 Shell and 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 compli-
cated way). One of the most useful things is that you can use variables. Actually, there
are two types of variables:
• shell variables, which are only visible in the current shell, and are mostly used to
change the shell’s behaviour;
• environment variables, which are accessible from any program or script called
from the shell.
Traditionally, shell variables are in lower case and environment variables in upper case.
Shell variables influence how the shell behaves. They are set by entering set
<variable>=<contents>, and you can view all variables by just entering set. A num-
ber of examples are (try them!):
• set autologout=5 means the shell will automatically log you out if you haven’t
typed anything for 5 minutes;
• set autolist=1 means that if you press Tab to complete a command or filename,
the shell will show you all possible endings instead of just beep;
• set correct=cmd will make the shell correct your input automatically. For example,
try entering mna ls after you’ve set correct to cmd;
• 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 environ-
ment variables by entering setenv <variable> <contents>. Some of the environment
variables are simply copies of corresponding shell variables, e.g. HOME and PATH. Others
contain useful information, such as USER, GROUP, PWD etc. (have a look at what these vari-
ables contain in your shell using env). Some environment variables, such as PRINTER and
EDITOR can be used to set your preferences (here, your favourite printer and editor) and
are used by many programs.
To use variables in your scripts, you can access their contents by putting a $ in front of
them. For example, try this:
csp00~> setenv MYNAME "A. User"
59
Now you can use the contents in the following way:
csp00~> 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 10.11 You can change your prompt using set prompt=<string>, where
<string> can contain a number of special characters. For example, try these:
What do you think the output created by the ! in the last prompt means (note that
\ is the escape character, to keep the shell from interpreting the !)?
For more options, see the man page of tcsh.
Exercise 10.12 Create a .plan file to reflect your plans. See whether it works using
finger.
60
Chapter 11
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 is usually just one
single processor in each workstation, 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.
61
processes (that is, you!) and the size in memory (SZ) in kilobytes. Also interesting is the
second field, S, which shows the status of the process: S for “stopped”, R for “running”
and so on. Note that “stopped” 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 -l, a useful one is -a, which shows you all
processes, also those of other users. Try this:
csp00~> ps -l
You will see a lot of processes that belong to the system. A very useful combination of
options is -edaf (look at the man page of ps to find out what these do):
csp00~> ps -edaf | less
The first process you should see in this list, the one with PID 1, is the “mother of all
processes”, 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 hierarchy of processes: each process (except
init) has a parent process and may have child processes.
To get a view of this hierarchy, try this:
csp00~> pstree
Have a look at what level the process pstree runs at.
Exercise 11.1 From your current shell, start another shell by entering tcsh. Do this
twice more. Use ps and pstree again to inspect the processes. Do they confirm
what you think happened?
Exercise 11.2 To start a process in the background, put an ampersand (&) behind the
command name. For example, try:
csp00~> du -s /usr/* >& du.log &
The output should be something like:
[1] 2350
which means that the shell has started the first background process ([1]) and it has
62
process number 2350. A grinding noise made by the hard disk should confirm the
machine is really working on your du command.
Remember that the >& du.log means that all output should be written to the file du.log
instead of the screen. This is important; if a process is running in the background and it
needs to write to the screen, it will simply stop until you pay attention to it.
Exercise 11.3 To find out whether your process is still running, use ps. You can however
also use jobs, which shows the current background processes:
csp00~> jobs
[1] + Running du -s /usr/* >& du.log
You can move the process back to the foreground using fg <job>. In this case, if
you enter fg 1, 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
1) or you want to run it further in the background (bg 1). Try suspending your
du-process and moving it to the background.
Remember: there can be only one process in the foreground at the same time!
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 current shell.
Exercise 11.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.
63
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>.
11.5 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:
csp00~> 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.
1
The name core dump comes from ancient history, when computer memory was still made out of small
metal cores which could be flipped in two ways to represent 0’s and 1’s.
64
Exercise 11.6 Start two long-running processes like this:
csp00~> du / >& /dev/null &
csp00~> nice +19 du / >& /dev/null &
Remember 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 both processes using killall du (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.
65
Chapter 12
X-Windows
If you work a lot in Unix, you will start feeling the need for working in several terminals
at the same time. You could edit a file in one terminal, compile in a second and browse
the web in a third. To this end, in the 1980s people at MIT put together a package
called X-Windows. In fact, X-Windows is more a set of rules and functions than an actual
application. It has been stable for quite a while; the last 5 years or so, it has been at
Release 1.1, Version 6 - or X11R6. Therefore, X-Windows is often called X11. Below we’ll
briefly discuss the X11 setup, have a look at a number of desktop environments and play
around with the GUI.
12.1 Introduction
What X-Windows does is more or less the same as any modern operating system. However,
following the Unix tradition, it does not offer a full graphical user interface (GUI) like, for
example, Windows 2000 does. All X11 does is allow programs to control regions on the
screen, draw in them or print text. It does this by sending commands to the X-Server, the
only program allowed to talk directly to the video card, mouse etc. Next, you’ll need a
toolkit, a set of functions you can call to draw things like buttons, borders, lists etc. Using
that toolkit, you can create a window manager, the program which allows you to open
and close windows, move them around etc. Finally, the toolkit can also be used to create
application programs, e.g. editors. Figure 12.1 shows the structure of this “wedding cake”
model of interfaces.
The different parts of the model have different properties:
• The X-Server needs to be programmed specifically for each video card, type of mouse,
monitor etc. It’s the only hardware dependent part of X. For PCs, the X-Server used
is a very good and freely obtainable one called XFree861 . Note how different this
1
See http://www.xfree86.org/.
66
Window manager &
applications
User tools
Toolkit
Shell OS
X11 Kernel
X−Server Hardware
Figure 12.1: X11 GUI setup. Note how the entire GUI to the OS is just a different shell.
is from OSes like Windows and MacOS, in which the GUI is interwoven with the
kernel;
• the X11 layer can abstract away from the hardware. This is very important: at this
level, all video cards, mice etc. can be talked to in the same way. This layer has not
changed much over the past few years. Another very important advantage is that the
language spoken between the X11 layer and higher layers can be transported over
the network. That means you can start a program on another machine and have the
window displayed on your screen. We’ll discuss this later.
• the toolkit gives a set of functions to actually build a user interface: buttons, slid-
ers, etc. Perhaps unfortunately, due to the fact that anybody can write a toolkit
if he wants to, there is a large number of toolkits: X Toolkit, Athena, XView, Mo-
tif/Lesstif, QT, GTK etc. Programs using different toolkits will have a different look
& feel, which sometimes makes it hard to get to know new programs;
• the window manager mostly decides on what windows look like. It usually offers
something called virtual screens, which means that your desktop size is much larger
than your actual screen size, so you can switch between different screens. This allows
you to keep a large number of windows open at the same time.
In conclusion, the setup is a little complicated, but also well thought-out and offers nu-
merous advantages.
67
12.2 Window managers
There are a number of window managers still in use, which all differ slightly in look & feel
and in the way they can be configured:
The more modern ones – CDE, KDE and Gnome – integrate a lot of functions; they
are really full desktop environments, and take care of web browsing, music and sounds,
applications talking to each other etc. In fact, all borrow a lot of ideas from the Mac and
Windows OSes: icons, start buttons, task bars etc. However, they also use a lot of memory
and CPU power, so they will usually not be present on older machines.
Exercise 12.1 Go to the graphical login screen by pressing Ctrl -Alt -F7 . Choose
Session -> Gnome and fill in your login name and password. Your screen should
now look like figure 12.2 (a).
Log out (look under the “footprint” button in the bottom left) and now login with a
KDE session. Your screen should now look like figure 12.2 (b).
Note that you can also close the entire X session by pressing Ctrl -Alt -Backspace ,
but it’s better to log out “officially” to prevent programs from crashing.
For the remainder of this section, you will stay in KDE. However, the basics should not
differ much from Gnome or CDE.
68
(a) (b)
(c) (d)
(e) (f)
Figure 12.2: X11 screenshots: (a) Gnome, (b) KDE, (c) KDE’s start button, (d) opening
consoles, (e) remote logins and (f) remote applications.
69
Desktop
Icons
Menu items
Exercise 12.2 Use the “K” start button to find Utilities -> Konsole. Start two
konsoles (KDE console applications) and re-create figure 12.2 (d): in the bottom
terminal the top command is running.
Change the font size and background color of either console.
Start xterm, the original terminal application, from one of the konsoles using
xterm &. Notice the difference in look & feel; in xterm, you can press your mouse’s
buttons while holding down Ctrl to change settings.
Play with the virtual screens: place a third konsole on screen 4.
70
Exercise 12.3 Login remotely on csp0X (hint: see section 3.4), where X is a number
between 0 and 2 and different from the machine you’re currently working on.
Now, try to run a simple X application, for example xclock or xcalc. What do you
notice?
The programs on csp00 need to know where to send their information. The environment
variable DISPLAY should contain the machine name followed by :0.0 (which means monitor
0, screen 0, but the actual numbers are hardly ever used).
The last thing you must do is allow other workstations to display stuff on your screen –
you wouldn’t normally want just any machine on the Internet being able to put windows
on your screen, right?
All this might seem complicated, but any reasonably smart system administrator will
automate this for you, so that you don’t have to type in all these commands. In that case,
you can just rlogin and start an application.
Exercise 12.6 A shorthand notation for the above is xon. On your local workstation, try
this:
csp0X~> xon csp0X xcalc &
Note that you will still need to allow display using xhost.
71
12.5 Applications
Although opening multiple terminals is very useful, of course X is more than just that.
There is a large number of applications. It would go too far to go in to all of them here;
the exercises below will show you just some of them. In section 13.2 you’ll find some more
applications which use X. Feel free to play around with them.
Exercise 12.7 Run Netscape by clicking it’s icon in the task bar. After you’ve browsed
the web, close the window. This will also stop the application.
Exercise 12.8 In the menu under the “K” button, find a more user-friendly editor than
vi (that shouldn’t be too hard!) and write down what you think of this course. Save
your text in a file called myopinion.txt in your home directory.
72
Chapter 13
You’re now ready to start working with Unix. However, it’s very likely you’re not done
learning. As you will work more with Unix, you will start feeling the need for some
more utilities. Usually, when you need something, odds are there is already some program
present which does more or less what you want. The trick is finding it – but of course, you
know how to use apropos...
Below, a short list is given of both useful commands and packages installed on most modern
Unix systems (especially Linux, which comes with an impressive array of programs). If you
don’t have these on your workstation, you might ask the system administrator to install
them (if he’s got the time, of course).
13.1 Commands
• tr translates certain characters in its input, for example echo "abcde" | tr "c"
"x" will output abxde;
• sed, the stream editor, can be used to “program” editing line-by-line on entire files.
For example, cat myfile.txt | sed -e "s/UNIX/Unix/g" will print myfile.txt
to the screen, replacing all occurences of UNIX by Unix.
• wget is a command-line tool to download files from the Internet (HTTP or FTP);
• awk is a full programming language which you can use to work on files;
• touch updates the access and modification times of a file. Useful when you want to
prevent files from being thrown away because they’re “too old”, or to create empty
files;
73
• strings shows all strings present in a program file;
• diff shows the difference between two text files. It can also be used to create a
file containing “just the differences” which you can then send to someone else. This
person can then “apply” the differences using patch;
• tar takes a number of files and puts them into one large file, for easy communication
or compression;
• gzip compresses a file to a file with the same name, but ending in .gz;
• lpr can be used to print PostScript files. Actually, your file will be placed in a queue;
use lpq to look at the queue and lprm to remove your job from the queue;
• a2ps converts ASCII text to PostScript for printing, psnup can be used to put mul-
tiple pages on one page;
13.2 Packages
• gcc is the GNU C compiler, which can run on nearly all operating systems available.
Front-ends for other languages are available: g++ for C++, g77 for Fortran, but also
translators: f2c for Fortran, p2c for Pascal;
• mc is a text-based file manager based on the Norton Commander for Microsoft DOS;
• mdir, mcd, mcopy, mdel etc. allow you to read from and write to Microsoft DOS
floppy disks;
74
• elm, mutt and pine are e-mail programs;
• pnmtools work on graphics files: conversion, stretching, color changes, etc. See man
pnm;
• xpdf (X11) allows you to view Acrobat PDF files; ghostview (X11) shows PostScript
files;
75
Chapter 14
Exercises
You can finish the course by selecting one of the exercises below and performing it.
After you’re done, make an appointment with the supervisor to discuss your results.
1. Write a script readnews that displays the current headlines on Dutch Teletekst:
Note that wget has a limited man page, but wget --help shows a lot of options.
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 your home directory:
76
– use grep to find the line containing your home 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
• place mydf in your ~/bin directory and make sure you can execute it from
anywhere on the file system
• use the precmd alias in tcsh to execute mydf every time you get a new prompt
(see man tcsh)
77