![]() |
![]() |
|
|
Introduction to UNIX Part 1 |
|||
|
Julian Dermoudy This text remains © Julian Dermoudy and the University of Tasmania Computing Centre, 1992. Permission is granted for any individual to reproduce and distribute this text in whole or in part provided that this page accompanies the copy and that the copy is not sold. Note: the text has been modified to show the system in use at QUT. Introduction to Unix Part IContents1. The History of Unix 2. Computing Centre Resources 2.1 Computing Centre Machines 2.2 Computing Centre Software 3. The Unix File System 4. Logging In and Logging Out Exercise 5. Shells 5.1 What is a Shell? 5.2 Which Shell? 5.3 Shell variables 5.4 Logging In and Logging Out 5.5 Aliases 5.6 Filename Expansions 6. Basic Commands 6.1 Command Syntax 6.2 Obtaining Information on Commands Manuals The apropos Command The whatis Command The man Command 6.3 Information About People The finger Command The chfn Command The chsh Command The passwd Command The who Command The w Command 6.4 Negotiating the File System The cd Command The pwd Command The dirs Command 6.5 Directory Management The mkdir Command The rmdir Command 7. An Introduction to the vi Screen Editor Cursor movement Scrolling Searching Insertion Modification Deletion Revocation Miscellaneous Saving, Aborting, and Exiting 8. Exercises 1. The History of UnixUnix (a trademark of Bell Laboratories) was first developed by Ken Thompson in 1969 at Bell Laboratories as a private research project. Dennis Ritchie assisted the development soon after the first version was complete and later versions were rewritten in the C programming language which was designed by Ritchie. In 1974, after the sixth edition of the operating system at Bell Labs, the source code was distributed to Universities interested in developing it further. This led to a number of enhancements, developments, and diversions and resulted in many variations of "Unix". The two main 'flavours' of Unix are System V and 4.3 BSD. System V comes from AT&T Bell Labs (now the Unix Support Group), whilst 4.3 BSD is from the University of California at Berkeley. The two streams have converged into System V Release 4, but almost all vendors offer a Unix that provides either a sub-set or a super-set of System V Release 4. There are a number of reasons that Unix has become popular. Firstly it is written in C which made it easier to 'port'. Secondly it is a good operating system, particularly from the programmers' point of view. Existing tools may be combined easily to produce new tools, there is a consistent approach to all aspects of the operating system, and well controlled multi-tasking facilities are at the user's disposal. It was also cheap (approximately $200), came with source code, and ran on small inexpensive mini-computers. 2. Information Technology Services Hardware Resources3. The Unix File SystemThe Unix operating system contains files (information 'containers'), and directories ('containers' of files). An analogy is paper and Manilla folders. In Unix every aspect of the machine (including such things as printers and terminals) are represented as files, and there is a clean and consistent feel to things. Whether we wish to list a file to the screen, copy it to another file, or print it out, the approach is the same. The structure of directories on the machine is known as the file system. It is hierarchical in that directories can contain directories which in turn can contain directories, and so on. This kind of file system is often referred to as a tree structure . An example of a Unix file system follows:
In this example, / is the root of the file system, and bin, dev, etc, tmp, u, and usr are subdirectories of /. /is said to be the parent directory of these directories. It is worthwhile introducing some of these directories at this point:
In Unix, a pathname refers to the location of a directory or file within the file system. Components in pathnames are separated by a "/". For example a user with the name smith, may have a home directory of /u/ucc/smith . This is called an absolute pathname because it begins with a "/". If the pathname does not begin with a "/" then it is taken to be relative to the current directory and, not surprisingly, is called a relative pathname . There are two 'special' directory names in Unix:
4. Logging In and Logging OutIn this section you will log in to the machine. Depending upon which kind of machine you will be working from the steps to reach this point will differ. For the purposes of this course, let us assume you are using the Convex and can see on your screen the following
ConvexOS/Secure, Release V10.1 (sesame.qut.edu.au)
login:
At this point, sesame is awaiting the username of the user wishing to log in. Every system user is known to the system by a unique username and user number (user-id). The user's username should now be typed.
ConvexOS/Secure, Release V10.1 (sesame.qut.edu.au)
login:
Password:
The system is now waiting for the user's password. This will not be echoed onto the screen. Once this has been entered correctly, the following should appear on the screen:
Last login: Tue Jun 6 10:27:12 1995 (3h 8m ago) from 131.181.16.199
Last failed: NEVER
msgs: /mnt1/smith/.msgsrc : No such file or directory
sesame[csh]%
Sesame is now waiting for you to enter your command. To log out of a Unix machine, type <Control>-D, exit , or logout. ExerciseLog in to sesame and then log out again. 5. Shells5.1 What is a Shell?When you see the "sesame[csh]%" prompt, you are not talking directly to the operating system kernel, you are in fact interacting with an intermediate command interpreter, a shell . This shell is just a normal program although it does allow you to do a number of useful things:
5.2 Which Shell?There are quite a number of shells available world-wide. We offer five shells for use by users:
All offer slightly different features. The Bourne shell (or just "the shell") is the shell from early versions of Unix, and is offered on all versions of Unix available. The C shell is similar to the Bourne shell but offers a C like programming language and has enhancements for controlling jobs. It is also likely to be found on all versions of Unix. The remaining shells are extensions from both of these (the T C shell being an extension of the C shell and the Korn and Bourne-again shells being extensions of the Bourne shell). Most users begin with the C shell and move onto the T C shell or Korn shell when they discover the features that are available to them. The shell's prompt often indicates which shell is in use. For example, the Bourne and Korn shell's default prompt is "$ ", the C shell's is "% ", the T C shell's is "> " and the Bourne-again shell's is "bash$ ". 5.3 Shell VariablesShells record information about your environment in shell variables . These can
be examined and altered using the set command. Typing set with no arguments
displays all shell variables. For example, for a user named Smith: sesame[csh]% set addsuffix argv () autologout 0 cdpath /mnt1/smith cwd /mnt1/smith edit emacs gid 1087 history 20 home /mnt1/smith notify path (/usr/local/bin /etc /usr/etc /usr/convex /usr/ucb /bin /usr/bin . /mnt1 /smith/bin /usr/bin/X11 /pkg/pbm /gtl) prompt sesame[csh]% prompt2 ? prompt3 CORRECT> shell /bin/csh shlvl 1 status 0 tcsh 5.22.07 term vt100 tty ttypa uid 1531 user smith version tcsh 5.22.07 (CONVEX) 03/20/92 options 8b,nls,el,dl,al,dir sesame[csh]% There are several shell variables set. To change one of these the set command is
used again, but a value is also specified. For example, to change the prompt variable
(which will change the shell's prompt), the user could type: sesame[csh]% set prompt="Yes Master> " The quotes are necessary to group all the words together as the shell normally interprets white space as argument separators. Now the prompt will be Yes Master> To see the new shell variables type set at the prompt: Yes Master> set Yes Master>set addsuffix argv () autologout 0 cdpath /mnt1/smith cwd /mnt1/smith edit emacs gid 1087 history 20 home /mnt1/smith notify path (/usr/local/bin /etc /usr/etc /usr/convex /usr/ucb /bin /usr/bin . /mnt1/smith/ bin /usr/bin/X11 /pkg/pbm /gtl) prompt Yes master prompt2 ? prompt3 CORRECT> shell /bin/csh shlvl 1 status 0 tcsh 5.22.07 term vt100 tty ttypa uid 1531 user smith version tcsh 5.22.07 (CONVEX) 03/20/92 options 8b,nls,el,dl,al,dir Yes Master> To change the prompt back to its original format: Yes Master>set prompt "sesame[csh]%" These variables are visible to the current shell. They are not available to any other shells (invoked from the current shell) - unless they are default values. They may, however, be exported to other shells and programs. The C and T C shells also maintain an environment which is available to all the user's programs (including shells). This is manipulated with the printenv and setenv commands. printenv displays all environment variables. For example: sesame[csh%]printenv TERM=vt100 HOME=/mnt1/smith SHELL=/bin/csh USER=smith PATH=/usr/local/bin:/etc:/usr/etc:/usr/convex:/usr/ucb:/bin:/usr/bin: .:/mnt1/smith/bin: /usr/bin/X11:/pkg/pbm:/gtl LOGNAME=smith SHLVL=1 PWD=/mnt1/smith HOST=sesame.qut.edu.au HOSTTYPE=convex sesame[csh%] Notice that the syntax is different and note also the convention of lower case for shell variables and upper case for environment variables. 5.4 Logging In and Logging OutWhen a user logs in, some variables are set by default (depending upon the user's shell). In order to have the environment set up automatically the way the user desires it to be, variables and commands can be placed in a file which is automatically executed when the user logs in. In the C and T C shells, when the user logs in, a file called ".cshrc" (C shell resource) in the user's home directory is executed, and then a ".login " file (also in the user's home directory) is executed. In fact, whenever a shell is instantiated the .cshrc file is executed. When the user logs out, a file called ".logout" in the user's home directory is executed - if it exists. 5.5 AliasesIt is often the case that users type a command a lot. It would be nice if the name of the command could be shortened or if a whole command line could be abbreviated. In the C, T C, and Bourne-again shells there is. Aliases are stored command abbreviations. Let's consider the date command. If we wished to print out the date and time we would use this command and it would produce the following output: sesame[csh%] date Tue Jun 6 14:23:03 AES 1995 sesame[csh%] Say we wished to display the date and time and some other information in a different form. We could do this using the date command by specifying a format. For example: sesame[csh%] date '+Today is the %jth day of the year.%nIt is %a %h %d at %r.' Today is the 157th day of the year. It is Tue Jun 06 at 02:23:59 PM. sesame[csh%] This is a long command to type out every time we want this output. To simplify this task we could create an alias for it. To do this (with the alias called "longdate") we would type: sesame[csh%] alias longdate Now, when we type longdate the shell will expand it into the larger command: sesame[csh%] longdate Today is the 157th day of the year. It is Tue Jun 06 at 02:25:32 PM. sesame[csh%] To find out what aliases have been defined, the alias command should be used with no arguments, i.e.: sesame[csh%] alias 5.6 Filename ExpansionsThere are three basic filename expansions:
Some examples of these include:
Note that all filename expansion is done by the shell before the command 'sees' the arguments. 6. Basic Commands6.1 Command SyntaxThe syntax for Unix commands is invariably of the form: command -option1 -option2 ... argument1 argument2 ... or command -option1option2 ... argument1 argument2 ... Unix is case sensitive; command names, filenames, and directory names must be typed in the same case as they were defined, i.e. "fred", "Fred", and "FRED" do not represent the same file. Unix commands are often shortened (sometimes terse) forms of words. This is one of the criticisms levelled at the operating system. It is sometimes hard to determine the name of a command that is required. There are four useful commands available to assist this. 6.2 Obtaining Information on CommandsManualsOn-line help manuals are available on unix computers. These manuals are broken up into (essentially) eight sections:
Most users need only be concerned with section one (and perhaps section six). System administrators will also need to refer to section five and section eight, while systems and network programmers will almost certainly additionally require sections two, three, and four. Gaining access to these manuals is discussed below. The apropos Commandapropos a. - to the purpose; in respect of; concerning. The apropos command (an alias for the man -k command) performs a keyword look-up to locate commands. It searches the on-line manual whatis database (see below) for related commands. For example, if wondering what the command to rename a file is, the following could be typed: sesame[csh%] apropos rename mv (1) - move or rename files rename (2V) - change the name of a file sesame[csh%] Two commands have been located: mv and rename. The number in parenthesis indicates which section of the manual the command is from, thus mv is a user command whilst rename is a system call. Hence mv is likely to be the command required and is worth further investigation. The whatis CommandIn a similar vein to apropos is whatis. This command displays a one line summary of the specified command (which is what apropos displays). For example: sesame[csh%] whatis whatis whatis (1) - display a one-line summary about a keyword sesame[csh%] This command is useful to determine what a command will do before executing it, and is much faster than looking through the full manual entry. The man Commandman is the command used to consult the manuals. The format of man is: man [ section ] command where:
When no section number is specified, man will search through the sections from one to eight and display the first manual page encountered. For example: sesame[csh%] man man will return the first manual entry for the man command (section 1), while sesame[csh%] man 5 passwd will result in the file format section of the /etc/passwd file being displayed rather than the user command section of the passwd command which would have been found first. 6.3 Information About PeopleThere are a number of commands that are available to users to allow them to configure their environment. The finger Commandfinger displays information about people. With no options, it gives information about logged on users. The finger command shows the users' log in names, who they are 'in real life', which terminal number they are logged onto, how long they have been idle, when they logged on, and which machine they logged in from. finger also accepts an argument. This can be a username, a first name, or a surname and all matches will be displayed in more detail. The specified user need not be logged on. For example: sesame[csh%] finger smith Login name: smith In real life: Julian Smith Directory: /u/ucc/smith Shell: /usr/local/bin/tcsh On since Feb 3 10:42:09 on ttypd from ... 2 hours 43 minutes Idle Time No unread mail sesame[csh%] Using finger you can determine who is logged on, when someone last logged on, when they last read their mail, which person corresponds to a username (and vice-versa), which terminal they are using, and if they are sitting in front of their terminal. The chfn CommandThe 'in real life' field may be changed from that shown. This is done using the chfn (or change finger) command. For example: sesame[csh%] chfn Changing NIS finger information for smith on Sesame Default values are printed inside of '[]'. To accept the default, type <return>. To have a blank entry, type the word 'none'. Name [Julian Smith]: J.R. Smith Password: NIS entry changed on Sesame sesame[csh%] finger -s smith Login Name TTY Idle When Where smith J.R. Smith pd 2:52 Mon 10:42 sesame.qut.edu.au sesame[csh%] Note that you may be asked to enter your password (as shown above) before this information can be changed. The password is not echoed on the screen. The chfn command should be used with some restraint. Firstly, it is a useful mechanism for determining just who a user is - entries like "WALLY" or "Grand Wizard" don't tell people very much, and the user looks a bit silly. Secondly, our machines are reachable from (almost) all over the world and someone from another country may wish to 'finger' you, and a responsible attitude to the University would obviously be appreciated. A second command /usr/local/etc/gfinger (which was written locally) returns the original information on a user. The chsh CommandShown also by finger is the user's shell. This can be changed at any time using the chsh command. For example: sesame[csh%] chsh Changing NIS login shell for smith on sesame. Old shell: /usr/local/bin/tcsh New shell: /bin/csh Password: NIS entry changed on sesame sesame[csh%] Next time the user logs in he/she will get a C shell rather than a T C shell. The only valid shells are those mentioned in section five above. The passwd CommandThe user is able to change his password at any time. This is done using the passwd command. For example: sesame[csh%] passwd Changing NIS password for smith on sesame. Old password: New password: Retype new password: NIS entry changed on sesame sesame[csh%] As shown, the user must type in the old password before he/she is allowed to change it and must type the new password in twice for confirmation. None of the passwords are echoed to the screen. If users forget their passwords Computing Centre staff can change their password for them although no-one can determine what their password is as this is only stored on the system in an encrypted form. Some guidelines for passwords are:
The next few commands allow users to obtain information about others. The who Commandwho displays who is on the system, which terminal they logged on to, when they logged on, and from which machine they came. who may also be executed when information about the current user only is required: sesame[csh%] who am I sesame!smith ttyq4 Feb 7 09:29 (ComputingC5h19.c) sesame[csh%] The w CommandIt would be nice if we could determine what users are doing once we have determined that they are logged in. Two commands are available to do this: w and ps. The ps command will be discussed later. In addition to the information given by who, w also displays the command being executed, the amount of idle time, and the amount of time by the user and by the current process. 6.4 Negotiating the File SystemWhen moving about the file system, three general commands are: cd, pwd, and dirs. The cd CommandTo move from one directory to another, the cd command may be used. It has the following syntax: cd [ pathname ] With no arguments, the user is moved to his/her home directory, otherwise the user is moved to the specified directory. The pwd Commandpwd displays the physical name of user's current working directory. Sometimes the physical name of a directory is different from the logical name. For users of the C and T C shell, another command could be used instead of pwd: the dirs command. Alternatively, for users of the Bourne and Bourne-again shells, an alias could be set up to "echo $PWD". 6.5 Directory ManagementIt is a good idea to store files in appropriate directories rather than cluttering up the home directory. There are three commands to manage directories: mkdir - create a directory; rmdir - to remove a directory; and mv - to rename a directory (discussed later). We will use the ls (list directory contents) command to examine the contents of directories. This will be discussed in more detail later. The mkdir Commandmkdir allows the user to create directories. It has a simple syntax of the form: mkdir pathname where pathname is either a directory name or a pathname. The rmdir CommandWe can use the rmdir command to remove a directory. Its syntax is similar to mkdir. rmdir pathname where pathname is either a directory name or a pathname. 7. An Introduction to the vi Screen EditorA number of editors are supplied with Unix systems. One of these is the screen editor vi. Many people find vi to be unfriendly, but I feel that this is because they do not fully understand its features, or have not used it enough to become comfortable with it. vi is a case-sensistive, keyboard based editor. Almost every key-stroke issues a command, and it is actually two editors in one. From within vi the user can issue commands to the line-based ex editor. vi operates in two modes:
To move from insert mode to command mode, the user should press the <Escape> key. Pressing this key when in command mode will cause the machine to 'beep' telling the user that the editor is already in command mode. Some initial commands will now be introduced. Cursor movement:
Scrolling:
Searching:
Insertion:
Modification:
Deletion:
Revocation:
Miscellaneous:
Saving, Aborting, and Exiting:
Most commands may be prefixed by a number k, indicating the number of times to execute the command. 8. ExercisesDefine the difference between an "absolute pathname" and a "relative pathname". Log in to sesame.qut.edu.au Which shell are you using? How did you determine this? Change your prompt (to anything you desire). How did you do this? If you wanted a command to execute automatically every time you log in, how would you set this up? Create an alias to check if another user is logged on. A command to do this is "who | grep username ". What command did you use? What filename could you use to match the following files: fred1.dat fred2.dat fred3.dat fred10.datbut not " freddy"? How many user commands refer to the word "manual"? How did you find out? What does the grep command do? How did you obtain this summary? What does the "-a" option of the lscommand do? How did you extract this information? What is the "noclobber" C shell variable for? How did you find this out? Who uses the username "system" and when did they last log in? How did you determine this? Check and change your 'finger' information. What commands did you use? Is anyone currently running the "nn" news-reader program? How did you arrive at this answer? What is your home directory? How did you find this out? Change directory to "/usr/tmp". Does pwd return the same result as dirs? Why do you think this is? Make a directory called "train" in your home directory. What command did you use? Remove the "~/train" directory. What command did you use? Make the "~/train" directory again. Rename it "training". What command did you use? Use vi to edit your .cshrc file and add the alias from above at the end of the file.
|