77
Unix Lab course manual TN3561-P Dick de Ridder & Tom Hoeksma

Unix Lab Course Manual

Embed Size (px)

Citation preview

Page 1: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 1/77

UnixLab course manual

TN3561-P

Dick de Ridder & Tom Hoeksma

Page 2: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 2/77

Contents

1 Introduction 61.1 What is Unix? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61.2 The Unix timeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

1.3 The Unix philosophy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81.4 So, what is Unix? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

2 Lab hardware 102.1 The network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102.2 Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112.3 The terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

3 Logging in 133.1 Logging in . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

3.2 Entering commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153.3 Logging out . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163.4 Remote logins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163.5 Passwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

4 A look around the system 184.1 Information on other users . . . . . . . . . . . . . . . . . . . . . . . . . . . 184.2 Writing & talking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194.3 E-mail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

5 First steps 23

5.1 Some basic commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235.2 The manual pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

6 The file system 276.1 Layout and paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276.2 Moving around . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306.3 Looking around . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316.4 Inspecting files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316.5 Quota . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

2

Page 3: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 3/77

7 Files and directories 347.1 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347.2 Wildcards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357.3 Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357.4 File permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367.5 User groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377.6 Hidden files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387.7 Finding files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387.8 Searching files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397.9 Copying, moving and removing files . . . . . . . . . . . . . . . . . . . . . . 407.10 Making and removing directories . . . . . . . . . . . . . . . . . . . . . . . 42

8 Editing text files: vi 43

8.1 Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438.2 Search and replace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458.3 Save and exit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468.4 Block operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478.5 Putting it all together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488.6 Other editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

9 Standard I/O and redirection 509.1 Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509.2 Pipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519.3 Everything is a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

10 The shell 5410.1 The command line interface . . . . . . . . . . . . . . . . . . . . . . . . . . 5410.2 History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5510.3 Different shells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5610.4 S hell scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5610.5 Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5710.6 Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5810.7 Shell and environment variables . . . . . . . . . . . . . . . . . . . . . . . . 5910.8 The prompt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60

10.9 User information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60

11 Processes 6111.1 I nspecting processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6111.2 Foreground and background . . . . . . . . . . . . . . . . . . . . . . . . . . 6211.3 Killing processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6311.4 Core dumps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6411.5 Nice! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64

Page 4: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 4/77

12 X-Windows 6612.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6612.2 Window managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6812.3 Xterms and other consoles . . . . . . . . . . . . . . . . . . . . . . . . . . . 6812.4 R emote programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7012.5 Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

13 Advanced commands and packages 7313.1 Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7313.2 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

14 Exercises 76

4

Page 5: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 5/77

Foreword

This course has been written to be completely self-contained. It mixes pieces of text andtheory with examples and exercises you should perform yourself. Please read the textcarefully (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’snot necessary to write a full report.

Although in principle most of the text and exercises should be applicable to any Linuxdistribution, discussion of the network setup and applications is geared specifically to anumber 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 thebasis for the grade you receive.

Some notation: anything you should see on your screen is shown in Courier. All commandsin Unix are case sensitive, which means that there is a difference between a small “a” anda capital “A”. This also holds for file names. Anything you should type in is shown inCourier too, but it’s underlined as well. Keys you should press are typed in boxes, forexample

£

¢

   

   ¡Enter .

5

Page 6: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 6/77

Chapter 1

Introduction

This first section contains an overview of what Unix is. You’re supposed to read it beforeyou start doing anything with Unix, because it will give you an idea of why Unix behaveslike 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 lookat 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 thatlet the user interact with the hardware) that ran on these machines often only did limitedthings, such as reading and writing paper punch cards. All programming had to be donein machine language, which is a tedious job.

Between 1965 and 1968, researchers at AT&T Bell Labs in the US were working on aproject together with MIT and General Electric. The goal was to create a real operatingsystem, with a good user interface, for a popular machine at that time, the GE 465. Theproject was called MULTICS, which stands for “Multiplexed Information and ComputingService”. A large number of people had been working on it for quite some time, but the

6

Page 7: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 7/77

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 runMULTICS, if only because it had just 4 kilobytes of memory1. So, they put together a verysimple 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; amechanism for starting and stopping programs; and a mechanism for the programs totalk to the shell. The researchers called their toy operating system Unix, as a pun onMULTICS.

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, andafter 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 youhad to do was to build an assembler for the C-compiler, re-compile Unix and you had aworking 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 theresearchers could repair bugs3, change programs or even add functions to the operatingsystem themselves. They would send back these changes, and over the years Unix quicklygrew to contain a large set of commands. One especially important branch of  Unix wasmade at Berkeley University in California; they put in the important TCP/IP networkingcode, 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. Thenetworking 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 thismeant 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 startedcourt 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 originalUnix source code anymore. Still, these operating systems (such as QNX, MINIX andLinux) 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, OSFand later the Open Group. They decided to put together a number of specifications of 

1A kilobyte contains 1024 bytes (characters), a megabyte contains 1024 kilobytes, a gigabyte contains1024 megabytes, a terabyte... you get the picture.

2This also explains why many of the commands have such silly names as ls instead of  listfiles, orfsck instead of  filesystemcheck; this was done to save memory in the early days.

3Slang for small (or large) errors in a program, which programmers used to say was due to bugs crawlingaround in the computer (see http://www.tuxedo.org/~esr/jargon/).

7

Page 8: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 8/77

Hardware

Kernel

User tools

Shell OS

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 allUnix-like operating systems should follow so that they react in the same way. Althoughwe’re not quite there yet, today it’s reasonably simple to compile the same program onSGI, 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 operatingsystem 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 bottomlayer 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 . Thisis the user interface, which should make it easier for humans to instruct the computer toperform some tasks. Running above the shell are the user tools, a set of commands whichperform a specific function.

Different operating systems are usually the result of different ideas on how a user shouldtalk to the computer. For example, MS-DOS was really no more than a small kernelwith an extremely simple shell on top, with very few user tools. The Macintosh operatingsystem followed a different path: it tried to hide all the “gory details” of the computerto the user. On an Apple machine, it used to be impossible to even type in commands;

8

Page 9: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 9/77

everything had to be done through the graphical user interface shell (GUI). The Windowsseries of operating systems has included more and more of the Macintosh ideas, to sucha point that it is now nearly impossible to run programs on a Windows machine withouthaving to use the graphical user interface shell.

In contrast, a basic element of  Unix has always been to create parts of the operatingsystem 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 verypowerful for advanced users, offers no graphical user interface. If you want one, you canrun 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 thateach tool is a black box to the user – he doesn’t have to understand how it works – soyou’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. Eventhe graphical user interface does nothing more than draw things on the screen; if you wantwindows, you can run any of a number of  window managers (which, in turn, just drawwindows).

Finally, you need a way to make the boxes talk to each other. In Unix, this is done usingtext 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 bediscussed 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 willbehave in more or less the same way. If you learn Unix on a PC running Linux, it shouldnot cost you much time to use Solaris on a Sun machine, IRIX on an SGI, HP-UX on anHP, AIX on an IBM, etc. etc.

9

Page 10: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 10/77

Chapter 2

Lab hardware

2.1 The network

The CSP lab contains a network which connects a number of PCs. Each PC is a Linuxworkstation . One PC is designated a server ; this machine makes it possible – among otherthings – to log in to all other machines, connect to the Internet and print files to theprinter. Figure 2.1 shows the basic setup.

Traditionally, Unix networks consisted of a small number of very powerful servers and anumber 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, whichwere 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 workstationsinitially just consisted of a keyboard, a black & white graphical screen and a small amountof memory. They were made – and are still made – by companies like Sun, IBM, Apollo(now Hewlett-Packard) and Silicon Graphics. However, nowadays workstation typicallyhave between 128 and 512 megabytes of memory, a high-resolution graphical screen and10-30 gigabytes of hard disk. In fact, in some networks the workstations are now morepowerful than the servers.

If all this sounds like your typical desktop PC, that’s no coincidence. Due to the massivecompetition in the PC market, PCs have grown much faster and have since (roughly) 1998actually overtaken the Unix workstation market. Many companies and universities areswitching 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-performancecomputing, finance, database management and web-hosting. You can buy (extremelyexpensive) machines from the companies mentioned above with 4 to 32 processors, several

1Something like an HP 1000, with a whopping 32 kilobytes of memory!

10

Page 11: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 11/77

Internet

csp01 csp02

csp00

Printer

switch

Figure 2.1: The CSP lab network. Note how workstations csp01 and csp02 talk to eachother 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 yetfor 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 megabyteof 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 asmall computer project which eventually grew into Linux, a full-fledged Unix-like operatingsystem for the PC which you have probably heard of 2. In the beginning, you couldn’t dovery much with Linux, because it’s only a kernel , which is the bit of the operating systemwhich 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 FreeSoftware Foundation (FSF), the GNU tools3, Linux really took off.

2In fact, in his design Torvalds was influenced a great deal by MINIX, a Unix for the PC developedunder supervision of Andrew Tanenbaum, a famous professor of computer science based in Amsterdam.

3GNU is a recursive acronym: it stands for GNU’s Not Unix. The “movement” was started in the1980s by Richard Stallman, a computer scientist at MIT who feels that all software should be free (freeas in speech, not as in beer). His goal was to create a Unix-like operating system based entirely onsoftware everybody could freely distribute. The only thing he really lacked was a kernel , which is thething that Linus Torvalds wrote; and since Linux is free software too, his dream came true. Ironically, thewhole system is now known as Linux instead of GNU, so every now and then he complains that Linuxdistributions should be called GNU/Linux. See http://www.gnu.org/.

11

Page 12: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 12/77

At this moment, Linux is the fastest growing operating system for server PCs, also thanksto the large set of free software you get with every distribution, such as the excellent Apacheweb-server. The only thing still lacking is a good, unified user interface such as the one theWindows 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 1960speople 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 thecomputer. 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 thegaps 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 lineof commands and enter it into the computer. Again, the computer would do it’s job, andspit 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 largescreen-and-keyboards any more. On a Linux system, you can open 6 virtual terminals bypressing

£

¢

   

   ¡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 anunlimited number of windows each containing a terminal. All of these virtual terminalsstill 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 operatingsystem it should run, and wait until the virtual terminal appears.

Note: if, after you have started your workstation, you see a graphical loginscreen, press

£

¢

   

   ¡Ctrl -

£

¢

   

   ¡Alt -

£

¢

   

   ¡F1 first to go to a virtual terminal.

To shut down the workstation after you have finished, go back to thegraphical login screen by pressing

£

¢

   

   ¡Ctrl -

£

¢

   

   ¡Alt -

£

¢

   

   ¡F7 . Choose the System menu

and then Halt. Wait until the screen says Power down. Only then switch theworkstation off by pressing the power button on the front.

12

Page 13: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 13/77

Chapter 3

Logging in

An important element of  Unix is that it’s a multi-user operating system. That means thatseveral people can work on it, not just sequentially as in Windows 2000, but at the sametime. Therefore, you will have to tell the computer who you are when you start. This iscalled 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’srunning 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 theremainder 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 operatingsystem 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

Page 14: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 14/77

will now ask you for your password to prove that you are who you claim to be (notethat it will not show what you type, to prevent people looking over you shouldersfrom 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 workingin, and the > sign to indicate where you can start typing1.

1Other 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

Page 15: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 15/77

3.2 Entering commands

As you already know, the shell program you’re now working in is meant to be a layerbetween you and the kernel. You type in commands, the shell figures out which userprogram(s) to run and instructs the kernel to do so. In most of the exercises below, you’llbe 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 lineusing

£

¢

   

   ¡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 onyour 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!), youshould 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!

2Unlike Windows, Unix usually treats£

¢

   

   ¡Backspace and

£

¢

   

   ¡Delete as the same thing, removing the

character left of the cursor.

15

Page 16: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 16/77

3.3 Logging out

Before you do anything else, it’s good to know how you can leave the system. If you goaway, you don’t want to leave your computer in the state it is now, because anybody cancome in and look at your files, messing with them or even change your password! Loggingout 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 loginsOne of the most powerful features of  Unix is that you can use the network to work onany machine connected to it in almost exactly the same way you’re working on your ownmachine. You can try this out for yourself:

Exercise 3.3 Type in: rlogin csp0X, where X is a number between 0 and 2, and differentfrom that of the machine you’re working on. The computer will first assume yourlogin name is the same as the one you’re using now, so it will only ask for yourpassword; 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 adifferent machine! If you don’t get a login prompt, the machine might not be runningLinux 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. Thereare some basic mechanisms for making sure that people working on your system cannot dothis, which will be discussed later. On top of that, you certainly don’t want unauthorisedaccess to your workstation: hence the need for passwords.

Passwords can be changed using the passwd program4. First, it’s good to think aboutwhat your password should be.

3Often, 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 .

4On some networked systems, you might have to use yppasswd. Your system administrator will tellyou which program you should use.

16

Page 17: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 17/77

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 amountof 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 shouldpreferably include some non-alphabetic characters, for example !, # or $. A standard wayof creating a difficult password is to think of a sentence only you know and then take thefirst 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 someletters by non-alphabetic equivalents, so you get T=1vspw!.

Exercise 3.4 Execute the passwd command. First, the program will ask you for your oldpassword to make sure it’s you. Then, it will ask you for the new password. Usethe rule above to create a hard-to-crack password. Again, you will have to enter itblindly (it will not echo the password), so be careful. You have to enter the passwordonce 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 systemadministrators often have better things to do.

Exercise 3.5 Log out and try to log in with a non-existing name, e.g roodkapje. Thesystem of course knows there’s no user roodkapje, but will still ask for a password.Why do you think this is?

17

Page 18: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 18/77

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 tohave 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 yourworkstation (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 the1970s), 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 namec 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 useraccount on any Unix system; beyond that, there is no built-in hierarchy. It belongs to thesystem 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 evenremove all your files, so it’s best not to write bad things about him or get into a fight2.

1In the middle of the night, so either he’s very hard-working, or there was some big crisis which forcedhim to fix things.

2For 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

Page 19: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 19/77

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 planis. 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 (whichwere 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 thesecond 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 yoursecond terminal. The system will now beep and ask for input; just type a messageand 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 howit prints your name and terminal, so this feature sadly can’t be used to send fakemessages...

19

Page 20: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 20/77

Note that writing is a bit intrusive, as the message will be printed on the other user’sscreen without warning and will overwrite anything that’s there. Fortunately, there’s norisk that the text you enter will actually be used as input for a program, but still it canbe 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 itfrom 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 someonewho wants to talk to him. He will then have to respond by entering talk c [email protected], you will see a split screen in which you can type in the top half, and the other usercan type in the bottom half.

Note: the talk command currently does not work on csp00. If you areworking 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 andreturn 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

Page 21: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 21/77

4.3 E-mail

As was already discussed in section 3.1, each user has a mailbox. Anyone on the systemcan send e-mail messages to you, which will be stored in this box (the box itself is a file ina 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 manymessages you have and how many still are unread. Below, for each message you willsee a line showing whether the message is new (N), unread (U, which means thatyou’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 entercommands for the mail program itself. You can get a list of possible command byentering ? at the prompt.

In mail, one of the messages is the active message, marked with a >. To read theactive message, enter t; to read another message, enter t n, where n is the messagenumber. 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 messageis logged on. You’ll be automatically returned to mail after the commandcompletes.

• Store message 1 in a file called mymail, using the command s 1 mymail. Thenleave 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 theprogram 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 edittext files. We will get back to this later.

21

Page 22: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 22/77

As said before, mail is the simplest command that allows you to read your e-mail. Thereare 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 cursorkeys 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

Page 23: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 23/77

Chapter 5

First steps

Now that you know how to type in commands and read mail when it comes in, it’s time tolearn some basic commands. As said before, Unix contains many small, simple commandsthat 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 whichprogram 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’sCentral 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 thisoption to tell date not to report what time it is today, but also in the future and thepast. 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 and3 days, and 17 days ago.

23

Page 24: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 24/77

This example immediately shows you that what seems like an ordinary little command canactually be quite powerful. Each program usually has a number of arguments (characters ornumbers following the program name, separated by spaces) which you can use to instructthe program to do something else than it does normally (by default ). Arguments canbe options like -d above, followed by the actual value for the option, or switches, singlearguments 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, thecal 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 themonth 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 followedby the year.

Exercise 5.4 What would happen if you would give the year first, then the month? Trythis: cal 2001 12.

24

Page 25: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 25/77

5.2 The manual pages

You’ve already encountered one way of finding out what arguments a command takes: byspecifying --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, whichyou can call up with the command man.

Exercise 5.5 Use the man command to find out what the cal command does and whatarguments 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 thisinformation 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 detaileddescription of what it is, when the command was made and by whom, where to find moreinformation or related commands, and the copyright. Sometimes, the man page is sobrutally 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 resultof a large number of programmers working together. For the tcsh, at the bottom of theBUGS section, you can even find out where to send an e-mail if you think you’ve find a newbug. The honesty and opennes of man pages is one of the results of  Unix being developedmainly by academics instead of commercial organisations.

25

Page 26: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 26/77

Note that in the cal man page, the top line says CAL(1). This means that cal is found inSection 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 foundautomatically 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 lookingfor.

Manual pages are among the most important things you need to learn about Unix. Fromnow on, as little explicit help on commands will be given as possible; you know where tofind 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 geta one-line description of a command; the argument must match the command exactly.The apropos command also shows descriptions of commands which match your argumentpartially, which makes it easy to search for a command of which you don’t exactly knowthe 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 onnew commands, it is time to learn something about the basis of  Unix, the file system .

26

Page 27: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 27/77

Chapter 6

The file system

A file system usually is a way in which the computer organises commands and data on thehard disk. The commands you have tried above all reside on a hard disk somewhere, sowhen you type in (for example) date, the computer will have to know where to find thebytes that belong to date, how many there are and how to read them into memory. Sucha sequence of bytes, together with information like it’s name, location on the hard diskand size, is called a file.

In Unix however, the filesystem is more than that; it’s almost a way of life. In Unix, theadagium is that “everything is a file”. We will discuss the filesystem below, how to walkaround 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 anessential mechanism, as otherwise all files would reside in a single directory and thingswould get very complicated. For example, it would not be possible to have two files withthe same name!

Due to the subdirectory mechanism, the Unix filesystem is hierarchical, like many othercurrently used operating systems. You can think of it as a tree1. The top of this tree iscalled 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.

1Although this might seem like the only good way to organise a file system, there are people who thinkdifferently. For example, some people build operating systems which completely hide from you wherethings are stored. You just call a command and the system finds out where to find it. The Internet canbe thought of as such a filesystem; you just click on links, the computer knows where in the world theinformation should come from.

27

Page 28: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 28/77

Exercise 6.1 To get an idea of what the tree looks like, Linux has a nifty command calledtree. 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 importantones 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 subdirectorybelow 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 thecommand 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 inthe tree, you will first have to return to the root ( /) and from there go to /usr/bin to finddate. 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 specifythat date is in the bin subdirectory of the dierectory you’re in. The relative pathnamethen 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 oneyou’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

Page 29: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 29/77

/

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

Page 30: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 30/77

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, “~”. (thisalso explains why there’s a tilde in your prompt).

To change directories, you can use the cd command (short for change directory . It simplytakes 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/bincd -

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

cd

30

Page 31: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 31/77

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 seeany 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 directoryyou’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 towonder 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 onyour 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 backto read text that’s already off the screen. However, there is a more efficient version of 

31

Page 32: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 32/77

 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 willshow 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 QuotaAs usually many people work on a single Unix system, the system administrator needs tomake sure no single person can abuse the system. One of the ways he can do this is byusing 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 differentdirectories. 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 blocksmay still be used.

However, this does not mean you can use all space left on the file system. To find out howmuch 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 thateffect. Usually, when you get close to your limit, you will receive messages from the systemwarning 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 whichfiles is du. Try:

csp00~> cd

csp00~> du

2As in: less is more...

32

Page 33: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 33/77

This will show you how many blocks you’re using in each subdirectory of the currentdirectory. Often the output of  du -s * is more useful; this will summarise the usage forall directories in the current directory.

33

Page 34: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 34/77

Chapter 7

Files and directories

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

7.1 Files

A very useful option to the ls command you saw before is -l, which tells ls to give moreinformation 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 onekilobyte (1024 bytes). The directory /bin seems to take up 6784 blocks, or roughly 6.8megabytes.

Next, for each file, ls -l now shows the file type, the first character of the string on theleft. The file type indicates whether the file is a plain file (indicated by -), a link (l, seebelow) 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

Page 35: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 35/77

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 yousee, for example when you know that you’re looking for a filename containing a certainword. If this is the case, you can call ls with wildcards. The most important wilcdardsare:

• * 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 canalso be a pointer to a directory. Links can be useful for when you want to have a file withchanging 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 thelatest file. These links can be either hard  or soft  (also called symbolic). A hard link means

35

Page 36: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 36/77

that there is actually one file with two names; if you throw away one of the names, youthrow away the other one as well. A soft link is less strict; you can remove the link withoutchanging 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. Thatmeans that if you run the program awk, really the program gawk is run. This is shown inthe 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 passwordfile in the directory /etc. You can now use this link as if it were the password fileitself:

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 permissionsNext to the file type, ls -l shows the file permissions, the row of  r’s, w’s and x’s. This listreally consists of three groups of three characters each. The first three characters containsthe permissions for the user , the owner of the file; the second three are for the group, thegroup of the file; and the last three are for others, everybody else. The idea of a user groupwill 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 thepermissions you will encounter most:

36

Page 37: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 37/77

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 thedefault for text files: you allow people to see the contents of the file but not to changethem.

rwxr-xr-x myprogram means that everybody can execute the program myprogram, butonly 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 simplestform, looks like this:

chmod ugo+rwx somefile

This call of  chmod gives permission to read (r), write (w) and execute (x) somefile to theuser (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 -. Forexample, 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 (theuser) cannot read it anymore. Verify this using cat or less. End by changing thepermissions 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 andothers. The output of  ls -l shows the user and group to which the files belong. In thelisting 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 differentprojects. You would like the members of each team to be able to read and write the team’sfiles, but members of one team shouldn’t be able to read another team’s files. The groupmechanism makes this very easy, by putting users into three different groups and settingthe file permissions correctly.

In our lab environment, the main group is users. To see what groups you belong to, youcan (for example) use id:

37

Page 38: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 38/77

Exercise 7.4 Try this:

csp00~> id

It should print your user id (that is, your user number), your group id and all groupsyou 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 filesTo avoid cluttering the screen whenever you type ls, the convention is that all files startingwith a single . (for example, .cshrc) are hidden  files. You will not see them if you justtype ls or ls -l. If you do want to see them, you will have to use ls -a or ls -la. Trythis 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 -lmeans, it’s time to learn how to search for specific files. There are a large number of fileson a typical Unix system, so finding that one program you want can be hard. Some usefulcommands 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 thestandard places where Unix stores its programs. It will not find programs you madeyourself. 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

Page 39: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 39/77

find : the Swiss army knife of search tools. This command has a lot of options. The mostoften 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 filesbelow /etc.

Other useful options of find allow you to just select files newer or older than a certaindate, files of certain types, sizes, files with certain permissions, files belonging to acertain 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 olderthan 100 days.

file : shows information on file types. Just by the name, it’s usually very hard to judgewhat a file contains. It might be a program, some text, some program source code, afigure, an image, a sound etc. The file command tries to give an accurate descriptionof 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. Filetypes you can show on the screen are: ASCII text, C program text, Bourne shellscript 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 particularpieces 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 betweenquotes ("), 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

Page 40: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 40/77

• "^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. Forexample, 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, tryto 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

Page 41: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 41/77

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 thelast 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 overwritesit. 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 copyall 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 afile to another directory, all you do is give it another name. The mv command moves filesand can be called just like cp.

Exercise 7.11 In the previous exercise, you created a copy of  myfile.txt calledanotherfile.txt. Rename this file to renamedfile.txt using mv. Next, moveit 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 recyclebin, so there’s no way of getting it back!

Exercise 7.12 Use rm to remove the file you just moved, /tmp/renamedfile.txt. Verifyusing ls /tmp.

41

Page 42: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 42/77

Like cp, you can use rm recursively: rm -R. Be very careful with this option! You canthrow 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 isdone 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 beremoved, you can also use rm -R somedirectory).

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

Exercise 7.14 Create a backup directory and store copies of all text files in your homedirectory 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 hason board: vi.

1A favourite example among system administrators is: rm -R /, which removes everything  from thefile system.

42

Page 43: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 43/77

Chapter 8

Editing text files: vi

vi is the Unix text editor that most people love to hate. In the beginning, there wasonly 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 alreadyimproved 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 newerversion of  vi is installed, which is called vim (vi improved). It still mostly behaves likethe 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 mechanismwhich nowadays is not used in other programs very often. Still, a lot of programs behavelike 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 inthe 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) andthe 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

Page 44: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 44/77

Add file:

Save:

Exit if saved:

Exit, no save:

Search & replace:

r

w

q

q!

[a]s/.../...Search: /... ?... n N

Undo: u Repeat: .

s cw cc CChar Word Line Line rest

Delete/replace...

ia c

Mark:  m 

Save & exit: ZZ

Match any enclosed character

Match any character not enclosed

Take next character literallyBeginning of line

End of line

Any character

Search meta−characters:

.

$

^ \

[chars]

[^chars]

vi commands ex commands

Cursor:

Scroll:

Status:

[Ctrl−F] [Ctrl−B]

[Ctrl−G]

h j k l #G

:

command mode

Redraw: [Ctrl−L]

Delete: x dw dd D

a o i O

Yank:

Replace 1 char:

yw yy

r

Replace mode: R 

LineCharAfter... Before...

Char Line

Insert...

[Esc]text mode

Put:

Join 2 lines:

 p P

J

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

Page 45: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 45/77

• You can move the cursor using the cursor keys or (for keyboards that lack cursorkeys) 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 thecommand dd, while your cursor is somewhere on that line.

• Remove two of the empty lines following “A history of Unix” by moving thecursor 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 likethe 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 optionfor that: press

£

¢

   

   ¡u to undo the mistake.

• The comma after “An historical overview” should really be a full stop. If youwant 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”, entertext 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 movingthe cursor there and pressing

£

¢

   

   ¡x . You will stay in command mode.

• The line following “1979” contains more than 80 characters, so it will not looknice 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 thetext a little easier on the eyes:

Exercise 8.2

45

Page 46: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 46/77

• 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  thecurrent cursor position (use ? instead of / to jump to the first occurrence of thestring before the current cursor position).

• The goal is now to add the string “the language” between “from” and “B”. Inorder to do this, move the cursor to the space between “from” and “B” andpress

£

¢

   

   ¡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 capitalletters in two places. It would look better as “pipes”, but changing everything byhand would be annoying. It’s better to use search and replace.

• The command :s (note the :, which means “enter ex mode”) can be used forsearching 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 theentire 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. Trythis. Does it work?

• Replace all occurences of “Unix” by “UNIX”. What do you notice on the lineon 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 UNIXalso takes off outside universities.

Now it’s time to leave vi:

46

Page 47: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 47/77

• The simplest way to leave vi would be to enter ZZ, which means “write andquit”. 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 preventyour 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 complainingabout unsaved changes, you can force the quit using :q!.

• Using address ranges, you can also save a part of the file. Edit myfile.txt withvi and try: :1,$-10w myfile.part1.txt. What do you think it does? Verifyusing 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 recentlydeleted, 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 isshifted to "2 etc.

• Named buffers, which are operated on only by the user and whose contents are storedthroughout the editing session. If they are indicated by "a ... "z, their contents areoverwritten when you copy text into it; if indicated by "A ... "Z, the new text isappended.

This may seem a bit complicated, but it’s not so bad. Remember that the General PurposeBuffer (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 workon 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

Page 48: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 48/77

so that you can construct commands like these (don’t forget that all commands should befollowed 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 hadto specify the number of lines to yank or delete, e.g. 5. However, often it’s easier to marka 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 incommands, 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 namedbuffer "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 bethe 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 mightwant to consider this mapping: :map Z :, which means that whenever you type a Z (invi) you enter ex instead. This means you can no longer use ZZ to leave vi, forcing you to

48

Page 49: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 49/77

use either :wq or :q!. As there is no such thing as a recycle bin in Unix, you can’t be toocareful!

Map commands can be stored in the file .exrc in your home directory. This file is readeach 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 extensivehelp 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 4characters. Use the meta-characters for searching shown in figure 8.1. First tryto 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 othereditors. This is a short overview; try some of them to see whether one suits you betterthan 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 likeTurboPascal 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), thereare many more (user-friendly) editors to choose from. We will discuss these in the chapteron X-Windows below.

49

Page 50: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 50/77

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 deeperinto 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 ASCIItext 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 outputto 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 wantto 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

Page 51: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 51/77

Standard error is used to write error messages to. If you don’t specify anything, these willalso be written to the screen. However, you can also redirect it to go together with thestandard 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, removedate.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 standardinput, 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. Mailit 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 youwant 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 dothis: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 outputof one command directly to the standard input of another command using pipes. Pipes

51

Page 52: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 52/77

Program 1 Program 2 Program 3

Input file Output fileTemp file Temp file

(a)

Input file Output fileProgram 2Program 1 Program 3

(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) illustratesthis. 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 tosee intermediate results, or maybe store them. The command that al-lows you to do this is tee (for T-connection ). If   tee is called withthe -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 thisfile 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 formthe glue between these tools.

Finally, when discussing pipes, it’s useful to mention cat. This command was introducedin section 6.4, but it’s a bit more versatile than you thought:

52

Page 53: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 53/77

• you can supply a number of file names, which cat will show after another. Forexample, try:

csp00~> cat myfile*txt

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

csp00~> cat > short.txt

You will notice that you don’t get a prompt. Instead, everything you type is storedin 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 nowyou have an idea of why this is useful. Using pipes and redirection you can access a lotof devices just by putting text into a file representing the device, or reading from thatfile. The Unix kernel settings can also be accessed as files. As a result, you can use thecommand 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

Page 54: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 54/77

Chapter 10

The shell

In this section, some specific things about the shell will be discussed. Of course, you havebeen 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 someremarks:

• If you specify arguments to commands, protect them using quotes. Protection meansthat you do not want the shell to start interpreting meta-characters. There are twotypes of quotes. The double quotes (e.g. grep "some thing" file) just protectagainst simple meta-characters, such as space. The single quotes protect quite a bitmore. 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. Youwill 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

Page 55: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 55/77

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

csp00~> cdcsp00~> ls

is the same as:

csp00~> cd; ls

• If you want to group two commands together, use parentheses. For example, try thedifference 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 filename 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, youcan 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 generalfile. 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 sincethe 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 23you entered the command cp myfile.txt myfile1.txt, then !23:s/1/2/ will actuallyperform cp myfile.txt myfile2.txt.

55

Page 56: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 56/77

10.3 Different shells

The shell you have been using during this course is called tcsh. We already discussed youcan use other shells. The main difference between shells is the notation; most of them cando 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. Thetcsh and csh shells both have a notation which looks a lot like the C programminglanguage.

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

After the system-wide files, the shell executes your own specific initialisation files. Theseare called .login and .cshrc and reside in your home directory. When you log out, a filecalled .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

Page 57: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 57/77

You can create text files containing list of commands for the shell to process (as if youtyped them in). Such files are called shell scripts, and they are very useful as they allowyou 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 anotherinterpreter (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 canexecute 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 alist 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 previousexercise. What happens?

57

Page 58: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 58/77

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 discussedin more detail in section 10.7). The variable path contains a list of directories the shellwill search for programs. It will search the directories in order, so if you create your ownls script file and put it in a directory before /bin, it will be used instead of the originalsystem version of  ls.

Exercise 10.7 Look at your path shell variable using the set command. Can you find adirectory in your path in which you are able to store your shell script l? Move l tothis 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 shellre-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 byarguments.

To get an overview of all active aliases, you can just enter alias without arguments. Toget 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

Page 59: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 59/77

10.7 Shell and environment variables

Above, you already had a look at shell scripts. These scripts can do more or less everythingyou 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, thereare two types of variables:

• shell variables, which are only visible in the current shell, and are mostly used tochange the shell’s behaviour;

• environment variables, which are accessible from any program or script calledfrom 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’ttyped 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 environmentvariables are simply copies of corresponding shell variables, e.g. HOME and PATH. Otherscontain 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 andEDITOR can be used to set your preferences (here, your favourite printer and editor) andare 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

Page 60: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 60/77

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 justprints 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 promptis 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 usingfinger.

60

Page 61: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 61/77

Chapter 11

Processes

As said before, Unix is a multi-tasking  operating system, like many operating systemsnowadays (examples of single-tasking operating systems are Apple’s MacOS and MicrosoftDOS, both of which are very old). Multi-tasking means that more than one program (orprocess) can run – seemingly – at the same time. However, as there is usually just onesingle processor in each workstation, what really happens is that each process runs for alittle 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 processis running; in this case it’s terminal 1. Next, ps shows how much time has been used bythe process so far. As usually these commands run very quickly, here it’s 00:00:00, or lessthan 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

Page 62: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 62/77

processes (that is, you!) and the size in memory (SZ) in kilobytes. Also interesting is thesecond 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 mightbe 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 basedon initialisation files. This means there’s a hierarchy of processes: each process (exceptinit) 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 thistwice more. Use ps and pstree again to inspect the processes. Do they confirmwhat 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 youagain until the process is finished. In some cases, you might want to start a process butreturn to the prompt immediately. This is especially useful for programs that run for along time, such as long calculations.

Exercise 11.2 To start a process in the background , put an ampersand (&) behind thecommand 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

Page 63: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 63/77

process number 2350. A grinding noise made by the hard disk should confirm themachine 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 itneeds 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 howeveralso 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 yourprompt 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 yourdu-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, youmight wonder how you can stop processes. The simplest solution, when the process isrunning 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 numberPID;

• 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]). Stopit using the job number, e.g. kill %1. Start it again, and now kill it using the PID.

63

Page 64: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 64/77

Actually, the kill command sounds far worse than it is. It really just sends a signal  toa 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 thenice 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 allocatedto 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 diskspace; 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 anice, 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 torun each process as often as each other process. However, some processes should have ahigher priority, such as processes which regulate hard disk access, network transport, theclock 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 nicestlevel is 19, and the level with the highest priority is -20. To prevent normal users fromattaching too high a priority to their own processes, you can only assign “nicer” levels toyour processes; that is, the nice level can only be increased. Only the system administratorcan give processes a lower nice level and thus a higher priority.

1The name core dump comes from ancient history, when computer memory was still made out of smallmetal cores which could be flipped in two ways to represent 0’s and 1’s.

64

Page 65: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 65/77

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, whichcan 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 atthe 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 peopleon a single machine.

65

Page 66: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 66/77

Chapter 12

X-Windows

If you work a lot in Unix, you will start feeling the need for working in several terminalsat the same time. You could edit a file in one terminal, compile in a second and browsethe web in a third. To this end, in the 1980s people at MIT put together a packagecalled X-Windows. In fact, X-Windows is more a set of rules and functions than an actualapplication. It has been stable for quite a while; the last 5 years or so, it has been atRelease 1.1, Version 6 - or X11R6. Therefore, X-Windows is often called X11. Below we’llbriefly discuss the X11 setup, have a look at a number of desktop environments and playaround 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, forexample, Windows 2000 does. All X11 does is allow programs to control regions on thescreen, draw in them or print text. It does this by sending commands to the X-Server , theonly program allowed to talk directly to the video card, mouse etc. Next, you’ll need atoolkit , a set of functions you can call to draw things like buttons, borders, lists etc. Usingthat 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 createapplication 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 usedis a very good and freely obtainable one called XFree86 1. Note how different this

1See http://www.xfree86.org/.

66

Page 67: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 67/77

Hardware

Kernel

User tools

Shell OSToolkit

X11

Window manager &applications

X−Server

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

• the X11 layer can abstract away from the hardware. This is very important: at thislevel, all video cards, mice etc. can be talked to in the same way. This layer has notchanged much over the past few years. Another very important advantage is that thelanguage spoken between the X11 layer and higher layers can be transported overthe 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 toolkitif 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 offerssomething called virtual screens, which means that your desktop size is much largerthan your actual screen size, so you can switch between different screens. This allowsyou 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

Page 68: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 68/77

12.2 Window managers

There are a number of window managers still in use, which all differ slightly in look & feeland 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; theyare 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 andWindows OSes: icons, start buttons, task bars etc. However, they also use a lot of memoryand 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 shouldnow look like figure 12.2 (a).

Log out (look under the “footprint” button in the bottom left) and now login with aKDE 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 sametime. 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

Page 69: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 69/77

(a) (b)

(c) (d)

(e) (f)

Figure 12.2: X11 screenshots: (a) Gnome, (b) KDE, (c) KDE’s start button, (d) openingconsoles, (e) remote logins and (f) remote applications.

69

Page 70: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 70/77

"Start button" Task barVirtual screens Console Netscape

Menu items

Icons

Desktop

Figure 12.3: KDE desktop elements.

Exercise 12.2 Use the “K” start button to find Utilities -> Konsole. Start twokonsoles (KDE console applications) and re-create figure 12.2 (d): in the bottomterminal 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 usingxterm &. 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 buthave its output displayed on your machine.

70

Page 71: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 71/77

Exercise 12.3 Login remotely on csp0X (hint: see section 3.4), where X is a numberbetween 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 younotice?

The programs on csp00 need to know where to send their information. The environmentvariable DISPLAY should contain the machine name followed by :0.0 (which means monitor0, 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 workingon (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 windowson your screen, right?

Exercise 12.5 On your local workstation , type:

csp0X~> xhost + csp0YThis means: allow (+) one remote workstation (csp0Y) to display applications on myscreen.

Run xclock or xcalc. This time it should work, and your screen should look likefigure 12.2 (f).

All this might seem complicated, but any reasonably smart system administrator willautomate 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 , trythis:

csp0X~> xon csp0X xcalc &

Note that you will still need to allow display using xhost.

71

Page 72: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 72/77

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 moreapplications 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 browsedthe 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 thanvi (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

Page 73: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 73/77

Chapter 13

Advanced commands and packages

You’re now ready to start working with Unix. However, it’s very likely you’re not donelearning. As you will work more with Unix, you will start feeling the need for somemore utilities. Usually, when you need something, odds are there is already some programpresent which does more or less what you want. The trick is finding it – but of course, youknow how to use apropos...

Below, a short list is given of both useful commands and packages installed on most modernUnix systems (especially Linux, which comes with an impressive array of programs). If youdon’t have these on your workstation, you might ask the system administrator to installthem (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 toprevent files from being thrown away because they’re “too old”, or to create emptyfiles;

73

Page 74: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 74/77

• 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 afile containing “just the differences” which you can then send to someone else. Thisperson 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 alsotranslators: 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 DOSfloppy disks;

74

Page 75: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 75/77

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

• 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

Page 76: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 76/77

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 fromhttp://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 t r - 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

Page 77: Unix Lab Course Manual

8/6/2019 Unix Lab Course Manual

http://slidepdf.com/reader/full/unix-lab-course-manual 77/77

– 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 fromanywhere 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 theprocess number field

• supply this output to kill -9