Computer Science Dept.
CI211 Unix Worksheet
You may now do this on the Unix machines in C57
CDE & UNIX WORKSHEET
To do the practical work for CI211 you need access to the Computer Science Unix server named stonkin, which provides a windowing environment called Common Desktop Environment. This worksheet describes how to access CDE from an Information Services PC, and some useful Unix commands. You have two weeks to do it in. Contents 1. Logging in to Computer Science ...................................................................................................... 1 2. Exploring the Common Desktop Environment ................................................................................ 1 3. Some Unix Commands .................................................................................................................... 2 3a. Create a Directory .................................................................................................................... 2 3b. Create a file using gvim, and save it into the new directory. .................................................... 3 3c. Create and Remove Sub-Directories ........................................................................................ 3 4. More about gvim.............................................................................................................................. 4 5. Printing ........................................................................................................................................... 5 5a. Preparation for Laser Printing ................................................................................................... 5 6. Online Help ...................................................................................................................................... 5 7. Exploring the file system ................................................................................................................. 6 8. Changing file permissions ................................................................................................................ 6 9. History & Typing Shortcuts ............................................................................................................. 6 10. Redirection and Pipes .................................................................................................................... 6 11. Emergency Exits ............................................................................................................................ 8 13. Logging out. ................................................................................................................................... 8
There are many things that are easier to demonstrate than describe. If anything puzzles you please ask a demonstrator for help. 1. Logging in to Computer Science Log in to the PC as usual and go to Courseware - Comp Sci - Computer Science Login from the Start menu. This will start up a program called Exceed which allows stonkin to send windows to your machine. (Exceed provides a few facilities such as copying and pasting between CDE and the Windows clipboard. These might be useful later when writing documentation.) You will be offered a choice of servers to log in to. Choose stonkin Type your username. Then press return or click on OK (NOT tab!). Stonkin will prompt for your password. As you enter your password nothing will be echoed to the screen. If the machine accepts you, you will be offered a choice of environments (in a blue window). Choose The Common Desktop Environment. 2. Exploring the Common Desktop Environment Your screen will rearrange itself, and display three windows on a blue background. At the bottom is a pictorial menu called the Front Panel.
The icons on the Front Panel are, on the left:- The Netscape Web Browser, Calendar, File Manager, text Note Pad and E-Mail(Do not use this one). On the right are:- A printer manager(you won’t need this), Desktop Controls ( allows you to customise your windows), Performance Meter, Help Viewer and
1
Computer Science Dept.
CI211 Unix Worksheet
Trash Can. The Help Viewer and the File Manager are already open if this is the first time you have used CDE. In the middle of the Front panel are four numbered buttons that allow you to switch between four different workspaces. There’s a padlock for locking the screen (for short periods only please), a globe that spins when things are happening and the EXIT button. The Help Viewer only holds topics to do with the Common Desktop Environment, it is no help with UNIX. However you might like to spend a few minutes looking at “Overview and Basic Desktop Skills”, “Basic Desktop Skills”, “Working with Windows". N.B. The button for closing a window is at the top left. If you habitually use the exit button at the top right for closing Microsoft windows you will find yourself with loads of minimised window icons in CDE! Also, one click is usually enough to open things. If you double click you get two of everything. The File Manager shows you your files. Stonkin provides a link to your Information Services filestore so you should be looking at the files on your M: drive. Close the Help Viewer, but leave the File manager open for the moment. It will be useful to illustrate what is happening when you alter your filestore in the next section. 3. Some Unix Commands Most of the work in this course is done in terminal windows using Unix. The small booklet Ann’s Simple Manual has explanations of the most useful Unix commands. When you need to use an unfamiliar command look it up in the booklet so you understand what it does. The various symbols like .. ~/ * are explained in the section called Shortcuts. Open a terminal window. To do this, click here for a pop-up menu.
Then click on This Host. -> You should now have a window with a Unix prompt in it, something like stonkin:xyz1> It should be the active window, and so have a pretty pink border, if not, click on it. Arrange the windows so that you can see both the File Manager and the terminal window. Try typing the Unix command pwd into your terminal window. The computer will reply with the path to the working directory, the directory you are in now. The path should end with your user-id, because you should be in your home directory. Compare this with the information at the top of your File Manager window. Type ls in your terminal window. This will list the files and directories in your working directory, the same as in the File Manager window.
3a. Create a Directory Start by creating a directory (folder) for today’s work: - Type mkdir CS211Work in your terminal window. Now type ls to see if the new directory is there. Look at the File Manager window; it should also show the new directory. Now to move into the new directory, type the command cd CS211Work Notice your prompt has changed, to indicate where you are. in the terminal window.
2
Computer Science Dept.
CI211 Unix Worksheet
The change of directory only applies to the window you were in when you gave the command. The File Manager window is still looking at your home directory. Double click the icon for the CS211Work directory to make the File Manager move down into it. Try the Unix command ls to get a list of files. You have no files in the CS211Work directory at present, so ls should give nothing. Try the Unix command more testfile This is asking to see the contents of testfile. This file does not exist yet, so you will get an error message. NOTE If Unix is able to carry out your commands successfully, it does it quietly; if it sends you messages it means something is wrong. Always ASK if you get a message you do not understand. 3b. Create a file using gvim, and save it into the new directory. To create a file into a particular directory the easiest way is to BE IN the directory when you start the editor. The recommended editor is called gvim. There is a short description of gvim in the middle of Ann’s Simple Manual. Read the first part of it now. You cannot open gvim from the Front Panel, so you must get it through your terminal window using a Unix command. The simplest way to start gvim is to give it a filename, as in:gvim testfile You should get a new window with the full pathname to testfile in the title bar at the top and a lot of squiggles down the left hand side. Gvim labels empty lines with squiggles. When gvim starts up it is in normal, or editing, mode. See what happens if you type something like “Feed Me” Nothing shows in the window, but the machine beeps at you! Now change to insert mode by typing the i key. Nothing shows, except that the shape of the cursor changes, but now the screen will accept your typing. Try something like "This is a test of gvim." (And/or anything else you like) Save the file and quit gvim. To do this, either :Press the Esc key to get out of insert mode. Type :wq (colon wq) or Select Save-Exit from the File menu.
3c. Create and Remove Sub-Directories (This section is just an exercise in manipulating directories) Create within your CI211Work directory a sub-directory, called "Sub". The command for this is: mkdir Sub In the file manager window choose Set View Options from the View menu, and set the options to Show By Tree, Folders and Files and Representation By Small Icons. Keep checking to see what the file manager does as you work through what follows. (The idea is to give you a picture of your filestore.) When you created the directory Sub you used a relative pathname; i.e. you created the directory in your current working directory. The mkdir command will accept any valid pathname, you do not have to be in the directory in which you want to create the new directory. Try using the command: mkdir Sub/test This should create a subdirectory called test within Sub Move into Sub: type the command cd Sub in the terminal window.
(The command to return to your home directory is cd by itself. The command to go up one level in the tree is cd .. ( cd, space, two dots).
3
Computer Science Dept.
Now put stuff in the new directories
CI211 Unix Worksheet
Create a file with gvim in your Sub/test directory, call it personal and put your name in it. (You may need Update from the File Manager’s View menu in order to see it.) Move up into Sub, make another subdirectory called junk, (in Sub) and move into it. Have you still got a file called testfile in CI211Work? Make a copy of it in junk. (hint: cp ~/CI211Work/testfile . Why does this work? If it does!)
You now have two files called testfile. Their contents are the same but they are distinct files. The computer does not confuse them because their full pathnames are different. Now move yourself up into the CI211Work directory. Use the ls command with the -R option to see what is in CI211Work . Then compare this with the information in the File Manager. And now we’ll throw most of it away! Removing directories Try to remove the junk directory using the rmdir command. You should fail. The rmdir command will not remove a directory that is not empty. Remove the file you placed in the junk directory, using rm testfile then try removing the directory again. This time you should succeed. Do ls -R again to check. Now remove the Sub/test directory. But you are going to need personal later, in section 8, so move it up a level, into Sub first. (hint: mv personal .. Why does this work?) Look at the contents of personal using more personal to make sure it’s the same file!
You should not need the File Manager any more, so close it down. 4. More about gvim For the next task you need some fairly big chunks of text. There is a suitable file in ~jnw/CI211Files, called aber.txt. Copy it to your CS211Work directory cp ~jnw/CI211Files/aber.txt . (Do not miss out this dot ....^) Now check the file is there using the ls command. Open aber.txt in gvim, and refer to the gvim pages in the middle of Ann’s simple manual. Try out all the different ways of moving around. Try all the ways of inserting text. (Any remarks about Aber?!) gvim is not a word processor so you must decide where the end of a line should be. The Return key puts in an end of line. Try all the ways of deleting text. Try searching for a string, and moving to a particular numbered line. Replace all occurrences of “Aberystwyth” with e.g. “Heaven”. And then change them back. Try cutting and pasting chunks of text. (Drag with the left button over the text to select it, move the mouse pointer to where you want the text to go and drop the text by clicking the middle mouse button.) Finally, try saving aber.txt with a different name, and with its own name. Quit gvim.
4
Computer Science Dept.
CI211 Unix Worksheet
WARNING: If you ever get a message saying “No write since last change, q! overrides” stop and think. :q! means quit without saving 5. Printing The print command is lpr. Read Ann’s Simple Manual to see how to use it. The name of the dot matrix printer in the Sun Lounge is cssun Get a print out of one of your files. lpr -Pcssun filename
5a. Preparation for Laser Printing Work through this section but you need not actually print anything unless you want to. Files sent to laser printers must be in Postscript form. ASCII files can be converted to Postscript by using a2ps. (See Ann’s Simple Manual) This produces output like the Ann’s Simple Manual handout. (If you want two files to appear on one page (provided they fit) use the -A option.) Suppose you want to print aber.txt. You could send it straight to the printer, but a better idea is to save the Postscript to a file and look at it first. So you can correct mistakes without wasting print quota (or trees). First do a2ps -f9 -oprintAber.ps aber.txt This converts aber.txt to Postscript and saves the result in printAber.ps. (You can call it what you like; the .ps is supposed to indicate a Postscript file. The -f option sets a font size.) Now you have it in a file you can look at it before you send it to be printed. First, just for fun, try more printAber.ps Don't panic That's what a Postscript file looks like. It is full of instructions for the laser printer and the actual aber text is at the end. To see a Postscript file properly you need a viewer such as gv. Type gv printAber.ps When you are happy that the file looks OK you could send it to a printer. The printer in the user area is the most convenient for small jobs from the Sun Lounge. First it would be a good idea to check whether there is a long queue for the printer. Do lpq -Puserarea Provided the queue is not too long, and you really want to print the file do lpr -Puserarea printAber.ps to send the Postscript file to the userarea printer. If there was no need to look at the file first you could simply send it to the printer with a2ps. a2ps -f9 -Puserarea aber.txt 6. Online Help Unix comes with a set of online manuals. You can find out about a command by using the "man" command. Entering the command "man ls" will tell you about the ls command, "man man" will tell you about the man command. Alternatively you could use the manual browser xman. The text of the booklet “Ann’s Simple Manual” can be found on the course webpages. There are pages of Unix help on the World Wide Web:Start the Netscape browser by clicking (one single click) the world clock at the left hand end of the Front Panel. Click on the Home button for the Computer Science Home Page. For Unix information follow this route:Unix and X windows, Unix Help. Departmental Facilities, Lots more Help and Information,
5
Computer Science Dept.
CI211 Unix Worksheet
7. Exploring the file system Try moving up the directory tree from your home directory, and see where you get to! (cd by itself will always get you home). Some parts of the UNIX file system have conventional uses that are more or less the same on all systems. For example the directory /bin contains binaries, that is executable programs, that form part of the standard UNIX distribution. If you look in /bin ( ls /bin ) you will find files with the names of some of the commands you have used. For instance you will find the mv, mkdir and ls commands there. These files contain the executables that do the work when you enter a command.
8. Changing file permissions Since anyone can move around the file store you need a way to keep your own stuff private. You can change the permissions on a file to prevent or allow access by other people. To prove that this works you will need to collaborate with another user. Choose a partner now and find out each other's username. Experiment with changing the permissions on your "personal" file. (That you created in section 1c). See Ann’s Simple Manual/chmod for how to do it. You may need to give everybody permission to access your home directory, before your partner can read your file. First check the path to your home directory using pwd. Then do chmod go+rx < path to your home directory> You should be able to move into your partner’s home directory with cd ~theirusername Get your partner to try to access your file before and after you change its permissions for “others”. Take away your own read and write permissions then try to read the file. Put back the read permissions, but not write, then try to edit the file.
9. History & Typing Shortcuts The computer saves a list of the commands you have used. Type history now. You should see a numbered list of your commands. You can repeat any of these commands by giving its number preceded by ! For instance, the 10th command can be repeated by typing !10 Choose a fairly harmless command and repeat it now. ! followed by a letter will repeat the most recent command which began with that letter. !! means repeat the most recent command. The up and down arrow keys on the keyboard will move you up and down your history list. Look at Ann’s Simple Manual, Shortcuts for ways to save typing long filenames. You have used some of these already.
10. Redirection and Pipes Read the page called “redirect” in Ann’s Simple Manual. The pipe symbol is just left of Z on the Sun Lounge keyboards Now try the following:man ls to get the man page about the ls command. Don’t read it all now! Suppose we wanted to know how many words there are in this man page. wc will count the lines, words and characters in a file, but how can we make it work on a man page? We can use a pipe to feed the output from man to the input for wc. Do man ls | wc Now we have an answer on the screen, but what if we want to save it in a file? Then man ls | wc > countls will redirect the answer to the file countls. Look in the file countls to make sure. ( more countls )
6
Computer Science Dept.
CI211 Unix Worksheet
7
Computer Science Dept.
CI211 Unix Worksheet
11. Emergency Exits Sometimes the computer seems to get stuck and won't respond. The cure depends on what the problem is. Control-D (^D) will send an end of file character. Try typing the command cat This command normally prints the contents of a given file. But if there is no filename it takes its input from the keyboard. So cat will repeat everything you type for evermore. Try typing a few lines. Then give cat the signal to stop with ^D. If a program gets stuck in an infinite loop, you can stop it with ^C. There is a command kill which can be used to stop a process if you know its job number or its Process ID. (You should only use kill if something is hanging and you cannot stop it any other way!) Suppose you started xman with xman & Then xman will be running in the background. Type jobs to get the job number (in square brackets). Then kill %jobnumber will kill xman. If the process you want to kill is not in the background, you need the ps command to give you the Process ID. Start the xman browser xman without an &, so it is not a background job. In a new terminal window type ps -u your-user-id This gives a list of all the processes belonging to you. The number on the left is the PID. Find the PID for xman. Suppose it is 12345. Then kill 12345 should kill xman. Try it.
End of Worksheet This is the end of the Environment/Unix worksheets. The files and directories you created for this worksheet will not be needed again. You can tidy them away when the demonstrator has seen them and signed this worksheet. 13. Logging out. You logged in to two machines so log out from both:Close any applications that are still running, Netscape for instance. Click on EXIT on the front panel. You will now be back in a Windows environment. Close Exceed. (Click on the STOP icon in the Exceed tool panel, or right click the Exceed button on the TaskBar and choose close.) Logout as usual from the PC. To get this worksheet signed off, use Tweek, and upload your changed aber.txt file.
8