Você está na página 1de 77

Unix

Lab course manual


TN3561-P

Dick de Ridder & Tom Hoeksma


Contents

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

4 A look around the system 18


4.1 Information on other users . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.2 Writing & talking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4.3 E-mail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

5 First steps 23
5.1 Some basic commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
5.2 The manual pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

6 The file system 27


6.1 Layout and paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
6.2 Moving around . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
6.3 Looking around . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
6.4 Inspecting files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
6.5 Quota . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

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

8 Editing text files: vi 43


8.1 Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
8.2 Search and replace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
8.3 Save and exit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
8.4 Block operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
8.5 Putting it all together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
8.6 Other editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

9 Standard I/O and redirection 50


9.1 Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
9.2 Pipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
9.3 Everything is a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

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

13 Advanced commands and packages 73


13.1 Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
13.2 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

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.

1.1 What is Unix?


The Unix operating system is now more than 30 years old, but it is still used widely.
This is due to the fact that it’s very powerful, versatile and flexible. It runs on everything
from embedded systems (for example, in your dishwasher or DVD-player) to large, multi-
processor servers, and still forms the backbone of the Internet.
However, there no such thing as one Unix. To find out what Unix is, we will have to look
at the way it was created.

1.2 The Unix timeline


In the 1960s, computers couldn’t really talk to each other. Even computers by the same
manufacturer were often incompatible. Furthermore, the operating systems (programs that
let the user interact with the hardware) that ran on these machines often only did limited
things, such as reading and writing paper punch cards. All programming had to be done
in machine language, which is a tedious job.
Between 1965 and 1968, researchers at AT&T Bell Labs in the US were working on a
project together with MIT and General Electric. The goal was to create a real operating
system, with a good user interface, for a popular machine at that time, the GE 465. The
project was called MULTICS, which stands for “Multiplexed Information and Computing
Service”. A large number of people had been working on it for quite some time, but the

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

Figure 1.1: The basis of an operating system.

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.

1.3 The Unix philosophy


From the onset, Unix has been an operating system that has been put together by aca-
demics. As a consequence, a lot of thought has been put into what exactly an operating
system should do; and, on the downside, not much attention has been paid to ease of use.
Basically, any operating system consists of three layers, shown in figure 1.1. The bottom
layer is the kernel. This handles only basic functions, such as talking to devices (for
example the keyboard, the screen, the printer). On top of that, there is the shell. This
is the user interface, which should make it easier for humans to instruct the computer to
perform some tasks. Running above the shell are the user tools, a set of commands which
perform a specific function.
Different operating systems are usually the result of different ideas on how a user should
talk to the computer. For example, MS-DOS was really no more than a small kernel
with an extremely simple shell on top, with very few user tools. The Macintosh operating
system followed a different path: it tried to hide all the “gory details” of the computer
to the user. On an Apple machine, it used to be impossible to even type in commands;

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.

1.4 So, what is Unix?


Hopefully, the story above has given you the idea that there’s no such thing as the Unix.
In practice, Unix is a set of rules. All operating systems that follow that set of rules will
behave in more or less the same way. If you learn Unix on a PC running Linux, it should
not cost you much time to use Solaris on a Sun machine, IRIX on an SGI, HP-UX on an
HP, AIX on an IBM, etc. etc.

9
Chapter 2

Lab hardware

2.1 The network


The CSP lab contains a network which connects a number of PCs. Each PC is a Linux
workstation. One PC is designated a server; this machine makes it possible – among other
things – to log in to all other machines, connect to the Internet and print files to the
printer. Figure 2.1 shows the basic setup.
Traditionally, Unix networks consisted of a small number of very powerful servers and a
number of not-so-powerful workstations. In fact, in the very first Unix networks there
usually was just one server1 . Users connected to that server using dumb terminals, which
were little more than a keyboard and a text-based screen.
However, as computer power increased and got cheaper, it became feasible to buy com-
puters which could do much more and place one on each user’s desk. These workstations
initially just consisted of a keyboard, a black & white graphical screen and a small amount
of memory. They were made – and are still made – by companies like Sun, IBM, Apollo
(now Hewlett-Packard) and Silicon Graphics. However, nowadays workstation typically
have between 128 and 512 megabytes of memory, a high-resolution graphical screen and
10-30 gigabytes of hard disk. In fact, in some networks the workstations are now more
powerful than the servers.
If all this sounds like your typical desktop PC, that’s no coincidence. Due to the massive
competition in the PC market, PCs have grown much faster and have since (roughly) 1998
actually overtaken the Unix workstation market. Many companies and universities are
switching to PCs because they get more bang for their buck; and they can still run Unix.
However, there is still room for heavy Unix servers in some areas, such as high-performance
computing, finance, database management and web-hosting. You can buy (extremely
expensive) machines from the companies mentioned above with 4 to 32 processors, several
1
Something like an HP 1000, with a whopping 32 kilobytes of memory!

10
Internet

csp00
switch

Printer csp01 csp02

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.

2.3 The terminal


The basic element of any Unix system is still the terminal. Remember that in the 1960s
people had to talk to computers using paper punch cards. You would write your program,
then create a stack of cards with small gaps in it in certain places and feed them to the
computer. The computer would then do it’s job (if you didn’t make any errors) and spit
out a number of punch cards containing the answer. You would then have to translate the
gaps in the cards back to English.
Of course, this makes it pretty hard to use your computer interactively. Therefore, termi-
nals were invented. In the beginning, these were just keyboards. You could type one line
of commands and enter it into the computer. Again, the computer would do it’s job, and
spit out the answers on a line printer. Later, the line printer was replaced by a screen.
You type your commands, the computer calculates and prints the answer on the screen.
Nowadays, Unix still uses the idea of a terminal at its basis. However, we don’t need large
screen-and-keyboards any more. On a Linux system, you can open 6 virtual terminals by
     
pressing Ctrl -Alt -F1  through Ctrl -Alt -F6 . With one screen and keyboard, you can
already use 6 terminals. Even better, when you start using X-Windows, you can open an
unlimited number of windows each containing a terminal. All of these virtual terminals
still behave like they’re old-fashioned screen-and-keyboards; they emulate them.
This concludes the introduction to Unix. It’s time to really start doing something. So,
start up your workstation, choose “Linux” when the workstation asks which operating
system it should run, and wait until the virtual terminal appears.

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:

• when you last logged in, for example:


Last login: Sun Aug 19 14:22:26 from csp01

• 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, the message


You have new mail.
which means that there are one or more new mail messages waiting in your mailbox
(you will learn more about this later);

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

3.4 Remote logins


One of the most powerful features of Unix is that you can use the network to work on
any machine connected to it in almost exactly the same way you’re working on your own
machine. You can try this out for yourself:

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

A look around the system

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.

4.1 Information on other users


As Unix is a multi-user system, at any moment several people can be logged in to your
workstation (perhaps another student, practicing remote logins). To find out who is cur-
rently around, you can use the command
csp00~> who
It shows you who is logged in, on which terminal (tty, from the old teletypes used in the
1970s), and since when. For example:
c unx001 ttyp0 Aug 19 11:40
c unx005 ttyp2 Aug 19 13:02
root ttya Aug 12 01:30
You can learn quite a lot from this. For example, for this Unix student with login name
c unx001, apparently there’s a fellow Unix student currently logged in, c unx005.
Also, root has logged in more than a week ago1 . The root account is the only special user
account on any Unix system; beyond that, there is no built-in hierarchy. It belongs to the
system administrator, and it’s the only user which can do things like add or remove users,
add devices, upgrade the operating system etc. Also, root has the power to read and even
remove all your files, so it’s best not to write bad things about him or get into a fight2 .
1
In the middle of the night, so either he’s very hard-working, or there was some big crisis which forced
him to fix things.
2
For enlightenment on what can happen if your system administrator gets mad at you, read the BOFH
(Bastard Operator From Hell) series at http://bofh.ntk.net/.

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?

Finally, there’s a command to get detailed information on a specific user: finger.

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

4.2 Writing & talking


There are a number of Unix commands which allow you to interact with other users.
However, as you’re probably the only user logged in at the moment, we will have to “fake”
another user. You can do this by logging in again at a different virtual terminal (which
were discussed on page 12).

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

The simplest command is write. This sends messages to other users.

Exercise 4.4 Return to your first virtual terminal and type:


csp00~> write c unx001 ttyp1
replacing, of course, c unx001 by your login name and ttyp1 by the name of your
second terminal. The system will now beep and ask for input; just type a message
  
and press Enter . If you want to stop entering lines, press Ctrl -D .
Now go to the second terminal and see whether your message has arrived. Note how
it prints your name and terminal, so this feature sadly can’t be used to send fake
messages...

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 Some basic commands

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

Exercise 5.7 Find out more about the man command.

Two other useful manual commands are whatis and apropos. With whatis, you get
a one-line description of a command; the argument must match the 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

The file system

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.

6.1 Layout and paths


In the Unix filesystem, files are stored in directories. Directories (which are called
folders in Windows) can contain not only files, but also subdirectories. This is an
essential mechanism, as otherwise all files would reside in a single directory and things
would get very complicated. For example, it would not be possible to have two files with
the same name!
Due to the subdirectory mechanism, the Unix filesystem is hierarchical, like many other
currently used operating systems. You can think of it as a tree1 . The top of this tree is
called the root directory (not to be confused with the root user, who is the supervisor).
Each directory can contain files and one or more subdirectories.
1
Although this might seem like the only good way to organise a file system, there are people who think
differently. For example, some people build operating systems which completely hide from you where
things are stored. You just call a command and the system finds out where to find it. The Internet can
be thought of as such a filesystem; you just click on links, the computer knows where in the world the
information should come from.

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:

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

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

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

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


below the root directory called usr. This directory itself has a subdirectory called bin,
and in this directory there is a file called date. We call /usr/bin/date the path to the
command date; it’s the path you have to take through the tree to get to the sequence of
bytes called date.
The path to date is an example of an absolute path. No matter where you currently are in
the tree, you will first have to return to the root (/) and from there go to /usr/bin to find
date. It’s also possible to have a relative path. Say you are currently in the directory /usr,
then it’s not necessary to go all the way back to the root. Instead, you can simply specify
that date is in the bin subdirectory of the dierectory you’re in. The relative pathname
then would be bin/date. Note that there’s no / at the beginning of a relative path.
The relative path above only refered to a file “downward” in the tree, away from the root.
However, it’s also possible to refer to higher directories. The first directory above the one
you’re currently in is called the parent directory and can be referenced by ... For example,
say we are in /usr, then the path to the root directory could be written as ... If we’re in
/usr/bin, the path to the root directory is ../...
Note that we have introduced a directory we “are in” now. This is called the current
working directory.

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

Table 6.1: Some important directories in the Linux filesystem.

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.

Two final useful properties of cd are:

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

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

Exercise 6.4 Type:


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

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.

A very useful option, -l, will be discussed later.

6.4 Inspecting files


If you type in:
csp00~> ls
you will see there are a number of files in your home directory. You might be starting to
wonder what’s in them. To find out, you can use the cat command (for catalogue), e.g.:
csp00~> cat myfile.txt
As you can see, cat just spits out the contents of a file. If the file is longer than can fit on
your screen, you will not be able to see the first part.
You could have a look at just the first or last few lines of a file. Try:
csp00~> head myfile.txt
and
csp00~> tail myfile.txt
A better solution is to use a program called more:
csp00~> more myfile.txt
This may seem familiar from when you looked at man pages: more displays a screenful of
text at a time and shows the next screen each time. Unfortunately, you cannot scroll back
to read text that’s already off the screen. However, there is a more efficient version of

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.

Exercise 6.6 Try this:


csp00~> less myfile.txt
and search for PDP.

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

Files and directories

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

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:

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

ls * : list all files in the current directory;


ls my* : list all file names starting with my;
ls *.txt : list all file names ending with .txt (including the .);
ls */* : list all files in all directories.

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

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

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

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

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

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

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

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

Exercise 7.2 Try:


csp00~> cd
csp00~> ln -s /etc/passwd passwords
This will create a symbolic link in your home directory which points to the password
file in the directory /etc. You can now use this link as if it were the password file
itself:
csp00~> less passwords
After you’re done looking through the password file, remove the link:
csp00~> rm passwords
Fortunately, this will only remove the link, not the real password file...

7.4 File permissions


Next to the file type, ls -l shows the file permissions, the row of r’s, w’s and x’s. This list
really consists of three groups of three characters each. The first three characters contains
the permissions for the user, the owner of the file; the second three are for the group, the
group of the file; and the last three are for others, everybody else. The idea of a user group
will be explained below.
The three permission flags are:

• r: if this flag is present, the file can be read from;

• w: if this flag is present, the file can be written to;

• x: if this flag is present for a file, the file can be executed; if it’s present for a directory,
the directory contents can be read.

This might all sound a bit cryptic, so here are a couple of examples. These are the
permissions you will encounter most:

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.

7.5 User groups


As discussed above, you can assign permissions for the user of a file, the group of a file and
others. The output of ls -l shows the user and group to which the files belong. In the
listing of /usr/bin above, all files are owned by user root, who is in group root.
You might wonder what a group exactly is. Groups were made to make system admin-
istration easier. Say you run a company in which three project teams work on different
projects. You would like the members of each team to be able to read and write the team’s
files, but members of one team shouldn’t be able to read another team’s files. The group
mechanism makes this very easy, by putting users into three different groups and setting
the file permissions correctly.
In our lab environment, the main group is users. To see what groups you belong to, you
can (for example) use id:

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.

7.6 Hidden files


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

7.7 Finding files


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

ls -R : using ls with the -R (for recursive) option will show you all files in all 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.

Exercise 7.8 Try this in your home directory:


csp00~> 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.

7.8 Searching files


Besides looking for particular files, you may also want to find files containing particular
pieces of text. In this case, the command to use is grep. This command takes as parameters
a string to search for and one or more files:
csp00~> grep "19" myfile.txt
The search string can be just a piece of text – here it’s 19. It’s a good idea to put it between
quotes ("), otherwise the shell might interpret a space to mean it’s a new parameter.
However, you can also supply grep with a regular expression. This is an expression match-
ing various strings according to a number of rules. Regular expressions use so-called meta
characters:

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.

• "." matches any character.

• "[abc]" matches an a, b or c.

• "[0123456789]" or "[0-9]" matches any single digit number.

• "[^abc]" matches anything but an a, b or c.

• "[abc]?" matches zero or one occurences of any of the characters a, b or c.

• "[abc]*" matches zero or more occurences of any of the characters a, b or c.

• "[abc]+" matches one or more occurences of any of the characters a, b or c.

• "[abc]{n}" matches n occurences of any of the characters a, b or c.

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

Exercise 7.9 Use grep to find the following expressions in myfile.txt. In each case, try
to predict what will be the output:

• " [A-Z] "


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

Note that in the last sequence, the { and }-signs are “escaped” by putting a \ in front of
them; otherwise, the shell would interpret the { and } and just pass "U[a-z]3 " to grep.
More about this later, in section 10.1.

7.9 Copying, moving and removing files

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.

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


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

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 .

7.9.4 Common options


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

• -i: if specified, always ask for confirmation when overwriting or removing files;

• -f: if specified, never ask for confirmation when overwriting or removing files.

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

7.10 Making and removing directories


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

Exercise 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

Editing text files: vi

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

Figure 8.1: Basic vi commands and modes.

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

Now, it’s time to repair some mistakes in myfile.txt:

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

8.2 Search and replace


That took care of the most glaring errors in myfile.txt. Next, we’re going to make the
text a little easier on the eyes:

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.

8.3 Save and exit


Exercise 8.3 Finally, you’re going to add this piece of text:

After the privatisation of AT&T/Bell, UNIX becomes a commercial prod-


uct. This makes UNIX attractive to companies, so that the use of UNIX
also takes off outside universities.

Now it’s time to leave vi:

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.

8.4 Block operations


Often, you will want to operate on more than single words, e.g. entire paragraphs of text.
In this case, you would use block commands. Block commands operate on so-called buffers,
temporary storage places for pieces of text. There are three types of buffers in vi:

• 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:

y : yank (copy) text into a buffer (for lines: dd)

d : delete (cut) text into a buffer (for lines: dd).

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.

• P pastes the contents of the GPB at 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:

• Open myfile2.txt in vi.


• Swap the first and second paragraphs.
• The last 4 lines of the first part are in the wrong order: the first line should be
the last. Try to repair the paragraph using block commands and named buffers.
Don’t make any typo’s!
• If all went well, write the file back to disk so that if something goes wrong later,
you can just :q! and not lose all your work.

8.5 Putting it all together


A useful ex command is :map, which allows you to map certain keys to others. You might
want to consider this mapping: :map Z :, which means that whenever you type a Z (in
vi) you enter ex instead. This means you can no longer use ZZ to leave vi, forcing you to

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.

8.6 Other editors


Besides vi, many modern Unix systems (especially Linux) contain a large number of other
editors. This is a short overview; try some of them to see whether one suits you better
than 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

Standard I/O and redirection

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

Input file Temp file Temp file Output file

(a)

Input file Program 1 Program 2 Program 3 Output file

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

• ls -l /usr/bin | wc -l counts the number of files in /usr/bin.

• 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?

9.3 Everything is a file


We discussed earlier how in Unix “everything is a file” (see section 6). Hopefully, by now
you have an idea of why this is useful. Using pipes and redirection you can access a lot
of devices just by putting text into a file representing the device, or reading from that
file. The Unix kernel settings can also be accessed as files. As a result, you can use the
command line to do very powerful things.
Device files are stored in /dev in the Unix filesystem. Have a look at that directory (e.g.
using ls -la /dev | less). You will see device files corresponding to the screen (fb*,
tty*), to floppy disks (fd*), to hard disks (hd*), the printer (lp*), SCSI disks (sd*) etc.
You could, for example, if you were system administrator just cat something to /dev/fd0
to write it to the floppy disk.
Some special device files are:

• /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.

Exercise 9.5 Have a look at /proc/cpuinfo. What do you think it means?

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.

10.1 The command line interface


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

• If you specify arguments to commands, protect them using quotes. 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:

• !! will repeat the last line.


• !13 will repeat line 13.
• !-5 will repeat the line you entered 5 lines ago.
• !mo will repeat the last command you entered that started with the string mo.

Exercise 10.2 Play around with the history commands.

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:

Exercise 10.3 Try bash. Leave the shell using exit.

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.

10.4 Shell scripting


Although the shell looks like a place where you can just type in commands, it’s actu-
ally much more than that. Besides being a command line interface, it’s also a command
language interpreter. This means that you can glue together commands using simple pro-
gramming language constructs and work on variables.

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.

Exercise 10.5 Re-write your shell script l to contain the following:


#!/bin/sh

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:

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


• show a long list of files and pipes the output through less.

Try running your script from a different directory.

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;

• home is the shell variable which contains your home directory;

• path contains a list of directories the shell will search for programs;

• term shows you what terminal type you are working on.

You can get a list of environment variables by entering env, and create or change 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.

10.8 The prompt


A very useful shell variable determines what the prompt looks like. Remember, the prompt
is the piece of text the shell prints every time it waits for your input, e.g.:
csp00~>
Here, csp00 is the machine you are working on; ~ is the current directory (remember that
~ is short-hand for your home directory), and > means “start typing here”.

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:

• set prompt="%d %P: "


• set prompt="%D-%Y-%W %t > "
• set prompt="Hello, %n... what can I do for you? "
• set prompt="%m [\!]> "

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.

10.9 User information


Remember the finger command discussed in section 4.1? The information it shows is
stored in a file called .plan in your home directory.

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.

11.1 Inspecting processes


The most useful command to inspect running processes is ps, for process status. Try this:
csp00~> ps
The output should look like this:
PID TTY TIME CMD
1736 tty1 00:00:00 tcsh
2211 tty1 00:00:00 ps
There are two processes in your current shell: tcsh (the shell you are currently running)
and ps itself. The first column of the output shows the process identification number (PID).
Each process has a unique number. The next column shows in which terminal this process
is running; in this case it’s terminal 1. Next, ps shows how much time has been used by
the process so far. As usually these commands run very quickly, here it’s 00:00:00, or less
than 0.5 seconds. Finally, ps shows the command name of the process.
To get more information, specify the option -l to ps. Try this. You will get a lot of
(cryptic) information, such as the UID (user identification number) of the owner of the

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?

11.2 Foreground and background


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

Exercise 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!

11.3 Killing processes


Now you know how you can move processes to the background and the foreground, you
might wonder how you can stop processes. The simplest solution, when the process is
 
running in the foreground, is to press the “interrupt” key, Ctrl -C .
 
Exercise 11.4 Start the process du -s /usr/* >>& du.log and stop it using Ctrl -C .

Another option is to use kill. You can use kill in two ways:

• kill <PID> will stop the process with the specified process identification number
PID;

• kill %<job> will stop the process running as job number job in the 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.4 Core dumps


When a program crashes (which can happen even in Unix), it tends to leave behind a core
dump1 . Such a core dump, stored in a file called core, contains all the memory allocated
to the process at the time it crashed. This file can then be used – but only by Unix gurus!
– to find out what went wrong. For normal users, these dumps just take up a lot of disk
space; so, throw them away. Or, better yet, prevent them from being made by:
csp00~> limit coredumpsize 0
(tcsh only).

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:

• very old and basic ones: twm, ctwm;


• OpenWindows, available on Sun and Linux machines only;
• fvwm, fvwm2, fvwm95 (which looks like Windows 95);
• windowmaker, which looks like the NeXT GUI did;
• mwm, based on Motif and only commercially available, mostly on SGI machines;
• CDE, a successor to mwm which works on Sun, SGI and HP workstations;
• KDE, a mostly Linux-based system;
• Gnome, another Linux-based system.

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.

12.3 Xterms and other consoles


One of the most useful things of X is that you can work in multiple terminals at the same
time. The most basic application which gives you a terminal is called xterm. However,
KDE and Gnome have more advanced versions in which you can easily change the font,
color etc. An example is shown in figure 12.2 (d).

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

"Start button" Virtual screens Console Netscape Task bar

Figure 12.3: KDE desktop elements.

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.

12.4 Remote programs


Figures 12.2 (e) and (f) show how you can start a program on a different workstation but
have its output displayed on your machine.

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

Exercise 12.4 On csp00, enter:


csp00~> setenv DISPLAY csp0Y:0.0
where Y should be replaced by the number of the workstation you’re currently working
on (hint: use hostname on your local workstation to find out it’s name).
Again, try to run xclock or xcalc. What do you see now?

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?

Exercise 12.5 On your local workstation, type:


csp0X~> xhost + csp0Y
This means: allow (+) one remote workstation (csp0Y) to display applications on my
screen.
Run xclock or xcalc. This time it should work, and your screen should look like
figure 12.2 (f).

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

Advanced commands and packages

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;

• bc is an arbitrary precision calculator;

• 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;

• od shows the contents of a file as ASCII or (hexa)decimal values;

• dd can be used to copy parts of binary and text files;

• locate uses a database to find files in the filesystem, similar to find;

• 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;

• cmp also compares files, but binary files (e.g. programs);

• 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;

• zip creates Windows-compatible PKZIP compressed file archives;

• ispell is a spelling checker;

• 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;

• ftp is an FTP client for downloading files from the Internet;

• telnet allows you to connect to other machines to work there;

• ssh does the same, but in a more secure way.

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;

• ddd (X11) is a good graphical program debugger;

• 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;

• tin, rtin, trn, slrn are for reading USENET newsgroups;

• irc allows you to chat on IRC channels;

• ncftp is a very useful extension of ftp;

• netscape (X11) is a graphical web browser; lynx is a text-based one;

• cdplay and other programs play audio CDs;

• mpg123 plays MP3 files;

• latex is a scientific word processor;

• 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;

• xpaint (X11) can be used to draw bitmap images;

• gimp (X11) is an extensive PhotoShop-like program;

• xfig (X11) for drawing vector graphics;

• xv (X11) is used to view images and capture screen dumps;

• ical (X11) is a calendar program.

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:

• use wget to download page 101 from


http://teletekst.nos.nl/cgi-bin/tt/nos/page/t/101
• use tr to replace occurences of ">" by "\012" (a carriage return)
• use grep to only retain lines containing "HREF"
• use tr to replace occurences of "<" by "\012" (a carriage return)
• Now remove all lines containing "HREF" using grep
• think of other things you might do to clean up the output

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:

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


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

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

• create a script mydf:


– call df

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)

4. Write a script killme that kills all your processes:

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


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

77

Você também pode gostar