ITEC 1011 A B
Week 11 Lab – Nov 23 24, 2000

 

Unix

In this lab you will learn how to use a UNIX system. With this operating system, commands are typed in directly rather than by pointing and clicking with the mouse. You have already seen an example of this: after logging in to your e-mail account, the e-mail program is run by typing "pine" instead of pointing and clicking on an icon. To use the UNIX operating system it is important to know the commands used to manipulate files and run programs. That is the purpose of this exercise.

1.

Logging in to E-mail

 

The mail server on which you check you e-mail is a UNIX machine. When you login to the mail server, after the initial announcements, the first thing you see is a % sign. This is called a "prompt". Whenever you see the prompt, it is "prompting" you to enter a command (such as "pine").

 

 

 (f1)

 

 

o        Log into your e-mail account.

 

2.

Simple UNIX commands (date, cal, who, finger):

 

 

o        At the command prompt type "date" and press return. The mail server computer will return the current date and time.

o        Now type "cal" and press return. The mail server will output a calendar for the current month.

 

 

 (f2)

 

 

o        The "cal" command can also return the calendar for any other year. For example, to display the calendar for the may (5th month) in the year 1775 we type in "cal 5 1775". The numbers following the "cal" command are called the "arguments" of the "cal" command.

o        Now type "who" and press return. This command will output a list of the users currently logged in to the mail server system.

o        The "finger" command is used to identify who a user is on the system. It takes a user name (such as beamish, or yu215707) as its argument. To find out who the user "beamish" is type "finger beamish" and press return.

 

3.

Getting Information: the "man" command and "whatis".

 

 

The "man" command man gives you access to an on-line manual which potentially contains a complete description of every command available on the system. In practice, the manual usually contains a subset of all commands. "man" can also provide you with one line descriptions of commands which match a specified keyword

 Examples of using the man command: (Please enter these commands)

o        To display the manual page for the "finger" command, type "man finger" and press return.

o        To display the manual page for the "man" command itself, type "man man" and press return.

o        The man command also allows searching for all commands involving a specific keyword. To search for all commands involving "mail" type "man -k mail" and press return.

o        You can also return a one line description of what a command does by using the "whatis" command. For example, to return a one-line description of the man command type "whatis man" and press return.

 

4.

Changing Your Password in UNIX: The passwd command.

 

 

You don't have to change your password, but should you want to this is the command to do it.

To change your password: enter the command passwd and then respond to the prompts by entering your old password followed by your new one. You are then asked to retype your password for confirmation.

Note that what you type will not appear on the screen for security reasons. Don’t forget your password!

 

5.

The File Structure in UNIX: ls, cd, pwd

 

 

In UNIX each user has a unique "home" directory. Your home directory is that part of the file system reserved for your files. After login, you are "put" into your home directory automatically.  You are in control of your home directory and the files which reside there. You are also in control of the file access permissions (to discussed later) to the files in your home directory. Generally, you alone should be able to create/delete/modify files in your home directory. Others may have permission to read or execute your files as you determine. In most UNIX systems, you can "move around" or navigate to other parts of the file system outside of your home directory. This depends upon how the file permissions have been set by others and/or the System Administrator, however. Unlike the Windows operating system in which we have Explorer to browse through the file structure, in UNIX files can only be browsed by issuing commands at the prompt.

 

 

o        To view the files in your "current working directory", type in the command "ls" and press return (ls is short for list). The "ls" command will output a list of all the files which are in your current working directory.

 

 

 (f3)

 

 

o        Now enter the command "ls -l" and press return. This tells the "ls" command to output using "long" format which reveals more information about the files in the directory. From the list, we can see that "Mail" is a directory.

o        By default, when you login your "current working directory" is your home directory. To view what your current working directory is type the command "pwd" and press return (pwd stands for print working directory). The resulting output is the path of the current directory from the root directory.

o        To change your current working directory we use the command "cd" for change directory. This command wants the name of a directory for its argument. We will change directory into the "Mail" directory. Type "cd Mail" and press return. Do not forget the capital M on Mail. Remember that UNIX filenames are case sensitive.

o        Type "ls" again and press return to list the files in the Mail directory.

o        Type "pwd" and press return to see the path of the current working directory.

o        To go back into the previous directory we give the command "cd .." and press return.

o        Now type "ls" and press return. Observe that the files you see are the ones in your "home" directory.

o        Type "pwd" and press return to see the path of the current working directory. It should be the same as before.

 

6.

Creating and Deleting directories

 

 

In UNIX, directories are created using the "mkdir" and "rmdir" commands which make or remove a directory respectively.

o        Type the "ls" command and press return to list the files in the current directory (which should be your home directory if you followed the instructions above).

o        We will create a directory called "temp" in the current working directory. Type the command "mkdir temp" and press return.

o        Now give the command "ls" again. Observe that "temp" now appears in the list of files.

o        Type "cd temp" and press return to make "temp" the current working directory.

o        Type "ls" and press return. The list of files you get is empty because there is nothing in that directory.

o        Type "cd .." and press return to go back into the previous directory.

o        To remove the directory we just created type the command "rmdir temp" and press return.

o        Type "ls" and press return. Observe now that "temp" does not appear in the list of files--this directory has been removed. It is only possible to remove directories if they are empty. So before you remove a directory you must delete all the files in it (more on this later).

 

 

 (f4)

 

7.

Viewing and Editing Text Files: pico, cat, more

 

 

In Windows, we used the Notepad program to edit text files. In UNIX, the program we will use is called pico. It allows creation and modification of text files.

o        Give the "ls" command to see what is in the current directory.

o        We will create a file called "blah.txt" in the current directory. (If you followed the previous instructions, you should be in your current directory.) To begin editing this file type the command "pico blah.txt" and press return. The window which opens looks exactly like the one used to edit e-mail. Type something into the text files (for example blah blah blah....).

 

 

 (f5)

 

 

o        To save the text file press CTRL-X, choose "Y" to Save Modified Buffer, and then hit enter to accept the filename.

o        Type "ls" again and press return. Observe that there is now a file in the directory called "blah.txt". This is the file you just created. To modify this file again, we would type "pico blah.txt" and the pico would begin modifying the file (you do not have to do this last part).

o        Another command which is useful is "cat". This will display a file without modifying it. Type the command "cat blah.txt" and press return. The output is your file which you created.

o        A very similar command is "more". This is just like the cat command except it will pause after each screen full of information so you have a chance to read it. Type "more blah.txt" and press return. Since blah.txt is not long enough to fill an entire screen more displays the file without pausing (just like the cat command).

 

 

 (f6)

 

8.

Copying, Moving, and Deleting Files

 

 

o        Give the "ls" command to see what is in your directory. You should have "blah.txt" from the previous instruction.

o        The command for copy is "cp". We will create a copy of the blah.txt called blink.txt: type the command "cp blah.txt blink.txt" and press return.

o        Give "ls" again and observe that there is a new file blink.txt in the directory. Type "more blink.txt" and press return to verify that this is in fact the same file.

o        The move command "mv" is like copy except the original file is destroyed after the copy is made. We will move the blink.txt file to the file blue.txt. Type "mv blink.txt blue.txt" and press return.

o        Give the "ls" command and observe that blink.txt is gone, and there is a new file blue.txt.

o        The command to delete a file is "rm" for remove. To remove the file blue.txt, type the command "rm blue.txt" and press return. Give the ls command and observe that the file blue.txt is now gone.

 

 

 (f7)

 

8.

Redirection and Pipes

 

 

When a command generates output, the computer usually writes the data to the screen. It is possible to redirect the output of a command into a different file. For example, you could redirect the output from the who command (which lists everybody who is logged in) into a file. We will redirect the output of the command who into a file called "whodat.txt"

o        Give the command "who > whodat.txt" and press return. The right angle bracket (>) tells the computer to send the output of the who command to the file whodat.txt.

o        Type "ls" and observe that there is a new file called "whodat.txt" in your directory.

o        To view the file type "more whodat.txt" and press return.

o        If you redirect output to a file that already exists, the previous file is destroyed and the redirected output replaces it. To append redirected output to a file that already exists, use two right angle brackets instead of one. Give the command "finger >> whodat.txt" and press return.

o        Now type "more whodat.txt" and press return. The output of the finger command is appended to the whodat.txt file.

Pipes are a variation on redirects: they allow you to redirect the output of a command into another command. An example of why you would want to do this is again the who command. Since the output is long we might want to "pipe" its output into the more command so it will pause after each screen page of information.

o        Give the command "who | more" and press return. The output will then pause so you can view it.

9.

Attaching Files to E-mail messages (and your assignment)

 

 

If you are on the course mailing list, you should have received a message from saying "DO NOT DELETE THIS MESSAGE". This message has a file attached to it. The first thing we will do is save the attachment in our directory.

o        Type "pine" and view the message.

 

 

 (f8)

 

 

o        Press "V" to view/save attachments.

o        Press "S" to save the attachment and then hit enter to accept "attach.txt" as the filename to save as.

o        Quit pine and give the ls command. You will see a file called "attach.txt" in your directory.

o        Type "more attach.txt" to view the file. Remember what is in this picture, it is part of today's assignment!

Now we will create a file and attach it to the e-mail for today's lab assignment. The file will contain a calendar generated using the cal command for the month containing your birthday (or any other date, I don't really care).

o        To generate the calendar you give as arguments to the cal command the number of the month followed by the year. For example, May (5th month) in 1492 would be "cal 5 1492". Make sure the calendar is what you want.

o        Redirect the output into a file called "cal.txt". Type (using the above example) "cal 5 1492 > cal.txt" and press return.

o        Type "more cal.txt" to view the contents of the file.

o        Open pine and start composing a message to "ta1011a@mathstat.yorku.ca" or "ta1011b@mathstat.yorku.ca"

o        Make the subject "nov26: lastname studentnumber".

o        In the "Attach:" field, put the filename "cal.txt". This will attach the file "cal.txt" to the e-mail: it will be sent with the message.

o        In the message body, describe the contents of attach.txt. This proves to me that you successfully saved and viewed the attachment.

o        Send the message.