21
UNIT 2 UNIX-GETTING STARTED I Structure 2.0 Introduction 2.1 Objlcctives 2.2 Getting Started 2.2.1 User Names And Groups 2.2.2 Logging In 2.2.3 Correcling Typing Mistakes 2.2.4 Format of UNIX Commands 2.2.51 Changlng Your Password 2.2.6 Characters with Special Meaning 2.2.7 UNTIX Documentation 2.3 Files and Directories 2.3.1 Current Directory 2 3 . 2 Looking At The Directory Contents 2.3.3 Absolute and Relative Pathnames 2.3.4 Some UNIX Directories and Files 2.4 Summary 2.5 Model Answers 2.0 INTRODUCTION This unit introduces you to UNIX System V Release 3.2 (hereinafter referred to as UNIX) and tells you how to start working on your UNIX computer. A few elementary commands are all that ydu need to get the feel of what working in a UNIX environment is like. The auempt is to let you sce enough UNIX features to allow you to walk upto a computer running UNIX login, although in this block we will not touch upon the design of UNIX. This unit is oriented tbwards showing you the richness of the system and making you comfortable working in the environment. After all, apart from the academic and theoretical aspects, UNIX is i rich, open (if a little unfriendly and unforgiving) and otherwise convenient operating system which gives you a bewildering array of tools to help you be productive. 2.1 OBJECTIVES After going through this unit you shall be able : To learn how to start a login session undcr UNIX. To learn some basic concepts like the hierarchical directory structure. To learn about the various types of files under UNIX. To learn how to close a login session. We will now learn how to start using a UNIX computer. This unit will talk about the basic steps involved in sig&ng on to a system running UNIX and also what you can do cnce ycu have gained ingress. But remember thai you cannot learn UNIX merely by rcading this unlt or even this block. That might at best allow you some familiarity with ~e tcrminology uscd and might tell you something about its organisation. You might even come to know something about its features and the tools available undcr it. But you will not bc ablc to work on a UNI# 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 othcr supplemehtary reading material, together with the UNIX documentation, {ill be a valuablc aid in your voyage of discovery. So you must gain access to a terminal connected to a working UNIX machine and Q out whatcver you fcel like. Do no1 be afraid of exploring or making wistakcs. Whenever you lcam about a command or any othcr fcature, do not hesiutc to try out 011 its variations. Do riot confine yourself to only what is men~ioncd hcrc. This

spsa unit5 part1

Embed Size (px)

DESCRIPTION

This Post Is Only For CSE THIRD YEAR STUDENTS FRM WIT.....

Citation preview

Page 1: spsa unit5 part1

UNIT 2 UNIX-GETTING STARTED I

Structure 2.0 Introduction 2.1 Objlcctives 2.2 Getting Started

2.2.1 User Names And Groups 2.2.2 Logging In 2.2.3 Correcling Typing Mistakes 2.2.4 Format of UNIX Commands 2.2.51 Changlng Your Password 2.2.6 Characters with Special Meaning 2.2.7 UNTIX Documentation

2.3 Files and Directories 2.3.1 Current Directory 23.2 Looking At The Directory Contents 2.3.3 Absolute and Relative Pathnames 2.3.4 Some UNIX Directories and Files

2.4 Summary 2.5 Model Answers

2.0 INTRODUCTION

This unit introduces you to UNIX System V Release 3.2 (hereinafter referred to as UNIX) and tells you how to start working on your UNIX computer. A few elementary commands are all that ydu need to get the feel of what working in a UNIX environment is like. The auempt is to let you sce enough UNIX features to allow you to walk upto a computer running UNIX login, although in this block we will not touch upon the design of UNIX. This unit is oriented tbwards showing you the richness of the system and making you comfortable working in the environment. After all, apart from the academic and theoretical aspects, UNIX is i rich, open (if a little unfriendly and unforgiving) and otherwise convenient operating system which gives you a bewildering array of tools to help you be productive.

2.1 OBJECTIVES

After going through this unit you shall be able :

To learn how to start a login session undcr UNIX.

To learn some basic concepts like the hierarchical directory structure.

To learn about the various types of files under UNIX.

To learn how to close a login session.

We will now learn how to start using a UNIX computer. This unit will talk about the basic steps involved in sig&ng on to a system running UNIX and also what you can do cnce ycu have gained ingress. But remember thai you cannot learn UNIX merely by rcading this unlt or even this block. That might at best allow you some familiarity with ~e tcrminology uscd and might tell you something about its organisation. You might even come to know something about its features and the tools available undcr it. But you will not bc ablc to work on a UNI# 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 othcr supplemehtary reading material, together with the UNIX documentation, {ill be a valuablc aid in your voyage of discovery. So you must gain access to a terminal connected to a working UNIX machine and Q out whatcver you fcel like. Do no1 be afraid of exploring or making wistakcs. Whenever you lcam about a command or any othcr fcature, do not hesiutc to try out 011 its variations. Do riot confine yourself to only what is men~ioncd hcrc. This

Page 2: spsa unit5 part1

block is of ncccssity vcry bricf and can only scrvc as an introduction. Usc any othcr malcrial to which you havc acccss and cxpcrim~~nt lo your hcan's conlcnl. You will lcarn as much from your mistakcs and from sccing uncxpcclcd oulcomcs as from things you do by Lhc book.

2.2.1 User Names and Groups Evcry UNIX uscr is givcn a namc whcn hc is allowcd acccss LO a UNIX syslcm. This is also called an accounl, as in co~nmcrcial arrangcmcnls an accounl is kcpt of lhc usagc of h e machinc by cach uscr. The uscr namc nccd not have any rclnlion lo Lhc actual namc of lhe uscr, lhough il quilc oftcn is sonlc abbreviation of Lhc name. For cxamplc a pcrson callcd R~un Ku~nar would usually bc givcn a uscr namc kumarr on a UNIX system. This is forrncd by his surnamc (abbrcvialcd if i t is too long) and Lhc lirst lcttcr of his firs1 name. I t is q u i t possible for a pcrson lo havc morc than onc account on a singlc nlachinc (in a diffcrcnt namc) cspccially if thc pcrson uscs ~ h c machinc in morc lhan onc capacily. For cxamplc Ram Kui~la: mighl bc working on ~ w o programming projcc~s, in both of which hc is par1 of a t~::lrc " , i : i j i ; cryptography projccl hc niigh~ havc a namc crypt02 and on his nalural'.languagc ;,:.ot,:..k!i,g pr(,jccl hc lnighl 1i:lvc a nalnc like nlpOl. Thc rcason for Lhis kind of arrangcmcnt I;:!< ,:: !ii) with his acccss righls and privileges in his din'erent projects. Also if Ram Kumar (2;:: :Y thc company his succcssor Zafar Khan mighl conlinuc his work on nlp04 while s:),;!t:iiotiy clsc is assigncd LO work on crypl02. Onc of Lhc things tha1 molivalcd Lhc designers !:I' UNI S was ~hcir dcsirc for easy sharing of inform:llion, consistent wilh thc ~lccds of scc1:rit y and privacy. SO UNIX allows uscr n:lmcs Lo be groupcd togcthcr undcr a common C F O U ~ nsmc. A11 uscrs belonging lo thc samc group can shxc group privilcgcs. L,

Sonlc u\cr riamcs are rcscrvcd by UNlX for ils use, for cxamplc bin and uucp. So you cannot usc lhcsc namcs for yourself. Thcrc is also a spccial kind of uscr on cvcry n T I X syslcm who has all possiblc acccss rights on thc syslcm. This uscr is callcd thc supcr uscr, lhc syslcm administrator or simply rool bccausc that is lhc uscr namc conventionally allollcd LO him. For adni~nisualivc convcnicncc largc sysslcms can havc more Lhan one supcr uscr account. The supcr uscr is thc onc who can crcalc ncw uscr accounts, shutdown thc syslcm and pcrfonn oihcr mainlcnancc tasks.

You might bc wondcring why cvcrybody cannot acccss thc computer as root. The rcason is that whcn you are granlcd acccss lo a compulcr syslcm you arc assigncd a uscr name as wcll as a password. Yowcan scl your password to whatcvcr you want subjccl LO ccrtain consu;~inls..So you cannol entcr lhc compulcr as rool unlcss you know the root password. The root password is zealously prolcclcd on any wcll maintained installation as public knowledge of this password would compromisc thc sccurity of Ulc installation.

Whilc root can acccss all uscr filcs and ovcrride any syslc~~i protection mcant for mere mortals, nobody can figurc out what your password is. Howcvcr root can change or rcmove your password.

Check Your Progress 1 1. Can morc than one pcrson use the same uscr account on a UNIX systcm?

2. Can lhcre be morc than onc account wid1 thc samc name on a UNIX system?

:P 3. Can morc than onc uscr accounl have lhc samc password?

2.2.2 Logging in ki)u will now learn how to gain acccss Lo a UNIX systcln so that you can usc its facililics. This pl~occss is called logging in to thc computer. To bc able to login LO a machine you must l1:lvc a valid uscr accouni on it and you must know your passworcl. Your account woi~lti Ilavc l x c n crca~cti far you b y ll~e sysicln adniinislra~or whcn you wcrc allol~cd io u ~ c thc

Page 3: spsa unit5 part1

UNIX Opcratinz ~ y \ t r m - I computer. .4t that Lime you would also have been told your first password. Whcn you sce your terminal it would be displaying a message like

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 havc to press the return key for the computer to regis~er what you have typed. This key is sometimes labelled as ENTER. You will find that as you type on the termind scren you will be able to see whatevcr you have typcd. This is bccause UNIX usually echoes whatevcr yoi~ type on the terminal. So your screen should now look like this

IGNOU UNIX compu!?r

login: k~marr

Passwo~d:

You must type in yow user zarne, also called the login name, eracily as allocated by the super user. This is becausc UNTS is case sensitive, that is, it disringuishcs between lowcr case and uppcr case letters. In this respect it differs f r ~ m operxing systems like VMS. So be careful of small and capital ?cttcrs while working on UNIX.

When UNIX asks you for pour- pasiivord, key it in carcfufly. Notice iiut your pass.n.oid is liet echoed as you type..In fact ihr, c3rmr does not move at all. This i s to prevcnt somcbody from reading your password over you si.,oil!dcr, as that would cr.;lbEz Lj-ia! person 1.0 n:isqacradc: you by logging into the cornpszr iri your rlame and using i:.

UNIX now checks whether you x e 2 ..~!jli;j aser and wkcit;.-r ; ~ : . 3 e:i:r:rccJ ;~I:L':'!.

password. If there is any mist&<: :la:-: ~ c : a pne.ss.-zc sayi;;!: - - Login incorrect

login:

This means you can try to login agaii:. 7'1::;; car. hc olh -: :cn<or,s ;i.!:y you might not :-. - ' 2

to login even though you are a valid uscr ar,c did not make any typing mistakes. The messages you get in those situations will however bc difrsre~t.

Why be so pessimistic? Let us assume you have managed to login successfully. The syslctfi will then display some messages and finally give you a sign that it is now ready to obcy ; uur commands. The messages you see depend on how the systcm has been configured or sct up by the system administrator and by you. So you might not even see any messages. Howevcr 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. Thtse usually give some information about the systein 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 logi~. The mesgage

You havc mail.

means somecihe has sent you mail usirig the uscr to user com:l~uri,ication fzcilities a*.,ailab!c in bTiX (see unit 4).

After the login messages you see a pnmpt. which is the sign t!at Ij?i:X is rcady fcr your commands. The prompt can be changed to whatever yoc llke but t!c d:F?:il; pmmp! s l ~ o depends on what shell you have Seen assigned. O:ie of thc most co:-::nun silcil, is !kr S shell, which has the folio*ing prompt by dcfau!;

Page 4: spsa unit5 part1

I

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

UNIX Getting Stnrtod I

,.- r:; :fi;,.;i;;:, ,... ;:; t,,, l ! : ~ : j ! g t : : 2 ;+.iici>s ,. , I c.., .,~iic!iL >.,+ . (dii !:ot ask lliln fur his password). Are. you 2bi ; iy$ ;:..,:a ;)i.7,;;:;;;-:

i i '? ., . i'r.. :k)r!!_iir~g '-,., i:i : i i i~ ig :it[ i r < i i i i i i l \iiiir:h dixs no! exist on your systzm (confirm this from t , ?... ,. . sysccri-i : : : ! ~ i i ~ ~ l i t i a r ! ; : , ~ . 1.; rii;.:,: ri i~; dil'!c~.c:ir:e in the e~m;!u~cr's response from that

i ~ t :?jc i;i!,g c:<c<i:~<c, \i<i::,: (jL:. yu*,~) { f ~ i x ~ b : :l:is is so'! i

, .-. . 4. i - ~ : ' k i aii!i ~ ' i i t y ~ r l ~lbigial jaii w liigiri wc'n ri~ough you did nothing wrong vvhile wing to

. . it. : ,,;, ' L'ri. ;rcL ( i , icasl fi ,.e, y s :. . ~ ~ . ~ , ~ ! : l s ~

- - 2.2.3 Correcting Typing Mistakes Many of us are not professional typists and we make a lot of mistakes while typing. In any case a11 of us are human beings and are prone to error. Whether you are a one or two finger expert or know touch typewriting, you are going to mistype your commands some time or the other. What do yo0 do when you want to find out in a session whether you have any sundays left in the month? Normally you would use the cal command thus

% cal

Suppose now that by mistake you type

- % csl

After you press the return key UNIX will say

csl: Command not found

if you are lucky and a command csl does not exist. If it does it will be executed and you could well be in deep muble depending on what csl does.

You would therefore do better to cancel your command or correct your mistake. These actions can be accomplished by using the kill and erase characters respectively. The kill character cancels the entire'line you typed while me erase character erases or rubs out the last character.

Page 5: spsa unit5 part1

On most UNIX installations you can use the backspace ke)r:to erase the previous character. On some lerrninals the erase character is AH. This means that you have to type [I while holding dbwn the CONTROL key. This key is usually marked CTRL or CTL. I t is usually located on both sides of the keyboard near the Shift keys. In this block we will wnte AH to mean CONTROL-H and you must be careful not to confuse this with the two separate characters "(circumflex) and H.

Every time you press the erase key the cursor move's back one character after deleting the lastocharacter. So ,to correct your mistake

% csl

you shoulQ press the erase character twice so that you see

and then rdtype 'a' and '1' correctly. You can then press the ENTER key to run the cal command.

-The,line kill character tells UNIX to kill the line, that is, to ignore everything on the line. You do noti get the prompt after typing this character unless you press the RETURN key. The line kill character is usually @ but can be changed to something else.

There is a command called stty which enables you to see what the erase and line kill character ate. It also allows you to change them if you wish. The command allows you to examine and alter many other terminal settings as well, but for the moment we will not consider anything else other than erase and line kill. Just type in

% stty

and observe the output. It will, among other things, say something like

erase AH kill @

This mtans that your efise character is AH and your line kill character is @.

Suppose your want to change your kill character to AX. %u can do this by running the command

% stty Gill AX

Now typing I@ has no effect on the command you type other than putting an @ as part of your command. It no longer kills your command line. You can similarly set your erase character to k by saying

% stty erase #

Both the settings can be changed at one stroke by saying,

% stty kill @ erase AH

Any changes you make using stty will remain in effect only for the duration of your current login sasion or until you use the command to make more changes. Next time you login the characters revert to whatever the system administrator has configured them. You might wonder what will happen if you set the erase character to something like '1'. You can see this in the Bourne shell which has the default prompt of a $ sign. Try

$ stty ewse 1

and now try arming the cal command. You yill not bgable to convey cal to the computer because '1' is taken as an instruction to erase the previous character. It is for this reason that the erase and line kill characters are usually not set to any characters you use commonly as part of commands. Thus it is better not to set them to letters, digits or hyphens or even other commonly us$d special characters.

However if ydu insist on using '1' as your erase character you can still run the cal command ,

by typing

Page 6: spsa unit5 part1

The 'Y is called the escape character because it turns off any special meaning attached by the system to the character immediately after i t This act is called escaping the character. If the character immediately after 1' has no special meaning then the T has no effect. So you can type

which will have the same effect as

* CIi(.r,k Your Progress 3 1. ib' l lat arc the ckaractcrs you need to use to corrcct typing mistakes while logging in? Do

rhcsc dcpc~id on your crase or kill chxactcrs in your last login session?

..................................................................................................................... ! h i i ~ ) ~ u ill you enter a 1' as pxt of your command:'

3. 0:~l :,(!I: do aw:iy with the crssc or line kill characters altogether (that is, no character j 11:):; ~ h c : c:ii~i:i af crnse or iine kill)'?

.............. '............*.............................r.......................r....................................

4. Can you set hog1 crnsc and kiil to the s:me chwactcr?

5. What f:;~ppens if you sct vow crasc character to T? I-Tow do you cscape special i.f;xac. icrs' now:)

I 2.2.4 Format of UNIX Commands We will now look at the general format of UNIX commands and take tie opportunity to

, study some simple commands. Let us go back to the C shell and the cal command which we mentioned earlier.

S AD

% cal c

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

UNIX - Gdtlng Started I

Page 7: spsa unit5 part1

care is taken to see that the date is set correctly. The date in UNIX means date and time, so the outputaf the command is something like

Wed Jun 15 13:44:39 IST 1994

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

Another simple one word command is

% who

kumarr Uy03 Jun1511:49 - khanz uy05 Jun 15 10:22

n l f l tt y07 Jun 14 23:36 L

This tells you the names of the users currently logged in to the system, their terminal numbers and,he 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 machint. There is another form of the who command which you c q now try out.

kumarr uY03 Jun 15 11:49

This time we have given the arguments am i to the actual command who. The 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 tehinal, the terminal humber 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. 1% 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 mattended while you are logged in, as it would be a security lapse. Some versions of UMX provide the command

% 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, whish 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

Wed Jun 15 14:02:11 IST 1994

kumarr tty03 Jun 15 11:49

nlp04 tty07 Jun 14 23:36

crypt02 Uflg Jun 15 1357

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 filenahes (discussed later in this unit), options or

Page 8: spsa unit5 part1

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 payrollm

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.

Sometimes the shell places restrictions on the use of certain characters because it interprets them in some special way. To use these, characters in arguments, you have to use quotes. The details of the C-shell are discussed in unit 4.

Check Your Pmgress 4

1. Ii,)w can you ger the calendar for some other month in sorne other year?

2. I;:e the calcndar for the year 1752 and look at it.'Is anything the matter?

3. I:~nd out how lo set the system date. Why do you think only the super use is allowed to (10 this?

4. :i;udy he who command and use it to find the date the machine was started up, and also Ill.?w many uscrs are currently using your system.

- 2.2.5 Changing your Password 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 yol; art: asked to choose one for yoursclf the first time you login. This can be done with the co~iinand

% passwd

Changing password for kurnarr

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

Page 9: spsa unit5 part1

UNIX Operating system-1 entered here,ithe 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 qsked 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 dassworci 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 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 commhnd. 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 o w n , ~ l y if you know his current password. On your system you might simply get the following message if you try to change som~ebody 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 haw the super user can change your password to anything without knowing what it is currently.

Check Your Progress 5 1. Can you run the passwd command and again sct your password to what i t alrwdy is?

2. Can a fricnd (not the super user) hclp ~f you have forgotten your password?

........... ...................................................................................................................... A

Since your password is the only way of protecting your account, you must take care to choose passwords well, that is, choose one whictt cannot be easily guessed. As a general rule do not write ddwn your password anywhere and let it be locked up in your head. Do not choose the names of members of your family or close friends or your dog. In fact it is best to avoid all proper names and all the words in the dictionary. Let your password have a few special characters in it (not those which have anything to do with terminal settings). Some UNIX implementations enforce rules like these whcn you set your password.

2.2.6 Chara~cters with Special Meaning As mentioned before some characters are interpreted in a special way by the C-shell. These meanings will Be discussed in dctail in the next unit of this block, but that apart, there are certain charactors you will find to be useful.

For example, suppose y,ou start a command which takes a long time to execute, and you O

Page 10: spsa unit5 part1

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

UNIX - Cct4Ing Slarted I

% 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. Theoutput will probably be dumped on your terminal far too fast for you to read. To stop output on the screen temporarily, type AS. You can restart the output by typing "Q.

Another special character can be used to terminate your login session. This is AD, which indicates to the shell that there is not going to be any more input from you. 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

% exit

Llreck Iiour 17rogress 6 1 7 - l ~ comnizlnds ynil have learnt so far produce only a small amount of screcn outpur.

i i i )w will you p;oducc ontpi~t which docs not fit into one screen, using only thc ~ornrnands yo[, havc lcarnt so far?

2. 1 !ow does ihe BREAK chxxtcr differ from KILL?

3. (''an you transfer the functions of AS, AQ and AD to some other characters?

2.2.7 UNIX Documentation T I X comes with copious documentation; some of which is ufyn available on-line. You should learn how to use the UNIX manuals. While we will rlot discuss this ppic 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 ta consider all the options available with many of the commands that we do discuss. The u111y way for you to master them will be by consultiq the documentation.

If any documentation is available on-line at your installation, you can look up the manual L 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.

Page 11: spsa unit5 part1

UNlX Operating System-I

2.3 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 init at any given time. It is possible to change the data in a file. This act does not affect the name of the file. Thus UNfX commands can be made to operate on the data in a file as a group.

A file usually exists on the hard disk(s) of the computer. This will be the case when you are logged in to th6 machine and are engaged in a session. The actual areas sf 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 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 called the parent directory. Thus in UNIX the file system is like an inverted tree of directories, starting at 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.

2.3.1 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 your current directory is at any time by using the command

% pwd

This means that your current directory is called kumarr and is located under the directory usr, which is in turn locatzd under the root directory. Of course the actual home directory you are allotted wili depcni! on your installation. By the way pwd is one of thc fcw UNIX commands which do nct iake any arguments or options.

The otiipilt that pwd displays is called the full pathname of your currcnt working directory. Th:s is also known as the complete or absolute pathname, that is, thc 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 kumarr 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 (*/'I.

We have not yet talked of what a valid filcname can be. Actually in UNIX there are no restrictions and ii filename can have any characters upto a maximum of 14. The same rules apply to directoties 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 shcll.

Page 12: spsa unit5 part1

UNIX Getting Started I Check Your Progress 8 1. What would happen if your home directory did not exist and you tried to login?

2.3.2 Looking at the Directory Contents We will now sce how to look at the contents of a directory. The command is

This gives you a listing of all files in the current directory. If you have just been alloued 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.

1s has several options and it will take you some experimentatiwo understand them all. The first option we look at is

% Is -a

This is your first taste of UNIX options, so look at the command line carefully. The command 1s 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, 1s never lists such files in its output. The output of 1s 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 Iusr. The directory TI' 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 1s. Thus to look at the directory listing of the root directory use the command

% Is/

aardvark

bin

dev

etc

lib

lost+found

ID UnP

UST

We must caution you that it is very un!ikely thnt ycu 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. b

I As you have seen the 1s command lists one file per line of output. TO see several names per line you can use

t % Is -x

aardvark bin dev etc lib I ost+found

1 I

tmP usr

I Now the output is sorted from left to right on each line. Another variation is the -C option

Page 13: spsa unit5 part1

UNIX Operating System-I which sorts down each column

% 1s -C aardvark dev lib unp usr

bin etc lost+found

You might havc 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 1s.

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 filename which is a directory. The '/' is not part of the name, so do not get confused. For example

% 1s *Cp/

akudvmk dev/ lib/. tmp/ usr/

bid etc/ lost+foundl

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

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

% 1s -m/

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

This gives yoli 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 subdirectories, To see the contents of /usr, you can say

% 1s -xp /usr

bin/ khanz/ kumarrl lib/ unp/

On most systeins 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 diretories of the users. You can also see your own home directory here. But wait! When you logged in and checked the name with

% pwd

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 portian which is /usr. Thus the 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, whereaq the complete pathname of the latter is /usr/bin. You can now look at the contents of the othcr directories and try specifying their complete pathnames. Yoqncan also try lookirlg at their contents by providing relative pathnames. We will look at complete and relative pqhnames 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 no* get back to our friend the 1s command. One of the most useful and often used options is -1, qhich gives the so called long listing of the directories asked for

% 1s -1 / -rw$r-xr-x 1 root root 1298 May 14 09:26 aardvark

drwxlr-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 fiist 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 that position. So you now know

Page 14: spsa unit5 part1

anothcr way of telling whcher a file is a directory, apart corn h e -p anb -F oprions you have UNIX - Ccttlng Startcd I

already lookcd at. Thc o~hcr 9 columns in the first field tell you about he permissions on that file. We will look at hese in detail in section 2.4.6.

The ncxt Gcld in the output is a number indicating the number of links to the file. For a file this shows h e number of names it has. In UNIX the same physical data may have several names, although it must have at least one. Each narne 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 h e data in h e 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 s q a number like 207 instead of h e 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 fifh field is the size of the file in bytes. You alrcady 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 alarmcd because in most cases the intuitive meaning of size does hold good and the figures you see usually do represent h e number of bytes of data in the file in question.

The ncxt item of information is the date the file was last modified, and in the end the narne of the filc is shown.

You now know how to find out many useful things about the file. You should now look at the dirdctory long listing of h e various system and other directories on your machine. In the coursc of this when you look at /bin you will see many familiar names. For instance, who, pwd and 1s 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 Is command. When a directory is given as an argument to 1s you get to see the contents of the directory. But suppose you want to check the permissions on a directory, say /usr/kumarr. If yoi~ try

. you will see nothing because 1s tries to list the contcntq of the directory and at prcscnt there is nothing in your home directory. To see the desired output you could say

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

which lish hsr/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 w inside thcm arc not shown. To look at the contents of a directory and recursively of all

subdirectories within it, use -R

% 1s -R /usr l3 I

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

k i you car1 see every file and directory on your system.

Anc)Ll:t:r option is the reverse option. The -r option rcverses the sort-order of files displayed by Is. Ytiu can try this with any option

Page 15: spsa unit5 part1

Operating

tmP lost+found

lib

dev

bin

aardvark .

So far you have given only directories as arguments to Is, but you can give it an ordinary file as well. It theh lists only that file if it exists. Moreover you can give any number of files or direciories as arguments t~ 1s 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 1s and experiment with them. Many UNIX commands have zillions of option* getting used to them all requires time and effort. But you will find that you soon get to know the options you use often. It is probably best, when learning a new command, to comntrate on a few useful looking options onty. As you use them frequently you will get to know them well. Then you can spend some time deepening your knowledge of thecommand by trying out the other options. Most beginners get overwhelmed by the large number ofsptions 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.

C

Check Your Progress 9

1. Read up dn and try out the other oplions to 1s. What is the result of 1s -1ms! Which oplior~ takes precedence? What is the result of 1s -d?

.................. r . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . : . . . . . . . . . . . . . . . . . . . .

2. If your system has the -x or -C option set by default how can you get the s m d x d Is listing?

3. Your terminal most likely has an escape sequence to clear the screen. Find out what the sequence is and create a file whose name,contains the sequence. Thus if the secluer~ce is ESCL2.J. youran create a file callid 7aESC['tJbc. Now use 1s to try and look at the listing of your directory. What do you see? How can you Iook at the listing'!

4. If you see two files abcd and abcd in a dircctory !istir~g, that is, ~tvo iiles wilh apparently the sarrlc name, what would you conclude? How would ).ou confirm what you sunnise?

Page 16: spsa unit5 part1

. On a system which allows filenames of arbitrary length, would you rather store &ta in WIX - Getting Started I

the filename rather than in the file itself?

2.3.3 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 appvch is mare convenient. So you should take the aouble to assimilate the concept and learn how to navigate around the file system with felicity. Let us look at a typical directory h i m h y on a UNIX machine.

. r w l

I I I I 1

bin dev etc lib W

\ I 1

lib fmP

I 1

Of course the exact layout of the directory hierarchy on your machine is likely to be diierent. 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. 'lhis 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/Irumarr 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 1s command and provide the filename as an. argument to it. In UNIX you can provide a pathname (relative or 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

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 needto remember that '..' stands for the parent directory of the current directory '.'. So if you are at /usr/khanz, you can say

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

This is inefficient because you implicitly move to mot before naming the file. The first '..' takes you to Jusr and the second '..' takes you one level higher, to '/' or mot itself. Then you

Page 17: spsa unit5 part1

CNIX Operating system-I begiiyour debcent 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 filehame 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 diredtory, Thus if you are located in /usr/khanz and you want to specify a file in the directory /usrhumarr, it is easier to say ../kumarr rather than /usr/kumarr.

There is a third way of looking at the size of augcfg.C. For this you will have to l e m a new command cd, which lets you change your current directory. This command can be given an argument which is your intended destination and it then changes your directory to whzt you asked, providdd you have the appropriate permissions. An3 how do you specify your desired destination? Bly specifying h e pathnnme, of course. The pathnamecan be specified, as you would have ulldoubtedly guessed, either as a relative pathname or a complete one. So you can say from /usr/khanz

and then look Bt the size by

This really amounts to specifying the filename relative to /usr/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 --

% Is -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 &om the root directory. This is.

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

2.3.4 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 thf: functions of soMe of the system files. The UNIX directory structure is typically as shown in the earlier figur~.

We again emphasise that only some of the system directories are shown here. Your machine

Page 18: spsa unit5 part1

could have a somewhat different organisation. How will you find out the directory tree for your UNIX system? You can now explore the filcs 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 Is, 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 sizcs of executable files on your machine. These will depend, among other things, on the architecture of your computer.

The Idev directory contains device special files concerned with hardware devices like printcrs, terminals and hard disks. You will leam more about these files and the Idev diqctory later in this unit.

The /etc directory, as the name suggests, has several miscellaneous files and directories. It contains many commands which are r e s e ~ e d for the use of the system administrator. Ordinary users cannot execute many of these commands. Apart from this, the Ietc directory also contains some text filcs. Let us take a quick look at some of these text files. /etc/issue conbins the message before you login. Ietclmotd has the text of the message you see just aftcr you login. Ietclgroup has the names and group numbers of all the groups in the installation. /ctc/passwd contains the login name of each user, his user identification number. his encrypted password, his home dircctory, the dcfault shell when he logs in and other information about him. In some cases the password is storcd in anothcr filecalled- - Ietclshadow.

/lib contains system libraries used with your 'C' cornpilcr. Imp 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 erascd wilhout warning. So do not try to store anything here on a permanent basis. Keep files important to you under your home dircctory only.

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

Iusrlinclude contains header files used in writing C programs. /usr/games holds games distributcd with UNIX. This might not be present on some installations.

/usr/local/bin is often present as a repository of Iocal commands, often developed by local talent. These are commands of interest to and found convenicnt in that inseallation.

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 /lib and Iusrllib. Although we said earlier that UNIX places na restrictions on the characters you can use to consuuct a filename, there are some conventions followcd in a few cases. Usually filenames ending in '.xyz' are refenedto as '.xyz' f i l s or even as xyz files. Such conventions are not enforced by UNIX, although in =any cascs standard UNIX utilitics might do so. Thus h files are C or C++ program headcr files, C program files cnd 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 filcs cnd in '.o', library archive files in '.a', SCCS (Source Code Conuol System- to be discussed in unit 4) files start with 's.', 'p.' and so on.

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 ofeach. Although it is not hundred per cent reliable and is open to deceit, the command usually does a good job.

Cht ;k hour Progress 11 i. ,oak up thc UNIX documentauon for the various utilitics above and find out which of

Lticm enforce file naming conventions.

UNIX - Getting Started 1

9. Kur: h e rllc coinmar~d an various kinds of files from the various directories you have -,, ,.A " n and see IC ~llcir lypes are reportcd correctly.

Page 19: spsa unit5 part1

UNIX Operating System-I .................. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2.4 SUMMARY

In this unit we have started at the beginning and looked at many basic UNIX commands. However thee are many useful commands we have not been able to examine. You must refer to the manual and learn these. You should now be knowing enough about UNIX to conduct a session with ease. In the following units we will examine some more commands and look at some utilities in slight detail.

2.5 MODEL ANSWERS

Check Your Progress 1 , 1. Yes, any number of persons can use the same user account on a UNIX system. AUNIX

system does not try to identify a person physically and there is no constnint on more than one person using the same account. So as long as they all know the password of the account they can always use it.

2. No, there can be only one account under one name. In fact, it is the name of the account which defines the account

3. Yes, any dumber of accounts can have the same password.

Check Your Progress 2 1. The system thinks you are on a terminal which understands only capital letters and

responds in upper case, with actual upper case letters escaped by preceding them with a backslash (I) character. You can now type a AD to get back to the normal mode at the LOGIN: prompt.

2. You will r~ot be able to get the prompt unless you are able to guess his password. Thus, your own password should be such that it is not easy to guess.

3. There is no difference in the system's response. This is so that an intruder (who might not know for sure whether a particular account exists) does not get any information about the existence of an account unless he is able to loging.

4. Five possible reasons are:

(a) There might not be enough free disk space to let you login.

(b) The system wide limit on the number of processes that can be run concurrently might have bcen reached.

(c) The per user limit on the number of processes that can bk run might have been reached.

(d) The limit on the number of concurrent users laid down in the operating system software licence might have been already mched.

(e) There might be a hardware malfunction.

Check Your Progress 3 1. The usual characters can be used. These are not affected by the erase or kill characters in

your last login session.

2. By entering two T characters one after the other ('VY). The first 1' escapes the second, which thereupon loses its special meaning.

3. No model answer.

4. No model mswer.

Page 20: spsa unit5 part1

5. No model answer.

Clleck Your Progress 4

where mm represents the two digits of the month and yyyy the four digits of the year (in both cases leading zeroes can be omitted).

2. No model answer.

3. No model answer.

4. The command

% who -b

tells you when the system was booted. To see how many users are loggcd on, say

Check Your Progress 5 1. This will depend on the particular flavour of UNIX you an: using. Some versions allow

it while others might not. However the super user can set the password to anything he wants without rcslriction.

2. An ordinary user cannot help you if you have forgotten your password because he cannot look at or change your password unless he knows it alrcady.

Check Your Progress 6 1. No model answer.

2. The BREAK character terminates a process entirely, while the KILL character only cancels what was typcd by the user on a line, so that it is not sent to UNIX.

3. Yes, you can do so.

Check Your Progress 7

I . No modcl answer.

Clleck Your Progress 8 1. You would not be presented with the UNIX prompt and would be returncd to the login

prompt with the messagc:

No directory

Check Your Progress 9 1. No model answer.

2. No model answer.

3. The Is command is executed and thc files are listed as usual. As soon as the file 7aESC[2Jbc is to come, the screen gets cleared becausc it sees thc sequence to clear h e screcn. You can say 1s -b or 1s -? to look at the listing without h e characters getting interpreted. Some terminals might have a way of setting the hardware itsclf so that it docs not interprct control equcnces and displays them. But if you set your terminal to that, you will not be able to clear it atpll, bccause the effcct of the hardware setting is not limited to the duration of the 1s command.

4. This can happen if one or both of the files have non- printable characters embcdded. YO.L can again us Is -b or 1s -? to look at thc actual names of the two files, and those are guarante~d to be diffcrcnt.

5. No model answcr.

Check ,Your Progress 10

UNIX - Gctting Startcd I

Page 21: spsa unit5 part1

1. No model answer.

2. No model answer.

3. Yes, there is nothing to prevent you from directory of the same name.

Check Your Progress 11 1. No model answer.

2. No model answer.

-

having

-

a file (or

-

another directory)

- under a