Lab #2
CS 2351
UNIX Operating System
Name _________________________________
Section ___________________
This lesson will cover the following UNIX commands: man, who, more, pipe, finger, cat, ls, cp, mv, redirect, append, and rm. Wildcard characters are introduced. |
Man Command
In these labs, you will be learning about different commands. Commands tell UNIX to run a program by that name. The commands were written by programmers to make using the system easier and faster. More information on the commands can be found in your UNIX text. You can also obtain information about commands on-line on the computer. System documentation is stored on-line and is accessed by issuing the command:
man <name> <cr>
where <name> is the name of the command and <cr> represents the enter key on the terminal. Generally, the symbol <cr> will be omitted in these lab assignments but the appropriate key (enter/return) must be pressed in order for the command to be issued.
If you make errors when you are typing in any commands, use the backspace key to back up on the line and fix them if you have not pressed the enter key.
Who Command
In order to try out the man command, issue the command:
man who
You will get on-line information on what the who command does. The first information you will see is:
Reformatting page. Please wait ---- done.
User commands who (1)
Name
who - who is on the system
The "Name" gives the definition of the command. Next, you will see more information on what who does and some options that are available. There will be a --More (5%) message at the bottom of the page. To see more information, press the space bar key. To quit reading the man pages before the end of the file is reached, type q followed by the enter key. You are back to the prompt line when you see a $ prompt on the left of your screen.
Read what the who command does. Explain what the who command does? _____________________________________________________________________________
_____________________________________________________________________________
Next, issue the command: who
The who command will tell you who is on the system at the same time as you are. It lists the person's login names, terminal line, login time, and terminal name (this is the location of the person at that time). If there are many people on the system at the time that you try this, you may have several names go by quickly on your screen. In order to see all of the names on one screen at a time, the more command is used.
More and the Pipe Command
You can use the more command to display files on your terminal one screen at a time. It can be used with a | (vertical bar) with the who command so you can scroll one page at a time. In response to the more prompt, press the space bar to continue to the next page. The more command can also be used to allow you to view a long text file on the screen. An example of this would be if you had a text file called chapter1, you could type in more chapter1 and it would be listed one screen at a time.
The symbol for a pipe is a vertical bar (|). A pipe connects the standard output of one command directly with the standard input of another command. It connects commands together. Standard output is defined as the computer screen. In order to see how more works, type in the following:
who | more
You will see one screen of information at a time. When you want to see the next screen, press the space bar. Something different happens if you press the enter key. Try it. What happens when you press the enter key? ____________________________________________________________________________
Next, issue the command:
who am i
to obtain your login id, your terminal name, and the date and time of the login to the present session.
Your login id is ________________________________________________________________
Your terminal line is: ____________________________________________________________
The date and time of your login to this session is ________________________________________
The name of your terminal location is ________________________________________________
As you learn more commands, you will see that there are different options that are used with commands. There is always a space after the command and the use of an option. Options start with the - (minus sign) and then some letter. These add more flexibility to commands. Next is an example of the who command with an option.
Type in: who -q
The -q lists the login names of the people horizontally who are on the system and also gives you the total number of users. How many users are on the system now? ___________________________________
Finger Command
A command closely related to the who command is the finger command. Finger provides more information on the users who are on the system at the same time as you are. Issue the command:
finger
In order to see one screen at a time, type in:
finger | more
What is the difference between who and finger? ________________________________________
____________________________________________________________________________
____________________________________________________________________________
The finger command displays information on the user's login name, real name, terminal line, idle time, and login time and where they are connected to the computer. Idle time means that the user has not been using the computer for some amount of time although the person is still logged on to the computer. If the idle time is blank, then the person is using the system. If it is a single integer or just two integers, the time is in minutes. It shows hours and minutes if a ":" is present. Login time is displayed as month, day, hours, and minutes.
You may see an * (asterisk) next to the terminal names for some people. This has to do with a feature that allows you to write or talk directly to someone who is logged on to the system at that time. If there is an asterisk next to the terminal name, the person does not want to talk to you at that time. Some people don't want to be disturbed when they are typing in a program or document so they turn off this feature. If you want more information on talk or write, you can learn about them from the text book or read more information on the man pages. The default setting for this computer is "mesg y" meaning that people can send messages to you or you can send them to others. For more information on finger, you can also check your text, or you can check the man pages.
It is also possible to obtain the login id of a user by typing the real name of the user. An example would be if the name you were looking for was "Jason Jones", you would type:
finger Jason Jones
Use the finger command with your name.
What is the result?______________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
If you have a common first or last name, such as "John Smith," you will get a list of all people with the name "John" and all those with "Smith". This could be a very long list. What is the command to obtain login information about yourself and other persons with the same first or last name as yourself and display the information one screen at a time? ____________________________________________________________________________
If you know someone's name, you can use the finger command to find out their login id name so you can find out if they are on the system or so you can send them e-mail.
Cat Command
The cat command has two functions. Later, you will learn a third function of the cat command. These two functions are:
1. To display the contents of text files. In the first lab, you worked on a file called practice. In order to see how cat displays the contents of a file, type in the following:
cat practice
The file practice will be listed. The file is only listed and you cannot make changes in it. In order to make changes, you would have to use the vi editor. If you wanted to change the file, "practice," you would type in vi practice.
2. To concatenate (link or join) one or more files together and then send the result to the standard output. The standard output is the terminal screen.
In order to try out some different ways to use the cat command, make three new files. Use the vi editor to create these files. Type in:
vi filea
Be sure to press the i key for insert to start the files.
The filea should contain this line:
THIS IS filea.
After you have typed in this line, press the esc key. Then use the :wq to save the file. Next, use the vi editor to type in:
vi fileb
It should contain this line:
this is fileb.
Save this file using the :wq command and make a third file. Type in:
vi filec
It should contain this line:
THIS is filec.
Save this file. Now you will try different ways to display the contents of these files on the terminal by using the cat command. Display the contents of each file in turn by issuing the commands:
cat filea
cat fileb
cat filec
Now issue the commands:
cat filea; cat fileb; cat filec
How does the result from this sequence of commands compare
with those above?
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
Issue the command:
cat filea fileb filec
What appears on the screen? ____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
Use of Wildcard Characters
It is possible to abbreviate file name references for existing files by using either of two characters, namely * or ?, as a substitute for characters in a file name. These characters are referred to as wildcard characters when used as a substitute for characters in a name. Issue the command:
cat file*
What appears on the screen? ____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
Issue the command:
cat file?
What appears on the screen? ____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
Now issue the command:
cat fil*
What appears on the screen? ____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
Issue the command:
cat fil?
What appears on the screen? ____________________________________________________________________________
Note that one of these wildcard characters matches zero or more characters within a filename. The other matches only a single character within the name of an existing file. After trying the above commands:
what does the * do? ____________________________________________________________
what does the ? do? ____________________________________________________________
Redirect Symbol
When you use the cat command, the output goes to the terminal screen. The cat command can be used to redirect the standard output to a file instead of the screen when the redirect symbol ">" is used in conjunction with the target filename. Issue the command:
cat file? > newfile
A new file with the name newfile should have been created. What are the contents of newfile? ____________________________________________________________________________
What command did you use to display newfile?_________________________________________
If the file did not exist before you did the redirect, UNIX will create it. If the file already exists, its contents will be replaced. The original contents are gone and you cannot get them back!
One way to prevent a file from being overwritten is to use the append (>>) command instead of the redirect (>) command.
Append Command
Occasionally, you may want to add a file to the end of another file. This is called appending a file onto another file. In order to do this, you use the >> (two greater than signs). This would be used with the cat command. To see how the append command works, type in the following:
cat practice >> newfile
List out the newfile again. What happened to newfile as a result of the previous cat command?
____________________________________________________________________________
If the file did not already exist, it would be created. If you already have a file by that name, it will be added to the bottom of that file. Using the append option can be a way to protect files you have made. When a file does not already exist, there is no difference which method you use, the redirect or append. However, if you are not sure about if a file exists, then using the append is safer.
Ls Command
The command ls (This is the lower case L) lists the files in your directory. It lists them across the screen in columns. Two options used frequently with the ls command are -a and -l; these may be combined as -al . Issue the command:
ls
What was listed to the screen?
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
Next, try out the following:
ls -l
What was listed to the screen? (list all of the information of at least 4 of the lines) ____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
The -l option causes ls to generate a long listing. This will list more information about the files in the current directory. The output is similar to the following:
Total 10
drwxr-xr-x 2 jjs jjs 512 May 6 13:55 Labs
-rw------- 1 jjs jjs 10318 Oct 7 2001 practice
The first line is a count of the total number of blocks (1024 bytes in a block) of storage that the listed files use. Each following line contains detailed information about a file in the directory. In left to right order, the columns have the following meaning: the first one corresponds to access permissions; second, the number of links; third, owner's name; fourth, group owner of file; fifth, size of file in bytes; six and seventh, date and time of last modification; and last is the filename.
The access permissions consist of three groups of three permission categories and an entry which designates whether the file is a directory. If the access code begins with a "d", then the file is a directory. If there is a "-" in the first position, then it is a file. The format is:
drwxrwxrwx to
indicate a directory
-rwxrwxrwx to
indicate a file
Read, write and execute permissions are indicated by the letters r, w, and x, respectively. Non-permission is indicated by the character "-". The permission categories from left to right are user, group (project), and world (other). Thus the following access permissions for a file indicate read and write permission for the owner, read access for the group (project) and read access for everyone:
-rw-r--r--
Next type in:
ls -a
What was listed to the screen?
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
Filenames that begin with a "." (period)
are called invisible filenames. Some files that are invisible files are .profile, .cshrc, .kshrc,
and
"." and "..". You will work with some of these files in some later
labs. Invisible files are used to customize your account to fit your needs more
specifically. They work when you type in certain commands. They are not listed with the
regular ls command because they would clutter up the directory. The -a
option is used to list all filenames including the invisible filenames. Do you have any
files now that are invisible files? What are the names of the invisible files that are in
your working directory?
Type in:
ls -al
What was listed?
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
The "ls -al" lists out all information on files and also lists all of the invisible files.
Cp Command
The cp command is a copy command. It will copy one file to another file. It will also copy one or more files to a directory. You will learn about directories later. There is a source-file which is going to be the file that will be copied to another file, the destination-file. If the destination-file already exists, it is overwritten and any information in it is destroyed. After one file has been copied to another, the two files should be exactly the same. An example of the copy command is:
cp source destination
Now, use the vi editor to create a new file called source. It should contain the following line:
This is a test file for cp--the copy command.
Save the file and exit from vi. Now examine the contents of this file by issuing the command:
cat source
Next copy the file named source into a new file
named destination. Show the complete command you used to accomplish this:
____________________________________________________________________________
Issue the command:
cat source
Did the file source change after the copy was made? ____________________________________
Next issue:
cat destination
Is the file destination the same as the file source ? _____________________________________
Issue the command:
ls -l
What files were listed? ____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
Earlier, you created a file called newfile. Use the cat command to look at the file again. Now issue the following command:
cp source newfile
Now look at the file newfile. What happened to the
contents of newfile?
____________________________________________________________________________
Mv Command
Sometimes you want to change the name of a file or move a file from one directory to another. This can be accomplished by the mv command. It is similar to the cp command. The first argument is the name of the file to be renamed (or moved) and the second argument is the new name (or new destination). An example is:
mv file1 file2
The file1 name will be changed to file2. As in the case of the cp command, if the second file already exists, it will be overwritten when the name is changed. Also remember with the mv command, the first file no longer exists. The mv command can also be used to move a file to another directory. You will learn about directories in a later lab.
Issue the command:
mv source moved
Next issue the command:
ls -l
What happened to the source file? ________________________________________________
The Remove Command
You can delete files that you no longer want by using the command, rm.
Delete the following files from your directory by issuing the command:
rm moved destination
Issue the command:
ls -l
What files are now listed? ____________________________________________________________________________
____________________________________________________________________________
One problem when you use the rm command is that you dont get a second chance to decide if you want to delete a file. When you type in the rm and the name of the file, that file is gone! When you use a personal computer, you are asked if you really want to delete a file.
You can use the option -i with the rm if you want to be given an opportunity to decide if you want to keep the file. Here is an example: Assuming the file moved exists, and the user types in the following:
rm -i moved
the system will respond with:
rm: remove moved: (yes/no) ?
If you use the -i option you will get a chance to decide if you want to delete a file. The file is listed and then you can type in either a "y" for yes or "n" for no. In order to get some practice with this, type in the following:
rm -i newfile
When you are given a choice, type in y.
At the end of this lab, you should still have the following files:
practice
filea
fileb
filec
In order to see if you have these files, type in:
ls -l
Were these files listed?________________________________________
Please indicate any parts of this lab that were hard to understand. ____________________________________________________________________________
____________________________________________________________________________
05/08/03