Using the UNIX Operating System ________________________________________________________________ UNIX is the name of the OPERATING SYSTEM you will be using to perform all of your class programming assignments. An OPERATING SYSTEM is a set of programs which manages computer system resources and facilities such as the CPU, file system etc. The following are some of the UNIX commands you will initially find most useful when using the USC computer network. To use the following UNIX commands, you must have successfully logged in to the campus computing facilities. You know that you are successfully logged into the UNIX operating system if you see a message such as sal-sun77(1) (or something like it) on the screen. This is the UNIX command line prompt and tells you that UNIX is ready to accept a legitimate UNIX command. For all of the following commands, the angle brackets such as in are not to be typed. They are used to indicate that the expression contained within the brackets is one to be determined and entered by you. You can use the "man" command to learn more about specific UNIX commands. "man" accesses the UNIX manual to give you specific information on any UNIX command. To access the UNIX manual, enter: man and a definition/description of the command specified by will be presented on the screen. For example, to view a description of the command that causes the contents of your current directory to be displayed on the screen enter: man ls If you wish to access an overview of the UNIX manual, you can enter: man unix To enter a UNIX command, type the command on the UNIX command line and then press the ENTER (or return) key. For many UNIX commands, "wildcard characters" can be used. Wildcard characters include the "*" character and the "?" character. The "*" character will substitute for any set of one or more consecutive characters in an argument to some UNIX commands. The "?" character will substitute for any ONE single character in an argument to some UNIX command. See the UNIX command "ls" below for an example of the use of wildcards. 1. List the contents of the current (sub)directory: ls This command (lowercase el, lowercase s) will display the files and subdirectories in the current directory. The directory you are placed into upon first logging in is called the "home directory" (or ROOT directory). You can change the current subdirectory to some other subdirectory by using the "cd" command discussed below. ls -l This command will not only display the files and subdirectories in the current directory, but will also display special information about each file or subdirectory. An example of what might be displayed when "ls -l" is entered would be: drwx-----x 3 doe other 25955 Mar 19 12:06 ---------- - --- ----- ----- ------ ----- 1 2 3 4 5 6 7 field Where: Field 1 specifies whether this entry is a (sub)directory or a file. If "d" is present as the first character, then it is a (sub)directory, else it is a file. Field 1 also specifies the TYPE OF ACCESS allowed for this (sub)directory/file. TYPE OF ACCESS values are discussed further under the "chmod" UNIX command below. Field 2 specifies the number of LINKS present. This value is not of concern to us at this time. Fields 3 and 4 specify the USER and GROUP who "owns" the (sub)directory/file. You are the USER. Field 5 specifies the size (in bytes) of the file this entry represents (if it is a file). Fields 6 and 7 specify the date and time this (sub)directory/file was last modified (updated). ls -a This command will display all subdirectories and files in the current directory including all "hidden" files. The most useful hidden file is the ".login" file which contains UNIX commands that are executed each time you log in to your account. ls -R This command will list the contents of the current (sub)directory and will then list the contents of all the (sub)directories which are listed in the current (sub)directory, and then will list all contents of all the (sub)directories listed in subdirectories of these subdirectories, etc. That is, the contents of the current (sub)directory and of all the subdirectories that are descendants of the current (sub)directory are listed on the screen. ls -C This command will list the contents of the current (sub)directory in columnar form and in sorted order by the name of the subdirectory or file. Note that there MANY other "ls" options available. See a UNIX text or use the "man ls" UNIX command for more information. The "*" and "?" wildcard characters can be used with the "ls" UNIX command. For example: ls -l prog*.c will cause all files/directories which begin with the characters "prog", followed by any set of one or more characters, followed by ".c" to be listed on the screen with full information about them. Thus, if the current directory contains: prog1.c prog2.c prog15.c prog19b.c then full information about all of these files will be listed on the screen. However, if the UNIX command: ls -l prog?.c is entered, then only full information about files "prog1.c" and "prog2.c" will be listed. -------------------------------------------------------------- 2. View the contents of an ASCII (character) file: less This command will display one screen full of the contents of the file specified by "filename" (the angle brackets are not to be entered). When you are ready to view more of the file contents, just press the spacebar and the next screen full of the file contents will be displayed. The file specified by "filename" must be a character file and must not be a "binary" file (executable file). To have the contents of the file scroll "up" (that is back toward the top of the file) one screen full, press the lowercase "b" key. cat This command allows you to (a) view the contents of more than one file as one document, (b) combine two or more files into one file. a. To view two or more files as a single document (one screen full at a time), enter from the UNIX command line: cat file1 file2 ... filek | less The above command says to display on the screen all files listed between the term "cat" and the '|' pipe character. The listing of these files is "piped" to the UNIX command "less" so that the contents of the concatenated files are displayed one screen full at a time. See a UNIX text for a description of further uses of the pipe character. b. To combine two or more files into a single file enter from the UNIX command line: cat file1 file2 ... filek > concatenatedfile The above command will create a file having the name you use in place of "concatenatedfile", and will write and save to that file the entire contents of the files listed between the term "cat" and the '>' output re-direction character. There are many options which can be used with "cat". Consult a UNIX text or use the UNIX "man cat" command for further information. 3. Copy a file from one (sub)directory to another: cp This command will copy the contents of the file specified by "source.file" to the file specified by "destination.file". File "source.file" must already exist or an error occurs. Example: cp indata input.data will copy the contents of file "indata" to the file "input.data". If file "input.data" already exists, then it is overwritten with the contents of file "indata" (and thus the former contents of file "input.data would be destroyed). If file "input.data" does not exist, it is created and then "indata" is copied to "input.data". "Path names" can be used to designate "source.file" and "destination.file". Path names are discussed below. You can copy a file from one of your own (sub)directories to another, or from someone else's account to one of your (sub)directories by using specific "path names". Example: cp ~csci455/prog.data/indata programs/input.data will copy from the subdirectory "prog.data" (which is located in the subdirectory associated with the computer account whose user-id is "csci455") the file named "indata" to "programs" which must be a subdirectory in your current directory. The file copied will be given the name "input.data" in subdirectory "programs" (the subdirectory "programs" must have already been created). The character "~" is called a "tilde" and is used as a short-hand method for specifying some portion of a "path name". If we had not used the "~", we would have had to specify the complete path name of the directory (csci455). For example, if the "~" were NOT used, we would have to write: /tmp_mnt/auto/chaph5/csci455 to do the same thing ~csci455 does. Multiple files can be copied with one "cp" command if the "wildcard characters" are used. For example, if the following UNIX command is entered: cp ~csci455/gen/program* . then all files beginning with "program" and ending with any set of characters will be copied from the "gen" subdirectory of account "csci455" to your current directory and under the same file names. Notice the single period at the right extreme of the command above. This period MUST be present (the period tells the UNIX command interpreter to copy the specified files to your CURRENT DIRECTORY). There must be at least one space between the period and the character to its immediate left. 4. Change the name of a file: mv This command will change the name of file "current.filename" to "new.filename". Example: mv a.out prog1 will change the name of file "a.out" to "prog1". From this point on, to access the file that was originally named "a.out", you will have to use the name "prog1". The change of name is automatically reflected in your (sub)directory. If a file with the name "prog1" already exists, an error is reported and no name change is made. mv This command will copy the contents of file "current.filename" (stored in the current subdirectory) to subdirectory "subdir" and will give it the name "new.filename". The file named "current_filename" will then be deleted (unless it has other links to it). Wildcard characters can be used with the "mv" command. 5. Directory and subdirectory commands: A. Change the current (sub)directory to another: cd <(sub)directory> This command will make your current (sub)directory the (sub)directory specified by <(sub)directory>. Any file commands which act upon the "current" (sub)directory will now act upon only the files contained in the (sub)directory specified by <(sub)directory>. Path names can be used to specify <(sub)directory>. Assume directory "A" contains directory "B". Directory "A" is called the PARENT of directory "B", and "B" is called the SUBDIRECTORY of "A". To get from a subdirectory back to its parent, just enter: cd .. from the UNIX command line, and the current directory will then be the parent directory of that subdirectory. There is a space between the 'd' and the first period. cd / This command will cause the SYSTEM ROOT DIRECTORY to be made the "current directory". The SYSTEM ROOT DIRECTORY is the directory from which all files and other (sub)directories in the entire system can be accessed. If you have moved to (sub)directories far removed from your HOME directory, you can move immediately back to your HOME directory by entering: cd ~ There is a blank between the 'd' and the tilde character. Your HOME directory is the (sub)directory that you are put into when you log in to your account. You can use the "cd" command to move to (sub)directories which are not in your own computer account. For example, you can move to the "gen" subdirectory of the CSCI-101L account by doing: cd ~csci101/gen If you would like to know the full path name of the current (sub)directory you are in, enter the UNIX command: pwd This full path name begins at the system root directory name and shows all the (sub)directories from the system root directory to your current (sub)directory. B. Make a new subdirectory: mkdir This command will create the subdirectory named as a subdirectory of the current directory. To move to this new subdirectory, you must use the "cd " command. C. Remove a subdirectory: rmdir This command will remove (delete) the subdirectory named "subdirectory". To use this command, the current directory must be the "parent" of the subdirectory specified in the "rmdir" command. In addition, to remove the specified subdirectory, the latter must have all of its files and subdirectories removed (using the "rm" or rmdir commands) prior to executing the "rmdir" command. If the subdirectory specified by "subdirectory" is not empty of files and subdirectories, you will get an error message and no action will be taken. D. Remove a file from the current (sub)directory: rm This command will remove (delete) the file specified by "filename" from the current directory. Once a file has been removed (deleted) it cannot be restored, so be careful when removing files. Multiple files can be removed with one command as in: rm ... NOTE: Actually, if you accidently remove a file you did not mean to, you can have that file re-established by contacting the consultant in SAL-125. It will cost you a small fee, however, to re-establish the file, so be careful what you "rm". The wildcard characters can be used with the "rm" UNIX command, but be careful. For example: rm * will remove EVERY FILE IN YOUR CURRENT DIRECTORY. To reduce the chance of accidentally removing files you do not really want to remove, place the following line as the new first line in your ".login" hidden file on a line by itself: alias rm rm -i This command will force the operating system to prompt you as to whether you really want to remove a file whenever you enter the "rm" command. -------------------------------------------------------- 6. Print the contents of a text file: To find out the command to print out the contents of a file on a printer is SAL-125, ask the consultant in SAL-125 what the print command is. The command tends to change from time to time, so I cannot include it here. You can also phone (213) 740-5555 and get the UNIX consultant who will be able to give you the print command. -------------------------------------------------------- 7. Using the e-mail facilities: When you get your computer account, you automatically get an e-mail account. Other persons (on campus, off campus, in another city, or even in another country) can send you e-mail and you can send e-mail to others. A. Reading your e-mail. When you log into your account, you may see the message: You have new mail. displayed on your screen. This means you have one or more e-mail messages waiting in your "mailbox". To read the waiting e-mail messages enter from the UNIX command line: mail and you will see a list which contains a brief description of each of the e-mail messages stored in your "mailbox". You will also see the '&' character on a line by itself following the list of e-mail messages. The '&' character is the "e-mail command line prompt". The '&' tells you that the e-mail facility is now waiting for you to enter an e-mail command. To have the first message displayed on the screen, press the ENTER (RETURN) key when you see the '&' command line prompt. When you have finished reading the message, press the ENTER key again to see the next e-mail message. After reading a message, you can take several actions including: a. To save the message just displayed to a file for later reference, enter: s at the e-mail command line prompt. Where "filename" is the name of a file you wish to have created to store the e-mail message. "filename" can be any legitimate UNIX file name. After leaving the e-mail environment, you can use a "cat", or "less" command to read file "filename", or you can bring file "filename" up in the emacs editor (or VI editor) for reading or editing. b. To respond immediately to the e-mail message, you can enter: r at the e-mail command line prompt. You can now type a return message that will automatically be sent back to the person who sent you this e-mail message. Once you have completed typing your return message, press the ENTER key to put the screen cursor on a new line and then press CTRL-d to send the response. c. To delete (throw away) this message, enter: d at the e-mail command line prompt. The message just read will be deleted from your "mailbox" and will no longer be available to you (i.e. it will be gone forever). To undelete a message, enter: u where "message number" is the number of that message in your list of messages. d. If you wish to read a specific message from your list of messages, just enter the integer value that represents the list position of that message. For example, to read the third message on your list of messages, just enter: 3 at the e-mail command line prompt. e. To see next message either press the ENTER key or enter "+". f. To see the current message again, enter "p". g. To see the previous message again, enter "-". h. To forward the current message (the one you just read) to another person, enter: m and the current message will be forwarded to the person whose user-id you specified. The current message will also be marked for deletion (as if you had done a "d" command on it). B. To send an e-mail message to someone else, enter from the UNIX command line: mail This command will send an e-mail message to the person whose user ID (i.e. account name) is that specified by . When this command is executed, the prompt Subject" will appear on the screen. Type the subject of the e-mail message on a single line, and then press the ENTER key. The cursor will then move to a new line and you can type a multi-line message. When you have completed typing your message, press the ENTER key to move the cursor to a new line. Now, press and hold the CTRL (control) key while at same time press the "d" key once. Your message will be forwarded to the person specified. If you want to mail a file to someone, just enter: mail < filename Note the "<" character to the left of "filename" must be present. This will cause the contents of file "filename" to be mailed to the account specified by . File "filename" should be an ASCII (text) file. C. To exit the e-mail facility do one of the following: a. From the e-mail command line prompt, enter: x and you will be taken out of the e-mail facility and returned to the UNIX command line. The e-mail messages which were in your mailbox when you left the e-mail facility will remain in your mailbox unaffected (i.e. no messages will be removed or altered). If you get back into your mailbox, you will find all the messages still there for you to read again. b. When you have read all the messages you desire to read, you can exit the e-mail facility and have all the messages you have read thus far saved to a file named "mbox" by entering: q at the e-mail command line prompt. Note, however, that those e-mail messages that you did not read before exiting the e-mail facility will remain in your mailbox (and will not be stored in "mbox") and can be accessed again by re-entering the e-mail facility. You can examine the contents of file "mbox" using the "less" UNIX command, or you can bring "mbox" up in the emacs editor for reading and editing. All messages marked for deletion using the "d" command will be removed from your mailbox and will NOT be saved in file "mbox". D. To get help on how to use the e-mail facility and its commands, enter: ? at the e-mail command line prompt. ------------------------------------------------------ 8. Changing the file/directory access mode: This UNIX command is used to add (give) or take away (remove) read, write, or execute permission to the file/directory specified in the command. General format: chmod <+|-> where u means "user" = the owner of the file/directory g means "group", which you can disregard at this time 0 means "others" = others (not the user and not members of a group) a means "all" = user, group members, and others. where + means "add" access permission for u, g, o, or a - means "remove" (disallow) access permission where r means READ permission w means WRITE permission x means EXECUTE permission where "filename" is the name of the file whose access mode is being changed. where "directoryname" is the name of the directory whose access mode is being changed. Examples: chmod a+r myfile This command will give "read" access to file "myfile" to all persons who have a computer account (i.e. anyone is allowed to read the contents of file "myfile"). chmod o-r mysubdirectory This command will deny "read" access to directory "mysubdirectory" to persons who are not the owner of the directory or members of a specific group identified by the owner (others). When a file/directory is created, its default access mode is "no access" by group or others. Leave it this way unless you have good reason to change it. (the character in front of the minus sign is a lowercase oh). chmod a+w myfile chmod o-w mysubdirectory will have the same effect as discussed for a+r and o-r, except you are granting or denying "write" access to the file/directory specified. If you give write access to "others" (o) or "all" (a) to a file or directory, it can be changed or destroyed by anyone who has a computer account at USC. Thus, you would almost never want to give "read access" permission for any of your files or directories. chmod a+x myfile has the same effect as discussed for a+r and o-r, except you are granting or denying "execute" access to the file specified. If you give execute access to "others" (o) or "all" (a) to the file, it can be executed by anyone who has a computer account at USC. The file "myfile" must be an executable (compiled) program and not an ASCII (character) file. The "x" execute permission is not usually used with subdirectories. 9. Compiling a source code file: To compile a program written in C, do: gcc -o The file specified by "sourcecodefilename" must be an ASCII (character) source code file and must have the extension ".c" added to the file name. If any syntax errors are found in the source code, appropriate error messages are displayed on the screen. You correct these errors and compile the source code again. When the source code is compiled and no error messages are displayed, then your source code has been successfully compiled to machine (or executable) code. In such a case, the source code file has been translated into a machine code file with the name you specified by execfilename. File execfilename is said to be the executable form of the program and can be run (executed). -------------------------------------------------------- 10. Running a compiled program: Once a program is successfully compiled, it can be run (executed). To run a file, get to the UNIX command line and type the file's name and press the RETURN key. The program will execute successfully, or it will begin to execute and crash. If the program crashes, it is because there is one or more semantic errors in it. You must correct the semantic error(s), recompile, and then try to run the program again. 11. Getting help with UNIX or applications run on UNIX. In addition to using the "man" command referred to in the first section above, you can also get help in using UNIX or many of the applications (e.g. emacs, OpenWindows, the dbx debugger, etc.) you may be running on UNIX by attending the special 2-hour free classes offered by UCS (University Computing Services). There is a separate 2-hour classes on most of the system applications you will want to run under UNIX. To see the time, date, and location of these classes, just enter: help from the UNIX command line. You will then see a lengthy menu of topics you can get information about. To get the information on the 2-hour classes, enter the integer digit preceding the word "classes". To get information on when the 2-hour classes on UNIX are availabel, enter the integer digit preceding the word "unix". To get immediate help with using the UNIX operating system and/or system applications, you can see the consultant in SAL-125, or phone (213)740-5555 and get the UNIX consultant.