Вы находитесь на странице: 1из 42

Unit-5 1

Unit-5
Getting Started with Unix

GETTING STARTED

We will now learn how to start using a UNIX computer. This unit will talk about
the basic steps involved in signing on to a system running LINIX and also what
you can do once you have gained ingress. But remember that you cannot learn
UNIX merely by reading this unit or even this block. That might at best allow you
some familiarity with the terminology used and might tell you something about its
organisation. You might even come to know something about its features and the
tools available under it. But you will not be able to work on a UNIX computer or
feel comfortable in a UNIX environment, much less be productive in it. Nor will
you be able to appreciate the power and beauty of UNIX.

The only way to learn UNIX is by working on a real UNIX machine. This unit, this
block and other supplementary reading material, together with the UNIX
documentation, will be a valuable aid in your voyage of discovery. So you must
gain access to a terminal connected to a working UNIX machine and try out
whatever you feel like. Do not be afraid of exploring or making mistakes.
Whenever you learn about a command or any other feature, do not hesitate to try
out all its variations. Do not confine yourself to only what is mentioned here. This
block is of necessity very brief and can only serve as an introduction. Use any
other material to which you have access and experiment to your heart's content.
You will learn as much from your mistakes and from seeing unexpected
outcomes as from things you do by the book.

User Names and Groups

Every LTNIX user is given a name when he is allowed access to a UNIX system.
This is also called an account, as in commercial arrangements an account is kept
of the usage of the machine by each user. The user name need not have any
relation to the actual name of the user, though it quite often is some abbreviation
of the name. For example a person called Ram Kumar would usually he given a
user name kumarr on a UNIX system. This is formed by his surname
(abbreviated if it is too long) and the first letter of his first name. It is quite
possible for a person to have more than one account on a single machine (in a
different name) especially if the person uses the machine in more than one
capacity. For example Ram Kumar might be working on two programming

ACME/Gulshan Soni
Unit-5 2

projects, in both of which he is part of a team. On his cryptography project he


might have a name crypt02 and on his natural language processing project he
might have a name like n1p04. The reason for this kind of arrangement has to do
with his access rights and privileges in his different projects. Also if Ram Kumar
leaves the company his successor Zafar Khan might continue his work on nlp04
while somebody else is assigned to work on crypt02. One of the things that
motivated the designers of UNIX was their desire for easy sharing of information,
consistent with the needs of security and privacy. So UNIX allows user names to
be grouped together under a common group name. All users belonging to the
same group can share group privileges.

Some user names are reserved by UNIX for its use, for example bin and uucp.
So you cannot use these names for yourself. There is also a special kind of user
on every UNIX system who has all possible access rights on the system. This
user is called the super user, the system administrator or simply root because
that is the user name conventionally allotted to him. For administrative
convenience large systems can have more than one super user account. The
super user is the one who can create new user accounts, shutdown the system
and perform other maintenance, tasks.

You might be wondering why everybody cannot access the computer as root.
The reason is that when you are granted access to a computer system you are
assigned a user name as well as a password. You can set your password to
whatever you want subject to certain constraints. So you cannot enter the
computer as root unless you know the root password. The root password is
zealously protected on any well maintained installation as public knowledge of
this password would compromise the security of the installation.

While root can access all user files and override any system protection meant for
mere mortals, nobody can figure out what your password is. However root can
change or remove your password.

Logging in

You will now learn how to gain access to a UNIX system so that you can use its
facilities. This process is called logging in to the computer. To be able to login to
a machine you must have a valid user account on it and you must know your
password. Your account would have been created for you by the system
administrator when you were allowed to use the computer. At that time you would
also have been told your first password. When you see your terminal it would be
displaying a message like

ACME/Gulshan Soni
Unit-5 3

IGNOU UNIX computer

login:

The actual message on the first line depends on the installation. This could even
be absent. The message does not affect anything else you do in any way.

You should now type in your user name and press the RETURN key. In most
cases you have to press the return key for the computer to register what you
have typed. This key is sometimes labelled as ENTER. You will find that as you
type on the terminal screen you will be able to see whatever you have typed.
This is because UNIX usually echoes whatever you type on the terminal. So your
screen should now look like this

IGNOU UNIX computer

login: kumarr

Password:

You must type in your user name, also called the login name, exactly as
allocated by the super user. This is because UNIX is case sensitive, that is, it
distinguishes between lower case and upper case letters. In this respect it differs
from operating systems like VMS. So be careful of small and capital letters while
working on UNIX.

When MX asks you for your password, key it in carefully. Notice that your
password is not echoed as you type. In fact the cursor does not move at all. This
is to prevent somebody from reading your password over you shoulder, as that
would enable that person to masquerade as you by logging into the computer in
your name and using it.

UNIX now checks whether you are a valid user and whether you entered the
correct Password. If there is any mistake you get a message saying

Login incorrect

login:

This means you can try to login again. There can be other reasons why you
might not be able to login even though you are a valid user and did not make any
typing mistakes. The messages you get in those situations will however be
different.

Why be so pessimistic? Let us assume you have managed to login successfully.


The system will then display some messages and finally give you a sign that it is
now ready to obey your commands. Ale messages you see depend on how the
system has been configured or set up by the system administrator and by you.

ACME/Gulshan Soni
Unit-5 4

So you might not even see any messages. However usually there is a message
indicating when you logged in last. This is useful because if the date and time
mentioned there are different from what you remember about your last login, it
could mean that somebody else is using your account

Let us now look at some of the other common types of messages you see on
most systems as you login. These usually give some information about the
system like the space available on the machine, news about the system and
whether you have any mail. The news is called the message of the day and
appears whenever you login. The message

You have mail.

After the login messages you see a prompt, which is the sign that UNIX is ready
for your commands. The prompt can be changed to whatever you like but the
default prompt also depends on what shell you have been assigned. One of the
most common shells is the C shell, which has the following prompt by default

This is the prompt we will use throughout the block unless some other prompt is
explicitly called for. When you see the prompt on your terminal it normally means
that UNIX has finished executing the last command you gave it and is ready for
your next command.

On some UNIX installations there is a limit on the number of attempts, say five,
you can make at logging in. The action taken depends on the installation but can
be alerting the system administrator or deactivating the terminal, perhaps for a
short time only. So you should be careful not to make too many typing mistakes.
In particular be careful not to forget or mistype your password and avoid
passwords with certain characters like #.

Format of UNIX Commands

We will now look at the general format of UNIX commands and take the
opportunity to study some simple commands. Let us go back to the C shell and
the cal command which we mentioned earlier.

$ ^D

% cal

This gives the calendar for the current month and year (of course this will depend
on what the system date has been set to and is what the computer will believe to

ACME/Gulshan Soni
Unit-5 5

be the current month and year), and you can use it to say, find out how many
sundays are left in the month. Another simple command is

% date

which displays the current system date and time. You will realise that the
computer has no way of knowing what the current date and time really are, so
what it can tell you is only what it thinks is the current date and time. This can be
set by the system administrator to almost anything but in most installations,
especially those that are networked with other computers, care is taken to see
that the date is set correctly. The date in UNIX means date and time, so the
output of the command is something like

Wed Jun 15 13:44:39 IST 1994

Notice that the time zone is part of the output. This is significant when you are on
a network spanning time zones.

Another simple one word command is

% who
Kumarr tty03 Jun 15 11:49
Khanz tty05 Jun 15 10:22
nlp04 tty07 Jun 14 23:36

This tells you the names of the users currently logged in to the system, their
terminal numbers and the date they logged in. You will find that you will always
be listed as one of the users, since you usually run the commands only when you
are logged in to the machine. There is another form of the who command which
you can now try out.

% who am i
kumarr tty03 Jun 15 11:49

This time we have given the arguments am i to the actual command who. lie
result is similar to that obtained earlier, but now you are the only user listed. This
command has the effect of telling you the login name of the user currently logged
in at that terminal, the terminal number of the terminal and the date the user
logged in. Other users of the system are not listed.

Arguments to commands are separated from the command by one or more


spaces. It might seem silly to ask the computer who you are, but if the previous
user has not terminated his session, you can find who it was by this command.
But you would do well never to leave your terminal unattended while you are
logged in, as it would be a security lapse. Some versions of UNIX provide the
command

ACME/Gulshan Soni
Unit-5 6

% who are you

which is synonymous with who am i, but sounds much more intelligent.

You have now seen the general format of UNIX commands, which comprises of
the basic command followed by zero or more arguments. The command and the
various arguments are separated by one or more spaces and the whole
sequence is terminated by the newline character, which is produced when the
ENTER key is pressed.

You can enter more than one command on the same line by separating the
commands from one another with semicolons like this

% date; who
Wed Jun 15 14:02:11 IST 1994
kumarr tty03 Jun 15 11:49
nlp04 tty07 Jun 14 23:36
crypt02 tty08 Jun 15 13:57

The commands are executed one after the other in the order they were specified
on the command line. After the last command is over you get the prompt again.

Arguments to commands should not contain spaces otherwise the different


words of the argument would be interpreted as different arguments by the
computer. If for some reason the argument needs to contain a space, you must
enclose the argument in double quotes (") or in single quotes (').

Most arguments to commands are filenames (discussed later in this unit), options
or expressions. All of these could occur in the same command. The exact order
in which thee arguments are listed can depend on the command and should be
ascertained by examining the documentation for that command. Usually options
immediately follow the command with the expressions and filenames coming
next. You will see details of such cases later when we study more complex
commands than the ones we have looked at so far.

If an argument itself contains quotes of one kind you can enclose it in quotes of
the other kind. Thus

% grep -n "Ram Kumar's Salary" employee payroll

looks for the expression Ram Kumar's Salary in the files employee and payroll
and prints the line numbers of the lines in which the expression is found.

Changing Your Password

ACME/Gulshan Soni
Unit-5 7

You saw earlier that your password was the only way of preventing somebody
else from using your account on the system. Without it anybody who knew your
login name could walk up to the machine and start using your account. This
would be really serious in the case of the super user or root.

When you are first given your account you are told what your password is. On
some installations your account is set up without a password and you are asked
to choose one for yourself the first time you login. Ibis can be done with the
command

% passwd

Changing password for kumarr


Old password: pi, 14
New password: expl=2.71
Re-enter new password: expl=2.71

Note that unlike the commands you saw so far, the passwd command is
interactive. It asks you to enter some information rather than doing all the work
by itself. The first thing it asks for is your current (or old) password. This is to
make sure that somebody else cannot change your password while you have left
your terminal unattended. If the wrong password is entered here, the computer
says Sorry and gives you back the prompt.

If you enter the old password correctly you are asked to type in your new
password. After that you are asked to enter it again. If you type two different
things here, the system tells you that they do not match and asks you to try
again. If you keep getting mismatches the command terminates after telling you
to try again later. This is because if you cannot change your password you are
unlikely to be able to enter it correctly to login.

Although in the example above we have shown the passwords, in an actual


session none of the passwords will be echoed. Your system will probably have
restrictions on what passwords you can choose. The password should not be too
short or too long. You should change it periodically so that if someone has been
using your account by laying hands on your password, they cannot continue to
do so indefinitely.

If you are wondering how the Passwords are stored on the machine such that
even the super user cannot find out what your password is, the answer is that
UNIX encrypts your password before storing it. This means that what is stored on
the computer bears no resemblance at all to what you typed in as your password.
When you try to login the next time, UNIX again encrypts the password you type
in and compares it with what has been stored.

If the two are the same, you are allowed to login, otherwise your attempt is
blocked. So while a super user, or anyone else for that matter. can read your

ACME/Gulshan Soni
Unit-5 8

encrypted password, nobody can find out what the actual password is ---- at least
not easily.

There is another form of the passwd command which is used to change the
password of some other user. By default the passwd command allows you to
change the password of the user who is logged in to the terminal. So to change
the password of khanz, you could say

% Passwd khanz

where the name of the user whose password is to be changed forms the
argument to the passwd command. The rest of the behaviour of the command is
just as before. You will now realise that you can change the password of any
user, including your own, only if you know his current password. On your system
you might simply get the following message if you try to change somebody else's
password

Permission denied.

How then does root have the power to change Your Password? Ah! When the
user executing the passwd command is the super user, UNIX does not ask it to
supply the old password. This is how the super user can change your password
to anything without knowing what it is currently.

Characters with speacial meaning

As mentioned before some characters are interpreted in a special way by the C-


shell. These meanings will be discussed in detail in the next unit of this block, but
that apart, there are certain characters you will find to be useful.

For example, suppose you start a command which takes a long time to execute,
and you change your mind and do not want to wait for the command to finish.
You can abandon or break a command in between by pressing the BREAK
character, which is the key marked DEL on most systems. This key can be set
using our old friend, the stty command

% stty intr ^C

sets the INTERRUPT character to CONTROL-C.

Again, consider a command which produces a lot of screen output. This could
happen if you were typing out a long file, for example. The output will probably be
dumped on your terminal far too fast for you to read. To stop output on the
screen temporarily type ^S. You can restart the output by typing ^Q.

Another special character can be used to terminate your login session. This is ^,
which indicates to the shell that there is not going to be any more input from you.

ACME/Gulshan Soni
Unit-5 9

So UNIX logs you out and again displays the login message on the screen for
another user, or you again, to login and start a new session. You can also logout
by saying

% logout

or

% exit

UNIX Documentation

UNIX comes with copious documentation, some of which is often available on-
line. You should learn how to use the UNIX manuals. While we will not discuss
this topic in detail here, you will have to acquire this skill if you want to obtain a
good understanding of UNIX. This is because in this block we do not have the
space to consider any but the most basic commands, and even those only
briefly. We will not even be able to consider all the options available with many of
the commands that we do discuss. The only way for you to master them will be
by consulting the documentation.

If any documentation is available on-line at your installation, you can look up the
manual entry for a command by using the man command. For example, to learn
more about the who command than what we have talked of, say

% man who

You can similarly learn more about the date, cal or any other command. So to
learn more about the man command itself, say

% man man

If the documentation is not on-line you will have to use the printed UNIX
manuals.

FILES AND DIRECTORIES

In this section we will describe the file and directory structures Of UNIX. Just as a
paper file is something into which you can put Papers and bunch a group of
papers together, a UNIX file is something into which you can put data. A file has
a name, and this name is a property of the file rather than the data present in it at
any given time. It is possible to change the data in file. Thus UNIX commands
can be made to operate on the data in a file as a group.

ACME/Gulshan Soni
Unit-5 10

A file usually exists on the hard disk(s) of the computer. This will be the case
when you are logged in to the machine and are engaged in a session. The actual
areas of the hard disk used by a file can change as the file is increased and
decreased in size. As you will see later, the size of a file in UNIX has a Precise
technical meaning, and the size of a file does not necessarily tell you the actual
amount of data in it.

UNIX has three kinds of files-- ordinary, directory and special. You have already
got an idea of what ordinary files are. Special files will be discussed in unit 5 of
this block.

Directory files contain information about about other files, including other
directories or special files. A directory groups its contents together hierarchically
under itself, and a directory within a directory is called a subdirectory of the
directory at the higher level, also call the parent directory. Thus in UNIX the file
system is like an inverted tree of directories, starting as a root and going down to
an arbitrary depth of hierarchically arranged levels.

We will now look at some of the files in UNIX and learn how to use the file
structure.

Current Directory

Every user who is given an account on a UNIX system is also given a directory
where he reaches on logging in. This directory is also called the home directory.
The current, working or current working directory is the directory in Which you
are currently located. On logging in, your current directory is normally your home
directory. You can find out what you current directory is at any time by using the
command

% pwd

/usr/kumarr

This means that your current directory is called kumarr and is located under the
directory usr, which is in turn located under the root directory of course the actual
home directory you are allotted will depend on your installation. By the way pwd
is one of the few UNIX commands which do not take any arguments or options.

The Output that pwd displays is called the full Pathname of your current working
directory. This is also known as the complete or absolute Pathname, that is, the
pathname starting from root. You can refer to your directory by just saying
kumarr. But this is not unambiguous because there can be another directory
called kurnarr under some other directory as well. But no two directories or files
on the same UNIX machine can have the same complete or full Pathname. The
various components of the path are separated from one another by slashes ( '/').

ACME/Gulshan Soni
Unit-5 11

We have not yet talked of what a valid filename can be. Actually in UNIX there
are no restrictions and a filename can have any characters upto a maximum of
14. The same rules apply to directories as well. In some UNIX implementations
filenames can be of any arbitrary length. In Practice it is best to avoid certain
characters in filenames because they have special meaning to the shell.

Looking at the Directory Contents

We will now see how to look at the contents of a directory. The command is

% ls

This gives you a listing of all files in the current directory. If you have just been
allotted your account and are logging in for the first time. you will be in your home
directory and that directory will be empty, that is, there will be no files in it.

Is has several options and it will take you some experimentation to understand
them all. The first option we look at is

% ls -a

This is your first taste of UNIX options, so look at the command line carefully.
The command ls is followed by at least one space after which the hyphen or
minus sign introduces the option letter. The -a option tells UNIX to list all files
including those that are 'hidden'. Hidden files are those which start with a
'.'character. Unless the -a option is used, ls never lists such files in its output. The
output of ls is always sorted in some order, the default order being alphabetical.
This sort order can be altered by other options to Is which we will take up later.
This is why the file (actually a directory) '.' is listed before '..'in the output.

The '.' refers to the current directory and '..' to its parent. These are pronounced
dot and dot dot respectively. In this case '.' refers to the /usr/kumarr and '..' to
/usr. The directory '/'or root is its own parent. This output is of course not very
interesting because your home directory is devoid of files and you do not yet
know how to create any. So let us look at some other directory. You can get the
listing of any directory by supplying its name as an argument to ls. Thus to look
at the directory listing of the root directory use the command

% ls/
aardvark
bin
dev
etc
lib
lost+found
tmp
usr

ACME/Gulshan Soni
Unit-5 12

We must caution you that it is very unlikely that you will see the same listing as
shown here. It is self evident that the listing will depend completely on the
machine you are working on. However there are some files that will surely exist
on the root directory of a working installation. The directories from bin to usr are
such files.

As you have seen the Is command lists one, file per line of output to see several
names per line you can use

% ls -x
aardvark bin dev etc lib lost+found
tmp usr

Now the output is sorted from left to right on each line. Another variation is the -C
option which sorts down each column

% Is -C
aardvark dev lib tmp usr
bin etc lost+found

You might have found your output to be in one of these forms the first time itself.
This would have been because your system was configured to make the -x or -C
option the default option for ls.

From the outputs so far you can get no indication of whether the files shown are
ordinary files or directories. For this you can use the -p option, which appends a
'/' to every file name which is a directory. The '/' is not part of the name, so do not
get confused. For example

% ls -Cp/
aardvark dev/ lib/ tmp/ usr/
bin/ etc/ lost+found/

Another such option is -F which also appends a '*' to every filename which is an
executable file, that is, a command. Try it out and see whether the result differs
from the -p option.

If you have a really large directory you might want to use an option of ls which
gives a very compact output

% ls -m /

aardvark, bin, dev, etc, lib, lost+found, tmp, usr

ACME/Gulshan Soni
Unit-5 13

This gives you the filenames separated by commas.

You can see from the above that the contents of the root directory consist of both
directories and ordinary files. The directories here, or anywhere else, can
themselves contain sub directories. To see the contents of /usr, you can say

% ls -xp /usr
bin/ khanz/ kumarr/ lib/ tmp/

On most systems you will see the names of user accounts in this directory. The
-p or -F options will show you that they are directories. You must have deduced
that you are seeing the home directories of the users. You can also see your own
home directory here. But wait! When you logged in and checked the name with

% pwd

/usr/kumarr

you found your home directory Specified differently. Why is this so? We have
seen in the last section that the pwd command tells us the full, complete or
absolute pathname of the current working directory. When we look at the
contents of /usr, kumarr is merely one of the directories under it, and is shown as
such. To get the complete pathname we must specify the preceding portion
which is /usr. Thus he full or complete pathname is /usr/kumarr.

It will now be easy for you to realise that the bin You saw listed as one of the
contents of the root directory, that is, '/', is different from the bin listed under usr.
The former has the full pathname /bin, whereas the complete pathname of the
latter is /usr/bin. You can now look at the contents of the other directories and try
specifying their complete pathnames. You can also try looking at their contents
by providing relative pathnames. We will look at complete and relative
pathnames again in the next section. You would do well to understand
pathnames, relative and absolute, thoroughly as that will be necessary in
navigating around the directory tree.

But let us now get back to our friend the ls command. One of the most useful and
often used options is -1, which gives the so called long listing of the directories
asked for

% ls-1/
-rwxr-xr-x 1 root root 1298 May 14 09:26 aardvark
drwxr-xr-x 2 bin bin 1248 Jan 01 1970 bin

Now this is a complicated looking Output, SO let us try and understand the
meaning of this listing. The first column of the output tells you whether the file is a
directory or not. A '-' means that it is an ordinary file while a directory has a 'd' in

ACME/Gulshan Soni
Unit-5 14

that position. So you now know another way of telling whether a file is a directory,
apart from the -p and -F options you have already looked at. The other 9 columns
in the first field tell you about the permissions on that file. We will look at these in
detail in section 2.4.6.

The next field in the output is a number indicating the number of links to the file.
For a file this shows the number of names it has. In UNIX the same physical data
may have several names, although it must have at least one. Each name is a link
to the file. Usually ordinary files have only one link, but if there are more it does
not mean that there are that many copies of the data in the file. There is only one
physical copy of the data which can be referenced using any of its names. In the
case of directories the number of links tells you about the number of
subdirectories it has.

The third field of the output shows the owner of the file. Root and bin are names
reserved by UNIX for its use as we have seen earlier. In some cases you might
see a number like 207 instead of the user name.

The next field is the group name and in certain situations can be a number in the
display. The user is a part of the group shown here.

The fifth field is the size of the file in bytes. You already know that the size of a
file in UNIX has a precise meaning which is unrelated to the amount of data in it.
However, do not be alarmed because in most cases the intuitive meaning of size
does hold good and the figures you see usually do represent the number of bytes
of data in the file in question.

The next item of information is the date the file was last modified, and in the end
the name of the file is shown.

You now know how to find out many useful things about the file. You should now
look at the directory long listing of the various system and other directories on
your machine. In the course of this when you look at /bin you will see many
familiar names. For instance, who, pwd and ls itself will be found in the /bin
directory. Actually /bin is where many of the binaries or executables of the
commands are to be found. There are other commands located under /usr/bin
and /etc as well.

We will now briefly look at three other options to the ls command. When a
directory is given as an argument to ls you get to see the contents of the
directory. But suppose you want to check the permissions on a directory, say
/usr/kumarr. If you try

% ls -1 /usr/kumarr

ACME/Gulshan Soni
Unit-5 15

you will see nothing because Is tries to list the contents of the directory and at
present there is nothing in your home directory. To see the desired output you
could say

% ls -1 /usr

whereupon kumarr would be one of the entries. But this is awkward. The answer
to this is the -d option

% ls -ld /usr/kumarr

which lists /usr/kumarr as a directory and shows all the information about it.

You have seen that ordinarily subdirectories are shown only as single entries and
any files inside them are not shown. To look at the contents of a directory and
recursively of all subdirectories within it, use -R

% ls -R /usr

will show the contents of /usr and also recursively of every subdirectory inside it,
down to ordinary files. Thus using

% ls-R/

you can see every file and directory on your system.

Another option is the reverse option. The -r option reverses the sort order of files
displayed by ls. You can try this with any option

% ls -r /

usr
tmp
lost+found
lib
etc
dev
bin
aardvark

So far you have given only directories as arguments to ls, but you can give it an
ordinary file as well. It then lists only that file if it exists. Moreover you can give
any number of files or directories as arguments to ls and it will list whichever
ones exist.

If You feel out of breath after looking at these options, there are a few more we
have not looked at. You are encouraged to look up the documentation for Is and
experiment with them. Many UNIX commands have zillions of options- getting
used to them all requires time and effort. But you will find that you soon get to

ACME/Gulshan Soni
Unit-5 16

know the options you use often. It is probably best, when learning a new
command, to concentrate on a few useful looking options only. As you use them
frequently you will get to know them well.

Then you can spend some time deepening your knowledge of the command by
trying out the other options. Most beginners get overwhelmed by the large
number of options and do not know where to start or when to stop. You will have
to work out a method which suits you. Maybe you are the type who likes to learn
everything about a command at one go. But many people, including the author,
find that building on a solid foundation of already know options is easiest.

Absolute and Relative Pathnames

You saw in the last section how pathnames could be relative or absolute. Since
the UNIX file system is logically structured like an inverted tree, it is important to
understand how to specify pathnames. Both methods can be used and in UNIX it
does not matter which approach you use in identifying the file you mean, as long
as you are careful about specifying it correctly. However there are situations
where one or the other approach is more convenient. So you should take the
trouble to assimilate the concept and learn how to navigate around the file
system with felicity. Let us look at a typical directory hierarchy on a UNIX
machine.

Of course the exact layout of the directory hierarchy on your machine is likely to
be different. We will soon be looking at some of the main directories and files on
a UNIX system. For the moment though, just concentrate on learning how to
move around. You already understand what is meant by the current directory.
This is the directory in which you are located at any given time. If you say Is, it is
the filenames in the current directory that are brought up for you to see. If you
have logged in as kumarr, you will probably land up in /usr/kumarr when you get
your prompt unless it has been arranged otherwise.

Now consider a file in /usr/kumarr/nlp like augcfg.C. Suppose you want to see the
size of this file alone. For this you need to use the Is command and provide the
filename as an argument to it. In UNIX you can provide a pathname (relative or

ACME/Gulshan Soni
Unit-5 17

absolute) as an argument to a command wherever you could otherwise provide a


bare filename. So that actually gives you three ways of accomplishing what you
want to (we will assume that you have the required permissions -- this will, in
fact, be the usual situation) do.

Let us first use an absolute pathname. So you have to specify the filename
starting from root or '/'. Thus your command needs to be

% ls -1 /usr/kumarr/nlp/augcfg.C

You have already used this method in the last section. The second way is to use
a relative pathname, where you specify the pathname relative to where are
currently. Here you only need to remember that '..' stands for the parent directory
of the current directory '.'. So if you are at /usr/khanz, you can say

% ls -1../kumarr/nlp/augcfg.C

The '..' takes you one level up, that is, to /usr. From there you continue naming
the file as before. Of course you could have used the following rather convoluted
way

% /s -1.././usr/kumarr/nlp/augcfg.C

This is inefficient because you implicitly move to root before naming the file. The
first'..' takes you to /usr and the second '..'takes you one level higher, to '/' or root
itself. Then you begin your descent until You reach the file you desire. Here it
would have been better to use an absolute pathname instead of this, for then
You would not have.had to use two steps to reach root.

Usually a file narne is specified by the method that results in the shortest
possible specification of the name. This depends on whether the filename is
closer to you or to the root.directory. T'hus if you are located in /usrlkhanz and
you want to specify a file in the directory /usrlkumarr, it is easier to say ..ikumarr
rather than Jusr/kumarr.

There is a third way of looking at the size of augcfg.C. For this you will have to
learn a new command cd, which let you change your current directory. This
command can be given an argument which is your intended destination and it
then changes your directory to what you asked, provided you have the
appropriate perrnissions. And how do you specify your desired destination? By
specifying die pathname, of course. The pathname can be specified, as you
would have undoubtedly guessed, either as a relative pathnarne or a complete
one. So you can say from /usrlkhanz

% cd /usrikumarf/nip

or

ACME/Gulshan Soni
Unit-5 18

% cd../kumarr/nlp

and then look at the size by

% ls -1 augcfg.c

This really amounts to specifying the filename relative to lusr/kumarr/nlp, the


current directory. In general when you specify a bare filename you are specifying
the filename relative to the current working directory. So the command above is
really a shorter way of saying.

% ls -1 /augcfg.C

One form of the cd command can be very convenient if you have wandered far
off your home directory and you want to return there, especially if your home
directory happens to be far away from the root directory. This is

% cd

without any arguments. it always brings you back to your home d irectory
irrespective of where you are, even if you were there to start with.

Some UNIX Directories and Files

It will be useful and interesting to get acquainted with the UNIX system directory
structure. We will now look at the layout and contents of the UNIX system
directories and understand how the various system files 'are grouped under
directories. We will also learn about the functions of some of the system files. Ale
UNIX directory structure is typically as shown in the earlier figure.

We again emphasise that only some of the system directories are shown here.
Your machine could have a somewhat different organisation. How will you find
out the directory tree for your UNIX system? You can now explore the files on
your machine.

The directory /bin contains, as you have already seen, the executables of UNIX
system commands. These include the commands you have learnt so far, like ls,
cd, pwd and who. You can look at the long listing of this directory and note the
information provided. Look at the sizes to get an idea of the sizes of executable
files on your machine. These will depend, among other things, on the
architecture of your computer.

The /dev directory contains device special files concerned with hardware devices
like printers, terminals and hard disks. You will learn more about these files and
the /dev directory later in this unit

ACME/Gulshan Soni
Unit-5 19

The /etc directory, as the name suggests, has several miscellaneous files and
directories. It contains many commands which are reserved for the use of the
system administrator. Ordinary users cannot execute many of these commands.
Apart from this, the /etc directory also contains some text files.

Let us take a quick look at some of these text files. /etc/issue contains the
message before you login. /etc/motd has the text of the message you see just
after you login. /etc/group has the names and group numbers of all the groups in
the installation. /etc/passwd contains the login name of each user, his user
identification number, his encrypted password, his home directory, the default
shell when he logs in and other information about him. In some cases the
password is stored in another file called /etc/shadow.

/lib contains system libraries used with your 'C' compiler. /tmp is used to store
temporary files. Some UNIX commands need work space in order to execute,
this is where they create their temporary files. This directory is cleared out
periodically on many installations. In any case any files you put here can be
erased without warning. So do not try to store anything here on a permanent
basis. Keep files important to you under your home directory only.

/usr/bin, as you have already found, holds UNIX system commands which are
more of utilities, although there is no clear distinction between commands in /bin
and those located here.

/usr/include contains header files used in writing C programs. /usr/games holds


games distributed with UNIX. This might not be present on some installations.

/usr/local/bin is often present as a repository of local commands, often developed


by local talent. These are commands of interest to and found convenient in that
installation.

While looking at the /usr/include directory, you must have noticed that all files
have names ending in '.h' and similarly you will find many files with names ending
in '.a' in nib and /usr/lib. Although we said earlier that UNIX places no restrictions
on the characters you can use to construct a filename, there are some
conventions followed in a few cases. Usually filenames ending in '.xyz' are
referred to as '.xyz' files or even as xyz files. Such conventions are not enforced
by UNIX, although in many cases standard UNIX utilities might do so.

Thus h files are C or C++ program header files, C program files end in '.c'
(enforced by cc), C++ program files end in '.C', lex source files end in '.1', yacc
source files end in '.y', assembler source files in '.s', object code files end in '.o',
library archive files in .a', SCCS (Source Code Control System - to be discussed
in unit 4) files start with 's.', 'p.' and so on.

ACME/Gulshan Soni
Unit-5 20

The file command is useful in determining of what type a given file is. This
command takes any number of files as its arguments and tries to determine the
type of each. Although it is not hundred per cent reliable and is open to deceit,
the command usually does a good job.

LOOKING AT FILE CONTENTS

So far you have not had occasion to look at what is stored inside a file. You have
only seen the filenames in a directory listed by the ls command, or moved from
one directory to another with cd, or the type of a file shown up by the file
command. To look at the actual contents of file like /etc/motd, say

% cat /etc/motd

This prints the contents of the file on the terminal. The cat stands for concatenate
and actually cat can be given any number of filenames as arguments. The effect
of cat is to join all the files in the order specified in the arguments and to send
them to the terminal screen. When only one file is specified the effect is to type
that file on the screen.

It makes sense to cat only text files to the terminal for otherwise the result might
not be readable. If you cat a binary file, that is, one which can contain any
arbitrary characters including control characters, you are likely to get strange
results. Your cursor might jump hither and thither on the screen, the screen might
get cleared from time to time, the terminal bell might sound or your keyboard
might get locked. These effects are caused because the cat command is sending
the contents of the file to the terminal. If the file contains character sequences
that have special effects on your terminal, you will observe these effects
occurring spontaneously. While this will not do any harm, it can be disconcerting
or annoying and in any case will not probably be very useful since you will not be
able to make sense of what got typed on the terminal.

If the text file you type is a large one, the Output will not fit on your screen. You
have already seen that you can use ^S and ^Q to control the stopping and
resumption of the output and thus read what is in the file. However if the file is
more than a few screens long, it will be difficult for you to keep suspending and
resuming output again and again. Moreover you will have to operate the
keyboard dexterously if you want just enough of the output to scroll by but not too
little or too much. If you are a trifle slow, some unread output might disappear
before you are able to read it.

To look at a text file screen by screen, you can use the more command.

- % more /etc/termcap

This command shows you one screenful of the file and then pauses until you ask

ACME/Gulshan Soni
Unit-5 21

it to proceed further. You can now examine the contents at leisure. When you are
ready to look at the next part of the file, you can press the Spacebar for seeing
the next screenful. You can move one or more lines forward by pressing a
number before the spacebar. You can also do this by pressing the number of
lines to move forward followed by the RETURN key.

Note that normally commands taken by more do not require you to press the
RETURN key, so that as soon as you have pressed the spacebar the display
moves forward by one screenful. You can search for a text string by saying '/'
followed by the search pattern.

The display will then scroll forward to show you the screen where that pattern
occurs next. In general you cannot move backwards in the file by using this
command. To terminate the display of the file, just press 'q' and you will get back
your prompt.

There are several other options available with the more command. You should
refer to the documentation and experiment with all the options so that you
understand them thoroughly.

There is another command called pg which has a similar function. You could use
this if more is not available or is not to your liking. You can compare both of them
if both are available on your system the ability to look at files in manageable
screenful is often very useful, so be sure to know at least the basic options to
these commands.

There is one more command that is useful when you want to look at only the last
few lines of a file. To see the last 20 lines of a file you can say

% tail -20 /ete/termcap

If the number of lines is omitted a default value of 10 lines is assumed. So you


now know how to look at the last line of a file. You can also use tail to start
looking at a file from any line number. For example

% tail +15 /etc/termcap

will show the contents of the file /ete/termcap from line 15 onwards. You could
also use the more command to achieve the same result

% more +15 /etc/termcap

However there is a difference between these two commands. When you use
more, the display will pause at the last line of the screen with the usual prompt,
and pressing the spacebar will show you another screenful. The tail command

ACME/Gulshan Soni
Unit-5 22

will show the rest of the file without a pause, unless you use ^S and ^Q to stop
and start the display.

FILE PERMISSION

It is now time for us to explore what file permissions are and what effect they
have on its accessibility. You can see the file permissions for a file by saying

% ls -1
drwxr-xr- 2 kumarr users 32 June 19 crypt
x 23:04
drwxr-xr- 2 kumarr users 32 June 19 learn
x 23:04
drwxr-xr- 2 kumarr users 32 June 19 nlp
x 23:04

As you already know, the first column of the first field has a 'd' in it if the file is a
directory. So we can see that crypt, learn and nlp are all directories. Then you
see 9 columns which specify the file permissions. The user community in UNIX is
divided into three categories- owner, group and others. The owner is the person
who first creates the file. Several users at an installation can be made part of a
user group. Such a facility is useful in keeping people working on the same
project categorised together, and UNIX was first conceived of as an operating
system which would allow groups of programmers to work together and share
information. All group members form the second category of users. Finally the
rest of the community is lumped under others, which are users who are neither
the owner nor part of the same group. In the example shown, the owner of the
directories is kumarr and he belongs to the group users. Other users like khanz
might also belong to this group.

Having understood this categorisation of UNIX users, you can now begin to study
the permission modes. Every file has three possible modes of access-- read,
write and execute, represented in the directory listing by r, w and x respectively.
A file to which you have read access can be read by you, which means you can
look at its contents by using any method like the cat command. If you have write
access to a file, you can alter its contents. Execute permission is relevant in the
case of executable files like those that we have seen in /bin, or for shell scripts,
which we will study later. You can run or execute a file only if you have execute
permission on it. Execute permission does not mean anything in the case of text
files, nor actually for any file that is not an executable, except for directories
where this permission has another meaning.

Now you know enough to understand the permission information. 'Me nine
columns are divided into three parts- owner, group and others - of three columns
each. The permissions are specified in the order read, write and execute. If a
permission is available, the corresponding letter is shown, while the absence of a

ACME/Gulshan Soni
Unit-5 23

permission is indicated by a hyphen. So rwx means the category concerned can


read the file, can write to it or alter its contents and can execute, the program
which the file contains r-x means the file can be read or executed but not altered
or written to, because write permission is absent. r- means the file can only be
read, and -x means it can only be executed. - - means there are no permissions
available and such a file cannot be accessed at all. However you can still see an
ordinary file like this listed in a directory listing taken in the usual way. So you see
that the permission columns in the listing shown above mean that the owner has
read, write and execute permission, whereas oilier members of his group have
read and execute permission but no write permission. Similarly others, that is,
users who are not part of the owner's group also have read and execute but no
write permission. Thus if you want to have all the permissions on a file, while
denying group members write permission and allowing others only execute
permission, the permission modes should be rwxr-x----x.

With this knowledge you should again look- at the UNIX system files and study
the owners and permission bits of each. You will find that all users have execute
permission on the files containing system commands. This is obviously
necessary because otherwise you could not use those commands. If possible,
ask your system administrator to remove your execute permission on the Is
command for a short while and then try to see your directory listing. If you are at
a large installation where this kind of thing might not be possible, you can either
wait until you know more about UNIX to be able to see the effects of the absence
of permissions, or ask somebody to make a copy of a system command in your
directory, remove execute permissions on it and try to run it from you directory,
taking care that you do not execute the system version of that command.

So far we have talked only about ordinary files and what file permissions mean in
their case. But directories are also files, as you have seen earlier. Do the
permissions all have the same meaning where directories are concerned? If so,
what is it like to execute a directory? Let us delve a bit into this and find out some
answers.

First of all You must understand that since directories are special kinds of files
which contain information about other files, the permission bits have somewhat
different meanings than what a hasty guess would suggest. Before we even look
at what read permission for a directory means, make sure you have done the
exercise (1) of section 2.3.5, which asked you to cat a directory and report what
you saw.

If you tried the cat command on a directory which has some files you will see a
long line of words, at least some of which you should be able to identify as
names of files in the directory. So you see that the contents of a directory are the
names of files under it.

Now it is easy to deduce what read permission on a directory could mean. If you

ACME/Gulshan Soni
Unit-5 24

can read a directory you can cat it and it follows that you can do an Is on the
directory. In the absence of read Permission you will not be able to look at the
directory listing.

What about write Permission? If you have write permission on a file you can
change its contents. In the same way having write Permission on a directory
allows you to change its contents. What does that mean? Creating, renaming or
removing files would mean altering the contents of that directory. So it follows
that having write permission on a directory enables you to create, rename or
delete files in that directory

Beginners often find it a bit difficult to grasp this point though it is not really hard
to understand. You should remember that having write permission on an ordinary
file allows you to change the contents of the file but does not allow you to delete
or rename the file. That is possible only. if you have write permission on the
directory containing the file. Later we will see how this fact could create a security
lapse in some situations.

Lastly, coming to execute permission you would agree that there is no way you
could execute a directory corresponding to the normal sense that the operation
refers to for an ordinary file. For a directory this permission bit determines
whether you can cd to the directory or can copy files from that directory (this only
if you have read permission on the directory as well).

This permission is often called search permission. To be able to cd to any


directory you must have search permission on every component of the absolute
pathname of the directory. If search permission is absent on any component, all
files and directories on that component and below it become inaccessible.

Apart from these permissions there are some other permission modes which You
will come across. We will take these up in the unit on system administration. For
now it will be sufficient to know that some other permission modes exist so that
you do not get taken aback if you find characters other than r, w or x in- the
permission modes of a directory listing.

BASIC OPERATIONS ON FILES

We have covered a fair amount of ground in UNIX but we still do not know how to
create or remove files. We will look at some basic file operations in this section.
You will then at last be able to have some files of your own in your directory
instead of having to rely on system directories to be able to see a non- blank
directory listing.

Copying Files

ACME/Gulshan Soni
Unit-5 25

Until you learn about text editors in a later unit in this block, you will have to make
files of your own by making a copy of an existing file or by running a program wh
ich creates a file. Let us now study how to make a copy of a file. Make a directory
learn in your home directory and cd to it. Now say

% cp/ etc/passwd passwords

and look at your directory listing. You will find that you are the proud owner of a
file called passwords, which gets created in the current directory. This file will be
an exact copy of the original file /etc/passwd. You can now look at the contents of
both files and generally verify that this is probably the case, although you will not
be able to easily do an exact match visually and you do not yet know of utilities
which can do it for you. For the time being you can try copying short files to verify
that it seems to be so.

The cp command in this form takes two arguments of which the first is the
source, or the file of which a copy is to be made. The second argument is the
destination, or the name of the file which is to hold the copy. The names of the
files can be specified as absolute or relative pathnames and the current directory
is the default, which is why in the example above the new file got created in the
current directory.

Here we copied the file into our own directory with a different name, but we could
have also given a command like

% cp /etc/group group

This will create a copy of /etc/group in the current directory and the copy will also
be called group. Another way of doing this is to omit the name of the destination
file and specify only the destination directory. In such a case the copy is given
the same name as the original. Thus

% cp /etc/motd .

or

% cp /etc/termcap ~

will create files called motd and termcap in the current and home directories
respectively.

You can copy several files from several other directories into a single directory by
enumerating all the source files and giving the destination directory name at the
end. When you use this method the names of the files are preserved, that is, the
copies have the same names as the originals. Thus

ACME/Gulshan Soni
Unit-5 26

% cp /etc/issue /etc/rc /usr/include/stdio.h.

will create files called issue, rc and stdio.h in the current directory.

So far we have looked at cases where there was no file with the same name as
the target already present. What would happen if a file with the same name as
the target did exist? In keeping with the general philosophy of UNIX, which is to
silently obey whatever commands the user gives (thereby respecting the
intelligence of the user and giving him credit for being responsible enough to
know what is good for him), the target will be unceremoniously overwritten by a
copy of the source. So before issuing the cp command you must ensure that this
is what you want, otherwise you will have lost the original contents of the target
file.

Let us now see some reasons which can cause cp to run into a problem. One is if
you try to create a file in a directory where you do not have write permission, or
when you try to overwrite an existing file on which you lack write permission. cp
will then complain that it cannot create the target. Another problem could be the
absence of read permission on the source, or running out of disk space while
copying.

The cp command preserves the permission modes of the original but changes
the owner and group to that of the user making the copy. Also there is regrettably
no straightforward way of recursively copying files from subdirectories under a
directory.

Renaming Files

We shall now look at the question of changing the names of existing files. Look at
your learn directory with

% Is
-X
group issue motd passwords rc stdio.h termcap

These are the files we copied into this directory in the previous sub-section. To
rename motd to headline say

% my motd headline

% Is -X

You can cheek the contents of headline to satisfy yourself that they have not
changed. As you can see from the directory listing, the original filename no
longer exists.

ACME/Gulshan Soni
Unit-5 27

Lest you wonder why the command is called my for move rather than something
like ren for rename, the usage we have seen so far is only one aspect of the my
command. The primary purpose of this command is to move files from one
location to another. AS a side effect it is possible to change the names of files in
this process. If the move is done within the same directory this operation is
meaningful only if the name is changed, which is of course the same as
renaming the file.

The capabilities of this command are similar to cp, except that the original or
source disappears when the command completes. If the destination of the move
is a different directory you can move several files at a time. Try this out by
making another directory of your own

% mkdir ~/learn/learn

% my ~/learn/group ~/learn/headline ~/learn/learn

You will find that the files group and headline are no longer present in ~/learn but
are now in ~/learn/learn.

If a move is being done within the same file system the my command is quite
quick because not much disk activity is required, irrespective of the amount of
data in the files. Also there is no danger of the command failing for lack of space
because no extra space is required on the disk. However the picture changes
when the move is being done across file systems. Since the super block and the
i-node table are different (you will learn about these in the unit on system
administration) for different file systems, a file can be moved from on file system
to another only by copying it to its destination and then deleting the original. This
is just how mv works.

Therefore in such a case you can find that my fails because of lack of space in
the destination file system. Moreover the command will take longer to execute
because data has to be physically copied to the destination file system. But there
is no danger of losing the original if the command fails because the original file is
deleted only after the copy to the destination has been completed successfully.

As with the cp command if the new name is the name of an existing file that file
gets silently overwritten by the new file. So be careful on this count.

Deleting Files

By now you have acquired a sprinkling of files in your work area, and it is time
you learnt-to delete files you no longer require. This can be done using the rm
command

% rm termc-2n

ACME/Gulshan Soni
Unit-5 28

If you look at your, diectory listing You can see that the file termcap is now gone.

The rm command takes any number Of arguments and deletes all files specified
in them. The files can, as usual, be specified using absolute or relative
pathnames. However the command will refuse to touch directories. So if you say

% cd; rm learn

rm: learn directory

UNIX informs You that learn is a directory and leaves it intact. If You do not have
write Permission in a directory you cannot remove any files from it and the
command will fail. But if only the file is write protected rm tells you the permission
modes of the file and asks you to confirm whether you really want to delete it.

% rm Protected

rm: Protected 444 mode? (y/n)

Now if you type any response starting with a y (for yes) the file will be deleted
after you Press the RETURN key.-Any Other response will leave the file as it is.

A related option to rm is the -i option which tells rm to Print the name of the file
and wait for Your Confirmation before deleting it. But now the mode of the file is
not shown. The converse of this Option is the -f option (for force) which tells rm to
delete the file silently without asking any questions, irrespective of the
Permissions. This Of course does not mean that you can delete files without
having the appropriate access rights, only that files you can delete will be deleted
without any further reference to you.

At least in the beginning while you are new to UNIX it is wise to use the -i
(interactive) option to rm, lest you inadvertently lose important data because of a
command wrongly given. Remember that in UNIX a file once deleted cannot be
recovered- it is gone for ever.

Now let us look at a very powerful form of the rm command. The - r (recursive)
option deletes all files in the directory specified and recursively in all
subdirectories of the directory, including the subdirectories themselves. In Other
words it cleans everything from under that directory including the directory itself.
Thus it is far More powerful than rmdir, which refuses to destroy non-empty
directories. In one command you can destroy the entire directory tree using the r-
option

% rm -r ~

ACME/Gulshan Soni
Unit-5 29

will wipe out everything under your home directory. At this point you could afford
it because you had not laboured much to create whatever files you had. But
imagine a situation on where you wipe out months of effort at one stroke, So be
very careful while using this option. The most sweeping form of the rm Command
is

% rm -r /

which will wipe out clean out everything on the disk except any unmounted file
systems. Since usually all partitions are mounted at boot time, you will end up
wiping out he entire installation including the rm command and the UNIX
operating system itself (see the unit on system administration for an explanation
of the mount command). Even if you have a complete backup, it will take a lot of
effort to restore the installation to its original state. Therefore you must be very
careful indeed when using rm - r. We see no situation where you might need to
use rm -r /. Removing everything on the disk can be done more thoroughly other
means.

You are now encouraged to try out rm r / on your installation as an ordinary user!
Why do we contradict ourselves in the space of two sentences, you might
wonder. The point is that rm - r can and will delete only those files which the
permission modes allow. While the superuser can delete any file irrespective of
permissions, an ordinary user will not be able to cause any damage to an
installation which has been carried out properly. If you are able to cause any
problems to others or to any other part of the installation it is a signal to the
system administrator that all is not right with security at the installation. But do
not do this as superuser unless you know you can reinstall UNIX on you
computer!

Links between Files

As we have said before when ~g about the Is command it is possible for a file to
have more than one name. If there is a file in your home directory called motd,
you can give it another name like this

.% In motd headline

Now if you look at the contents of the files you will see that they are the same.
You can create a link between the two file names by using this command. If you
wish you can create more links subject to a system imposed limit. Of course one
rarely has occasion to have more than a few links to a file. You can link a file to
an existing filename too

% In -f password headline

Now if you look at headline it will have the same contents as password rather

ACME/Gulshan Soni
Unit-5 30

than motd as was the case till now. Actually headline and password are now two
different names for the same file. The name headline has got detached from
motd and has got attached or linked to password.

You cannot link files across two different file systems. This is because the super
block, which stores file system information, is different in the two. However later
versions of UNIX permit what are called symbolic links between files in different
file systems, but we will not look at them in this block. The links we have seen
here are also called hard links as opposed to the symbolic links mentioned. Let
us now create a few more links to headline

% In headline sensation newsworthy


Study the directory listing now.
% Is -1
-rwxr-r- 4 users 1690 June 27 headline
kumarr 03:09
-rwxr-r- 4 users 1690 June 27 newsworthy
kumarr 03:09
-rwxr-r- 4 users 1690 June 27 passwd
kumarr 03:09
-rwxr-r- 4 users 1690 June 27 sensation
kumarr 03:09

Although these look like four different files, the second field in the listing tells you
that each of them has 4-links. This does not however mean that they are linked
to one another. For the moment you can assume this because we just created
the links ourselves.

What is the difference between creating links and making copies of files? When
you create a copy of a file, there is physical copying of the data in the source file
to the target. So some disk space will be used up in storing the Copy. Also, if you
now alter die source, the target is not affected, and vice versa because they are
two different, independent flies. But if two files are linked there is only one
physical copy of the data on the disk. Altering one file automatically changes the
other one because there is actually only the one file although it has two different
names. Extra disk space is not required to store the linked file.

So if you now delete the files above one by one and keep observing the directory
listing you will find that the number of links keeps reducing by one in all the
remaining names. When the last link to a file is removed the file gets deleted
because there is no name by which it can be accessed, and the disk blocks it
occupied eventually get reallocated to other files.

Can you now look at the listing of cp, mv and rm in the /bin directory? You will

ACME/Gulshan Soni
Unit-5 31

find that each of these executable files has three links. Actually the code which
performs all these three functions is the same. Depending on the name the
executable was invoked with. the code performs somewhat different tasks. You
have only to recall that mv has to do both a cp and an rm if the move is across
different file systems, and the close relation between these three commands
becomes at once apparent.

CHANGING PERMISSION MODES

We will now see how to change the permission modes of files and directories. So
far we have had occasion to look at various commands, many of which have to
do with files of various types. We have seen that the permissions on the files
make a great difference to the actions of these commands. For example the cat
command will usually type a file on the terminal but will refuse to do so if the user
does not have permission to read the file. So we can keep our files protected
from ordinary users if wish or need to do so by changing the permissions on it
appropriately. The command to do this is chmod. The permissions on a file can
be changed only by the owner of the file or by the super user. The owner, as you
have already seen, is the user who created the file. This creation can be by any
means, for example, by using a text editor, through a user written program or by
simply copying an existing file. Note that having permissions on a file does not
amount to owning it. If you can read somebody else's file, it does not mean that
you can prevent others from doing the same, but you can establish such
protection for a file of your own. Also do not get confused between the original
file and a copy you might have created, having only read access to the source
file. You can change your copy in any fashion you wish, but you will not be able
to alter the original.

There are two forms in which you can use the chmod command. Let us look at
the absolute method first, as It is slightly easier to understand and use. In this the
permission mode desired for the files is given to the command in an octal
notation which we will explain shortly. The mode of the file then gets changed to
what was asked irrespective of the permissions before the command was run.
The form of the command is thus

% chmod mode filename

where filename is a list of one or more files whose permission modes are to be
set to mode. If the permission bits are mode to start with there is no effect on the
file or files after running the chmod command.

You know that file permissions are specified by 9 columns, for example rwxr-xr-x
or rw-r---r-. In the absolute method the presence of a permission is indicated by a
1 and its absence by a 0. The resulting 9 bit binary number is then converted to
octal. This octal number is what has to be specified as mode for the chmod
command.

ACME/Gulshan Soni
Unit-5 32

You know that a binary number can be easily converted to octal by making
groups of 3 bits starting from the right. Now convert each group into octal as if it
were a single number. The resulting string of octal digits is the number in octal.
Thus

rwxr-x-x

can be written in binary as111101001 after replacing each permission by a land


each hyphen by a 0. This binary number can be written as111 101 001 after
grouping the bits in threes. The octal form of the number is thus 75 1. So to
convert a file to this mode say

% chmod 755 progfile

This will give progfile the pennissions rwxr-x-x. Likewise rw- r--r- in octal is 644.
So you can provide these permissions to your file motd by saying

% chmod 644 motd

Instead of one file you can set the permissions on several files at the same time
(all to the same value) by listing the files after the mode. So

% chmod 600 motd passwd

will make their permissions rw-----. Thus you can read these files or change them
whereas nobody else (except root) can even read them. So

% chmod 0 passwd

will mean nobody has any permissions on the file passwd and even you will not
be able to read a file of your own with such a set of permissions. However you
can change the permissions anytime since you own your file. Also the super user
can change the permissions of any file.

Let us now look at the symbolic method of telling chmod the mode. Here the
permission types are, as always, r, w and x. In addition there is a set of
characters which specify the target of the actions. The targets can be u (users), g
(group), 0 (others) or a (all of these). The actions are + to add a permission, = to
set it absolutely and - to remove a permission. So you can say (there must be no
spaces in the mode argument)

% chmod a+x progfile

to allow everybody to run progfile, irrespective of the earlier execute permissions


on it However this is different from saying

ACME/Gulshan Soni
Unit-5 33

% chmod 1 1 1 progfile

because this would remove read or write permission for everybody, whereas in
the earlier case, those permissions would have been left untouched. If the owner
had read, write and execute permission, he would retain it. If the group earlier
had read and execute permission it would continue to have that privilege. If
others had no permission they would acquire execute permission. One can
remove read and write permissions for others by saying

% chmod o-rw progfile

one can specify absolute permissions by saying

% chmod u=rwx,g=rxo=x progfile

Here the different target categories are given different permissions on progfile by
separating them with commas. No spaces should be present in the argument,
otherwise only the first part will be taken as the desired mode. The portion after
the space will be treated as a file name which has to be assigned those
permissions.

Most people find the numerical way easier to use. However if one does not want
to alter some permission bits, then there is no straightforward alternative to using
the symbolic mode. For example, if you want to deny others any permissions on
a file but do not want to alter your own or your group's permissions, you can say

% chmod o-rwx progrile

But to achieve the same result using the absolute method you would have to first
determine the existing permissions. Suppose the value is 644. You will now have
to say

% chmod 640 progfile

If the initial permissions were 666, you need to say

% chmod 660 progfile

instead. If using the symbolic method you would not need to worry. 'Me
command you need to give remains unchanged since the o action leaves the u
and g permissions intact.

STANDARD FILES

ACME/Gulshan Soni
Unit-5 34

We have seen quite a few UNIX commands by now, and you must have
observed that many commands produce or can produce output on the, terminal
screen. Likewise many commands can take input from the keyboard. Actually
these commands have been written to accept input from a standard input file and
to produce output in a standard output file. Usually these standard files are set to
the keyboard and the terminal screen respectively. Let us look at this in
somewhat more detail by studying some examples.

Standard Output

If you make a list of commands you have leamt so far you will find that many of
them produce some output For instance let us say

% cal

which Prints the calendar for the current month and year on the screen. In
practice there are very few commands designed to produce output on the screen
specifically. The programs are written to Produce output on what is called the
standard Output, and UNIX sets the standard Output to be the screen by default.
That is how the output happens to appear on the terminal.

The shell, which interprets all your commands and passes them onto the UNIX
kernel for execution, has a facility to alter the standard output. In other words,
you can define a file, rather than the screen, to be your standard output. (The
terminal screen is also a files as far as UNIX is concerned, but we will look at this
only in the unit on system administration.) To do this you need to say

% cal calfile

There can be zero or more Spaces before and after the sign. This sign indicates
that the standard output of the command preceding it should go to the file
specified to its right rather than to the terminal screen. This is called redirecting
the standard output. In the present case the calendar for the current month will
be placed in the file calfile. You can verify this by

% cat calfile

although You could have redirected this Output as well

% cat calfile catfile

Is that not away Of copying calfile to catfile? Note that the file to which output
gets redirected gets overwritten if it already exists, although it is possible to
arrange matters such that a command which would have overwritten an existing
file doesnot get executed. When the shell sees the sign it first creates an empty
(zero byte) file with the name given on the right of the sign. If such a file exists it

ACME/Gulshan Soni
Unit-5 35

gets truncated to zero bytes. You can verify this easily by

% Is- I Isfile

If you now examine Isfile by using the cat command, you will see a 0 byte file
called Isfile shown as one of the files in the current directory. This is because a
zero byte Isfile was created before the Is- I was run

% cal 06 1994 calfile

Now calfile will contain the calendars for the current month as well as June 1994.
Compare this with

% cal 06 1994 calfile

which leaves only the calendar for June 1994 in calfile.

Thus the sign is safer to use because it never destroys any data, but this
operation will keep adding to the file, and it can sometimes be difficult to make
out what part of the output was produced by your last command and which
portion is the outcome of previous redirections or was simply the original
contents of the file

Standard Input

Just as many command produce output on the screen, some Commands take
input from the keyboard although most take input from files. Look at an aspect of
the cat command you have not studied so far

% cat

The result of this is deafening silence. The uninitiated might wait several minutes
before aborting the command, thinking there is something wrong because the
system does not appear to be doing anything at all. The truth is that cat can take
its input both from the standard input as well as from a file. However the output is
always produced on the standard output. If any filenames are specified they are
used as the input but if none is mentioned the input is taken from the standard
input. There are also some commands which take input only from the standard
input.

In the present case no filename has been specified and cat is waiting for input
from the standard input, the keyboard here. So if you type something cat writes it
out to the standard output and the effect is that of echoing your input.

A foolish consistency is the hobgoblin Of little minds - Emerson

ACME/Gulshan Soni
Unit-5 36

A foolish consistency is the hobgoblin of little minds - Emerson

(Actually if you had given the command just as shown then the above is not
strictly correct. The cat command will buffer your input and when that buffer is full
it will straightaway write it out onto the standard output. So you will probably find
that you have to type several lines of text before you see it again on the screen.
However if you say

% cat -u

the result will be just as described, for the -u flag calls up cat in unbuffered
mode.) If you want to put an end to your misery you can terminate your input file
by saying ^d, thereby causing cat to finish and present you with your prompt.

To redirect standard input, say

% cat catfilesrc

whereupon cat will print the contents on the screen. This is just the same as

% cat catfilesrc

because cat can take its input from a file as well. So to copy this file to
catfiletarget, you can say

% cat catfilesrc catfiletarget

or

% cat catfilesrc catfiletarget

Thus you can redirect both standard input and standard output in the same
command. Some commands do not take input from the standard input. In such
cases redirection of the input is not possible, as with the Is, cp, mv, rm or who
commands.

Standard Error

So far we have seen the effect of redirecting the output of some commands that
completed successfully. Pet us look at this a bit more closely. For example, if
there is no command like gah, say

% gah gahfile

If you do so you will find that you get a protest message from UNIX on the
terminal but that gahfile is empty. Similarly

ACME/Gulshan Soni
Unit-5 37

% Is -1 gah Isfile

produces a message on the terminal but nothing in Isfile. Why does the
redirection fail? After all the command did produce output.

The reason is that there is a third standard file in UNIX, called the standard error.
UNIX programs and utilities are usually designed to provide error messages in
case there is something wrong and the program is not able to proceed as
expected. Such messages are often referred to as diagnostic output because
they can help the user diagnose the reason for failure. This kind of output is
usually written to the standard error file. Usually the standard error is also
connected to the terminal by default, but like the standard input or output, the
standard error can also be, redirected. To do this in the C-shell say

% gah &gahfile

This will place both the standard output and the standard error in gahfilc. Here it
will have only the error message telling you that there is no file called gah. How
do you place the standard output and standard error in different files? Well, this is
easy to do in the Bourne or Korn shells, but in the C-shell the way to achieve this
is somewhat convoluted. So we will not look at it right now. You are referred to
unit 4 on shell programming for this.

Filters and Pipelines

A filter is a command which can take its input from the standard input and can
produce output on the standard output. Having the capability to read from or write
to files is not a disqualification. So Is is not a filter because it does not read from
the standard input but cat is one because it can do so (although it can read from
a file as well) and also writes to the standard output.

You can think of a filter as a "device" placed between the standard input and the
standard output which filters the standard input before placing it on the standard
output. In the case of cat there is no filtering action at all, but a command like
grep does perform some weeding action on its output

The standard output of a command can serve as the standard output of another.
Several commands can be chained together like this. Such an arrangement is
called a pipeline. Pipelines are one of the big strengths of UNIX, because they
often enable us to group several existing commands quickly to perform a task for
which there is no command directly available.

A major design goal Of UNIX was to have an operating system which allowed
easy sharing of data and programs, and allowed people to build on the work of
others instead of having to do things from scratch. The facility of pipelining helps

ACME/Gulshan Soni
Unit-5 38

meet this goal because you can piece together commands written by different
people to achieve your objective rather than wasting your time on doing things
which have already been done. Let us take a simple example.

Suppose you want to find out how many of the files in a directory are directories
rather than ordinary files. It would have been wonderful if there were an option to
Is which did this job, but since that is not the case we will have to try something
else. One way is to look at the listing with Is -p and count lines which end in 1.
Such a visual method is tedious and prone to error especially if there are many
files in the directory. So let us try to make UNIX do this for us. How about the
following?

%IS -p tmp

% grep -c'$/' tmp

We first get the listing in a temporary file tmp and then count the number of
occurrences of / at the end of a line in tmp using the grep command. The result
will be available on the standard output While this method will work it has a few
disadvantages. One is that it is slow because an intermediate file has to be
created. Secondly we cannot start the grep command before the Is finishes. Also
if we run many commands like this we will be left with temporary files which we
will have to meticulously delete lest they clutter up our directory listing and
otherwise waste disk space. So we can use a pipeline like this

% IS -p 1 grep -c '$/' tmp

The 1 symbol is the pipe character. It means that the standard output of Is -p is
passed to grep. The act of connecting the standard output of a command to the
standard input of another is also referred to as piping the output of the first
command to the second. Here no temporary files need to be created or cleared
up by the user as UNIX itself takes care of the details. Also the speed improves
because the subsequent commands can start as soon as some data is available
to them.

A command like Is which does not take its input from the standard input can only
be the first command in a pipeline. Similarly a command which does not write to
the standard output can only be the last command in a pipeline. Also it is the
user's responsibility to see that each command receives input in a form which it
can meaningfully transform, otherwise the results will be gibberish. Thus do not
pass data files other than text files to grep because grep works only with text
files, with lines delimited by the newline character.

PROCESSES

ACME/Gulshan Soni
Unit-5 39

As you know, UNIX is a multi user, multi tasking operating system. This means
that several users can work on a UNIX system at the same time. Also each user
can be performing more than one task, or process at a time. A process can be
loosely thought of as a running program. Every process is thus a program but
only programs which ire running at a particular instant are processes. Every
instance of a running program is a process, so that a single program can be
running as several processes in different states. Processes have a life, they are
created, they run and finally terminate or die.

So far we have looked at processes running in the foreground. An example is the


C-shell which you invoke on logging in. It enables you to run other processes and
it presents you' with the prompt every time a process terminates. However such
foreground or synchronous processing is not the only option you have.

You can run any program asynchronously, that is, in the background by
terminating the command with an &. If you do so you are presented with the
prompt immediately and you can then start another process, in the foreground or
again in the background. Before that UNIX gives you a number called the
process identifier, the process-id or simply the pid. This is a number you can use
to find out about the state of that process later on. Try

% Is -1 &

This gives you back the prompt but you might miss it as well as the process-id
that UNIX displays because of the output which fills your screen. So running a
command in the background does not affect its output. Also if you have only a
few files in your directory this does not help you very much because the process
ends very soon anyway.

So it makes Sense to run a process in he background only when you expect it to


take a long time to complete and want to do somthing else in the meantime. If
you are going to get output make sure it goes to a file otherwise it will be mixed
up with the output of your background process. So you can say

% 18 -IR / dirfile &

which will give You a complete directory long listing Of Your insWla6on in dirfile.
If you logout before background Processes terminate they will all die but this can
be prevented by saying nohup before the command

% nohup IS -IR /

This will send the output of the command to the default file nohup out unless you
have specified some other file to take the output

% nohup Is -IR / dirfile

ACME/Gulshan Soni
Unit-5 40

But this still leaves the nohup command vulnerable to disconnection by an


interrupt, to prevent which you should give it in the background.

% nohup Is -IR / dirfile &

Now even if you logout the Process will continue until it terminates normally. Next
time you login you can examine dirfile, for your results. This is particularly useful
if you are connected to your machine over a telephone line and are vulnerable to
disconnections. Usually you would be well advised to redirect your standard error
as well, so that even if your command fails for some reason you can examine the
error messages it issued.

Beginners tend to go overboard with background processing because it is such a


wonderful facility. While there is nothing wrong in experimenting and learning by
trying out various things, you should pay heed to some general rules when You
are in a production environment. If you have started a process in the background
and the Output is going to a file, do not repeatedly examine the file to the
exclusion of other activity.

%IS -IR / dirfile &

% tail -f dirfile

If you find yourself doing this you might have perhaps have run the Is command
in the foreground. Running it in the background certainly has the advantage of
being able to do something else anytime you wish, an your process is not
vulnerable to interruption by any accidental depression of the DELETE key.
Secondly do not run too many background processes because every process
increase load on the system, and you must show consideration to other users.
Look up the nice command and use it to reduce the priority of lengthy processes
you run in the background. Of course how any is too many depends on the
machine and the other users. You should also make sure you do not set up race
conditions or otherwise issue a series of commands whose outcome depends on
the time taken for execution. For example look at

% prog Progfile &

% grep city progfile grepfile &

If prog Completes before you can issue the grep command, all is well. If prog
does not poduce any output before the grep starts, repfile will be empty. If the
execution speed of prog is somewhere in between, the contents of grepfile will
depend on this speed. So this is not a sequence of commands one should put in
the background one after the other. Start the grep only after the prog terminates
or put the two in a pipeline

ACME/Gulshan Soni
Unit-5 41

Finding Out About Processes

If you have run a command in the background and want to know whether it has
completed or not you can use the ps command.

% PS
PID TTY TIME CMD
1149 07 0:47 csh
1159 07 0:02 Is
1149 07 0:09 cc
1149 07 0:01 ps

This means that you have three commands running at the moment. The csh is
your login shell with process-id 1149 and started from terminal number 7. The
command has used up 47 seconds of computer time. There are three other
commands running and Is and cc are probably in the background. It is easy to
see that ps is always one of the commands running.

Thus one knows how many background processes one has set off, although
some of the processes ps shows might have been created by commands you ran
and not by you explicitly. This simple form of the ps command gives only the first
word of your command but will show all your processes including those you
might have started from some other terminal.

ps has quite a few options but we will look at only a few more. The -1 option
gives more information on each process while the -f option gives the full
command line including all arguments with which it was invoked. The -e option
shows all running processes instead of just your own.

If you run ps and find that a process you started in the background is no longer
listed, it means that the process has completed or that it aborted for some
reason. Also remember that ps gives a snapshot of the state of processes at
some time, and that by the time the output is displayed on the terminal matters
might have changed.

Stopping Background Processes

You know that a foreground process can be terminated by pressing the DELETE
key. But this will naturally not work for a process running in the background. To
stop such a process you need to know its pid, which you might have noted while
invoking the command or can deduce by examining the listing produced by ps.
Then just say

ACME/Gulshan Soni
Unit-5 42

% kill 1168

and that process will be stopped. If a process has created other child processes,
you need to find out their process-ids using ps and then give them all as
arguments to kill. Processes can be killed by passing them various signals with
numbers 1 to 15. If nothing is specified then 15 is the default signal number used
by kill. So the command above is the same as saying

% kill -15 1168

If you have many processes running and are desperate because you cannot find
out the process-ids, you can kill them all except your login shell by saying

% kill 0

Some commands are written such that they can catch signals and act in a
predetermined manner on receiving them rather than executing the default action
of terminating the process. The login shell is such a command. So

% kill 1149

has no effect. Such processes can be killed with signal number 9, which cannot
be caught or ignored. But

% kill -9 1149

will kill your login shell itself and you will be back at the login prompt.

ACME/Gulshan Soni

Вам также может понравиться