Lab 3 - Unix Lab#3 Page 1 of 9 Lab #3 CS 2351 UNIX...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Unix Lab#3 Page 1 of 9 Lab #3 CS 2351 UNIX Operating System Name l Section L This lesson examines directory files and plain files. The following UNIX commands are used: de, cd, mkdir, and rmdir , 1p, wc, date and cal. Directory and Plain Files The UNIX file system is a flexible and easy to use system that is particularly well suited for software development. The UNIX file system is a tree structure which allows files to be placed into convenient groups by collecting them into a directory file. A file is a collection of related information. These are also called plain files. Files could be text, or some type of data base with both text and numbers, or it could be a computer program. Ordinary files are organized into special files called directories. A directory contains zero or more (sub) directories and zero or more files. Directories are used to keep like items together in one place, similar to having a different note book for each class. A plain file corresponds to a leaf node on a tree since a plain file cannot have any successor. A directory file, corresponding to an internal node on a tree, may have one or more successors of either type. The hierarchical structure of the file system is possible because of the directory files. The entire file system is a single tree with the root of the tree specified as /. The tree structure is like an upside down tree with the root at the top. A reference to the file named / is a reference to the root directory. Each user has a main directory, called the home directory. The home directory is the root of a subtree in the file hierarchy. The name of the user's home directory usually corresponds to the user's login name. There are some rules for file or directory names. You choose names for any files and directories you make. UNIX file names may contain from one to fourteen of the following characters, in any combination: Uppercase letters (A to Z) Lowercase letters (a to z) Numerals (0 to 9) Period (.), underscore (_), comma (,) The file names should NOT contain spaces or any of the following special characters: &*\l {} $<>()#?‘"/;A!~% It is always a good idea to choose reasonably short names to save typing. You should also choose names that convey information about the contents of the file. You will be learning many UNIX command names. DO NOT USE command names as file names. http://cs.okstate.edu/~csZ3 5 l/unix03 .html l/27/20ll Unix Lab#3 Page 2 of 9 EXAMPLES OF A UNIX DIRECTORY STRUCTURE (Root Dire eterng Employee letter software MSIS Cunix Progl Labl Progl 1 Lab2 Ping? Frog 3 Words that are in boxes are directories. Words that are not enclosed in boxes are plain files. 1. The top directory is the root. 2. The first line of directories (bin, contrib, d, k, etc) are called the systems directory. These are similar for all UNIX systems. The boxes that have one letter in them (d, k, p, etc.) are considered partitions. There are many partitions on the SUN machine. There are several students in each partition. Each student is in one of these partitions. 3. The next row contains other directories. Notice the one marked "srp". This is a person's home directory. Each one of you will belong to a partition and have a home directory. The path name for this particular person is: /d/srp. This is an absolute path name because it starts with the root (/) and gives the entire path name. 4. Each person can have some files in his or her home directory. He or she can also have several directories where other files that are related can be kept. It is a way to keep things organized and easier to find. In this particular example, the person has one directory for MSIS homework and another for Cunix homework. These are then broken down into two other directories, prog and labs. The student can keep programs in the program directory and lab assignments in the labs directory. In each of these directories are files. http://cs.okstate.edu/~c5235 l/unix03 .html 1/27/201 1 Unix Lab#3 - Page 3 of 9 Pwd Command The command pwd (print working directory) can be used to find out what directory you are in. Type in: pwd /’ What is printed out? / 1w MC / e; a This should also be the path for your home directory. When you make new directories and change to that directory, your working directory will change. Mkdir Command The mkdir (make directory) command is used to create a new directory. The reason you might make new directories is to keep files that are alike in one directory. An example would be class assignments for certain classes could be kept in one directory. Other class assignments could be in other directories. Now, make a directory named sample within your own directory by issuing the command: mkdir sample Next, create another new directory named homework. What command you use? MM it .E oratw‘fllé /" Type in: 1 s — 1 You should see sample and homework listed. You will see lists of files that look similar to this. drwxr—xr—x 2 2351jje 2351jje 512 May 5 08:16 sample drwxrw-rw— 2 2351jje 2351jje 512 May 5 08:20 homework Cd Command The cd command allows you to change to a new directory. Change to the directory homework by using the cd command: cd homework What command can you use to check that you are in the new working directory? fwd A What is listed now as your working directory? _/’h UM _( L/jflsmm as" Ed; i Create these next two items in the homework directory: 1) Create a new directory called cunix. What command did you use to make this directory? MLéi if gimix http://cs.okstate.edu/~cs2351/unix03.html 1/27/2011 Unix Lab#3 Page 4 of 9 2) Create a new file assignl. This file is still in the homework directory. Create assignl by doing the following: cat > assignl Assignl is a plain file. <ctrl-d> Notice that you can use the cat command to enter information into a file instead of using the vi editor. This is a short cut method. However, if you make a mistake after you have completed a line and then hit the return key, you cannot make a correction on this file. For very short input files, this can be another method of making a file besides using the vi editor. When you have finished typing, you press the ctrl and (1 keys to get back to the prompt. Type in the following: 1 s — 1 Remember that there is always a space after a command and any options that are used. the "l" is a lower case "L" and not the number "l"(one). What files are listed? Migfl M a? Is one of the files a directory?_\;rg;§_f_(fl¥_7 , , . . I . y ) 1 :9 ii §W”/ W NV: ,1, a k ’ \ ' .43 If so, how do you know that 1t 1s a directory? Qjm 5m {.5 VJ, ,5, f is» «- '1 V _ (A. [a +9; In any directory there are two invisible files named “." and "..". These will only show up if you type in the type in the ls -a command. At this time, type in: l s — a You should see the “." and the ".." files. These refer to the current working directory (found by typing in pwd) and its parent respectively. The parent directory is the directory above the directory you are in at the time. It is one level up on the tree. You can change your working directory to the parent of the current working directory with the command cd .. . Make sure that you leave a space after the cd before you type in the two periods. Enter the following sequence of commands. pwd cd .. pwd What is printed to the screen? / AWN g 5;; d aim/ Your home directory is the parent to the directory "homework/"I. You can change to your login directory (also called home directory) at any time by typing: cd http://cs.0kstate.edu/~c523 51/unix03.html l/27/20ll Unix Lab#3 Page 5 of 9 When you no longer need a directory, you can remove it by using the rmdir command. The command would be: rmdir <directory name> First, remove the sample directory that you made. Type in: rmd i r 5 amp 1 e Next, try to remove the homework directory. What message did you get when you tried to do this? ifmAi /: jflow (“jib f ff ria'i' ErAfg / kid“; 3‘ You should have received an error message saying rmdir: directory "homework" Directory not EmptyYou cannot delete a directory that has files or other directories in it. The files (and directories) must be removed from the directory first (using rm or rmdir). You must return to the directory you want to delete and remove files and any directories in that directory first. Then you must be in the level above (the parent directory) in order to remove the directory. Return to the homework directory. Remove the directory, homework, by first removing the file and directory from it and then moving up to the parent directory. What commands did you use to remove the directory homework? Zme/owéwéfm (Mien /~—-> rmdiv‘l (am‘>(»;a{ ——> I‘M}; flay-am; g i Checking out different directories Make sure that you are in your home directory. Type in: pwd What is the response? 4 her/.4 Leia is If you are not in your home directory, type in cd before you do the following. You will now go browsing up the hierarchical structure of the UNIX file system. You will go to the root of the tree and over to some other directories. Type in: cd.. Next, issue the command pwd. What was the result? ham :2 You are now in the subdirectory that contains the names of other users who are in the same partition as you are. You can find out how many other users have accounts in the same partition as you by issuing the following command: ls l WC —1 http://cs.okstate.edu/~csZ3 51/unix03.html 1/27/2011 Unix Lab#3 Page 6 of 9 What was the result? 3 We Command The wc command is used for counting the number of lines, words, and characters in a file or directory. The wc has three options: - c Characters This option causes we to display only the number of characters in a file. — 1 Lines This option causes we to display only the number of lines in a file. —w Words This option causes we to display only the number of words in the file. If none of the options are listed after the we, then you will receive three numbers. The first is the number of lines, the second is the number of words, the third is the number of characters. Type in the following: lslwc 21 /,., What was the result? 3 '3 fl. In this particular case, the names make up one word and each line contains one file, so the number of words and the number of lines will be the same. Next, type in: ad . . pwd / You have reached the root of the tree. Next type in: What is the working directory now? ls -1 You will get a list of the directories that are in the systems directory. You will now go to one of the subdirectories. Next, Issue the command: cd /class or cd class You are now in the class directory. This directory contains special subdirectories for different classes. There is one for this class. It is called cs2351. This directory has files that you will use to copy back to your account. The system administrator sets up these special directories so others can access them. Change to the cs2351 directory. K" What commands did you use to do this? Léi g5 27: S \ Issue the command: http://cs.okstate.edu/~cs2351/unix03.html 1/27/2011 Unix Lab#3 Page 7 of 9 l 5 What answer did you get? '2 i . 3%,Meg 2 an Dc a; a“ A Some directories are protected so others cannot access them. That is what is happening here. Return to your home directory by typing in: cd The Print Command Sometimes you want to print out files. Later your instructor may have you do other assignments that need to be turned in. You can print out files by using the 1p command. This command requests that files be sent to a printer. The lp command also uses the -d option for destination. NOTE: If you are at home or somewhere else where you are connected by a modem, the lp command will not work on your home printer. The 1p command does not work for local printers. You can still issue the command from home, but the specified file will be printed at the designated printer on campus. The command will look like this: 1p —d<printer name> <filename> The preferred laser printer in MS 108 : mSlOSprB If you are located in some other lab besides MS 108, there are other printers in the rooms at OSU in Stillwater and also at OSU—Tulsa. You may need to ask your instructor for names of other printers. If you happen to be in some other room on campus, the printers will have different names. There is usually a sign above the printer telling you the network name. You don't have any long files to print out yet. In order to try out the print command, you can copy a file from the newuser directory that has been set up for students and faculty who are new to the Computer Science Department. This file will be used later in another lab. NOTE: There is a space between the cp and the directory address. One way to do this is to type in the following: cp /pub/htdocs/newuser/employee employee Another method that is shorter is to type: cp /pub/htdocs/newuser/employee The first method above repeats the name of the file in the directory where you are located. You can change the name of the file and call it something different. You do not need to call it the same as What it was called in the directory you copied it from. http ://cs.okstate.edu/~cs235 l/unix03 .html 1/27/201 1 Unix Lab#3 Page 8 of 9 The second one allows you to copy the file to your current directory —-noted by the . (period) at the end of the copy command. This saves you some time and typing and less chance of error since you have less to type. ,_ Look at the file, employee. What did you have to type in to do that?c'g [fag/Mi} gr; infwufa ,: a 1 ‘3 (53” I ’ expiw I Next, combine the file practice with the file employee. Do this by typing in: e“ {5/ cat practice employee > combine This should put the two files together. You are going to print the combine file out. However, there will be several of you printing out these files to the printer and they will all look alike. In order to tell which one is your file, you Will add your name to the file. If you are using the vi editor, type in: vi combine At the beginning of the file, open a new line at the top of the file. Add your name on it and also the class (such as CS 2351, Section 1) that you are in. Save the file again. Next, print it out. Type in: 1p -dms10 8pr3 combine NOTE: If you are in another room besides MS 108, you will have use the name for the printer in the lab that you are in. Write down the printer name if you printed this outside of MS 108. Using a home printer is not an option here. Notice that when you submit your request to the printer, you get back a comment: request id is printer_1-365 (1 file) *************************************************************************** Attach to this lab, the printout of the file that you printed and submit it with the lab *************************************************************************** The Date Command The date command displays the time and the date. It gives the weekday, month, day, time, time zone, and year. Type in the following: date What message was printed? Tim fl i 5 I i e t «ml, L The Calendar Command This command displays the calendar. It can be used to find out what the calendar would be for a year or you can find out only a month. Type in the following: http://cs.okstate.edu/~cs235 l/unix03 .html 1/27/2011 Unix Lab#3 Page 9 of 9 cal 2005 a, Fifi" What was the result of this command? Tim (ta-fig,” “a +3“ 3/;ng wag Since this command fills more than one screen, what is the full command could you use to see the 2005 calendar it one screen at a time? Q/ @005 ] mg“ If you wish to know what day your birthday will be on for this year, you can type in the month (from 1 to 12) and then the year. Use the cal command to type in the month and this year to find out what day _ your birthday is on. What command did you use to find out when your birthday will be? " V” Cm” - H 20 l L What day of the week is your birthday on this year? $14 01% If you just want the calendar for the current month, you just type in: cal What is the current month? jammy; At the end of this lab, you should have the following files in your home directory: practice filea fileb filec employee combine List out the files to see if they are there. Do not remove any of these files. What command did you use to do this? Is Please indicate any areas of this lab that were difficult to understand. Fa1107 http://cs.okstate.edu/~cs235 l/uniXO3.html 1/27/2011 Steven Belcher cs 2531 Section 402 The vi editor is a full screen editor that allows you to move the cursor around the screen when you are in the command mode. You can do this by (1) typing certain unique key sequences, (2) or you can move from one line to another or over one or more spaces on a line by using the h j k 1 keys. You can also move lines to a new locatin by using the "y" for yank. It will copy your line or lines to a buffer and you can then place it somewhere else by using the "p" for the put command The h key moves the cursor to the left, l moves the cursor to the right, j moves the cursor down one line and k moves up one line. If you want to open a new line above, you can press the "0" key. If you want to open a new line below the line you are on, press the "0" (lower case "0") key. Delete word 1 Delete word 2 mgt Cooper John 06151995 66000 mgt Davidson Darla 04151992 69500 mgt MacDonald George 06151985 70000 act Smith Thomas 04102002 56000 act Smith Alecia 04121991 65000 mis MacLeod Janice 01021977 90000 mis Mack Joe 02252003 85000 mis Winslow Sarah 02151995 58000 adm Smith Dexter 01021975 100000 mis Bennett Joan 08152001 79000 mgt Neason Elizabeth 10251998 65500 act NeSmith Donald 11301966 99500 ...
View Full Document

Page1 / 10

Lab 3 - Unix Lab#3 Page 1 of 9 Lab #3 CS 2351 UNIX...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online