Lab 7

UNIX II

Objectives:



Introduction

Since you now know the basics of getting around in the UNIX environment, it is time to start making use of its power.


Important Note

USE ACME AND KSH FOR THIS LAB OR RISK GETTING A ZERO!!!

If you do not know what "ksh" is, don't worry about it. You are fine. Just make sure that you do this lab on acme.


Aliases

If you find that you keep having to type the same long and hard-to-remember commands over and over again on acme, you should think about aliasing those commands. The format is as follows:

alias <name you can remember>="<command you can't remember>"

For example, you can prevent accidental deletion of files with the following alias:

alias rm="rm -i"

the ‘-i‘ means interactive, so the rm command will ask you for comfirmation before deleting a file. If you would later like to bypass the alias (say, because you want to delete 50 files), you can type:

\rm <files>

the backslash tells the system not to use your alias.


Finding Things

Finding Files

If you are a disorganized person or just forgot the name of a file created long ago, you can use the find command to find it again.

The generic format is something like:

find <starting dir> -name "<wildcardname>" -print



For example, to find all "*.html" files anywhere in your home directory, it would be:

find ~ -name "*.html" -print



However, this will also try to find matches for directories ending in .html. To limit find to strictly files, add "-type f":

find ~ -type f -name "*.html" -print



Finding Text in Files

To find out which file and which line contains a particular string of text, use the program grep.

Given a file and a pattern (or string of text), grep searches the text in the file by trying to match it to the given pattern. If the pattern is found, grep returns the line of text where the pattern was found. To use grep, type:

grep <pattern> <file>

for example, if you have a file named "lamb.txt" containing

Mary had a little lamb,

Little lamb, little lamb.

Mary had a little lamb.

Its fleece was white as snow.

and you wanted to find all lines that contained "Mary", you would type:

grep "Mary" lamb.txt

You would get:

Mary had a little lamb,

Mary had a little lamb.

Read the man pages for more details about grep.



Finding Programs

Sometimes you get the "command not found" error when you want to run a particular program. Why? Because when you type in the name of a program to execute, the computer searches for the program in a special list of directories. This list of directories is called a path. The computer looks through all the directories in your path to see if the program is located in one of those directories. If the program is found, great! The program will start as you expect. If, however, the program is not in your path, the computer will tell you that the command (program) is not found. To see what your current path is, type:

echo $PATH
If you get the "command not found" error, and you know you did not mistype the name of the program, you can find out where the program is by using whereis.


whereis

Whereis is a useful command for finding out where a program and its man pages are located on a computer. You use it by typing:
/usr/ucb/whereis <program name>
Whereis searches a list of standard places that programs often reside on a UNIX system. If it finds the program that you are looking for, it reports the directories that the program and its man pages are in. To run the program that you were looking for, you would type the full name of the program.

For example, if "elm" is not in your path for some reason, you would type this to find it:

/usr/ucb/whereis elm

and you would get

elm: /usr/local/bin/elm /usr/local/bin/elm.old

you would then type the following to run elm:


/usr/local/bin/elm


which

Which is a program that is similar to whereis. By typing:

which <program name>

it too returns the location of a program. However, instead of searching standard places that programs are placed, it searches your path. As a consequence, it is less useful than whereis.


Job Control

While some people are happy doing only one thing at a time, others like to do ten things at a time. This is easily accomplished in UNIX since it was designed for multi-tasking. Each program that you run is called a "job." You can kill, exit, and suspend jobs and put them in the foreground or background. This is called "job control." Here are some commands that have to do with job control:


ps


This command allows you to see which jobs you have started. It works like this:

The output will look something like this:

PID TTY TIME CMD

22740 pts/49 0:00 elm

22718 pts/49 0:02 emacs

22753 pts/49 0:00 sh

22752 pts/49 0:00 man

22754 pts/49 0:00 less

22710 pts/49 0:00 ksh



Control Commands

Note: ctrl means the control key.

ctrl - c

quits some programs. You will use this command a lot when you start writing C programs. ;)



ctrl - z

suspends all programs that do not explicitly try to catch this control command. It is a useful command because you need to suspend the program you are currently running before you can start up another program in the same telnet session.

For example, if you are running elm and want to start up tin without quitting elm, you would type ctrl-z. Then you get your acme prompt back, so you can type "tin". We'll tell you how to get elm back in a minute.

NOTE: pico has some strange annoyances. If you just run "pico" or "pico <filename", you cannot suspend pico with ctrl-z and start up another program. You must run pico as "pico -z" or "pico -z <filename>" to allow suspending with ctrl-z. You can also suspend pico with ctrl-s, but you will not want to. When you suspend pico with ctrl-s, your telnet session will refuse to respond to all keystrokes you type! If you type ctrl-s by accident, type ctrl-q to break out of it.



jobs

"jobs" tells you what programs you are running. If you are not at a prompt, type ctrl-z to suspend your current program. Type "jobs" at the prompt. You should get something like

[3] + Stopped (SIGTSTP) man man

[2] - Stopped (SIGSTOP) elm

[1] Stopped (SIGTSTP) emacs

The number on the left tells you the job ID number. Read on to find out how you would get a program back.



fg and bg

"fg" stands for foreground. You can put a suspended program in the foreground by typing

fg %<jobID>

So if you want to get elm back in the above example, you would type

fg %2

"bg" stands for background. When you suspend a program, it is not running anymore. However, sometimes you might want a program to actually run in the background while you run another program. To put a suspended program in the background, type

bg %<jobID>

So using the above example, if you want to put emacs in the background, you would type

bg %1

If you know that you would like to start a program in the background, you can put an "&" after the program name. For example, typing

elm &

will start elm in the background. To put it in the foreground, use the "fg" command.



top

Top is a fun little command that shows you all the processes currently running on the system. Before running top, it is a good thing to know that typing 'q' quits the program. To run top, simply type:

top

It will tell you who is hogging the system resources and making your life miserable.



kill

If a program has gone berserk and is no longer responding to you, you can kill it! And, unlike in Windows, the program will actually die! This is another very useful command for when you start programming in C.



How to Kill a Program




Spying and Socializing

lu

To look up the gt-account of anybody at Georgia Tech, just type

lu <Firstname> <Lastname>



finger

You can check whether someone has read your email by typing

finger gtXXXXX

finger also tells you other interesting info, such as whether that person is online and when he/she logged on.

If you finger your own account, you may notice that it says you have "No Plan." If you do have a plan, sig, or quote you would like everyone to see whenever they finger your account, create a text file called ".plan" and put that in there.



who

To find out where people are logged in from and when they logged in, type "who" at the prompt. Do this now.



w

To find out what other people are doing, just type "w" at the prompt. To see what you are doing, type "w gtXXXXX".



talk

To chat with someone who is online, use talk or ntalk. They work the same way. You use them like this:

talk username@domain

username is gtXXXXX and domain is acmex, acmey, or acmez.

This message will appear on the other person's screen:

Message from Talk_Daemon@acmex.gatech.edu at 15:04 ...

talk: connection requested by gtXXXXX@acmex.gatech.edu.

talk: respond with: talk gtXXXXX@acmex.gatech.edu

If someone requests to talk to you, you will get that same kind of message. To talk with the other person, just do what the message says. Type

talk gtXXXXX@acmex.gatech.edu

Check the man pages for more options.

Note: Think about what you want to say before you start typing. The other person can see what you type as you are typing it!



write

If you just want to send a short message to someone who is logged onto the same machine, use

write gtXXXXX

type your message and end it with ctrl-C.

You can only write to people that are on the same machine. So if you are on acmex and the other person is on acmey, log into acmey before trying to write. Also, writing to someone clutters up his/her screen, so be selective in when you do so. Check what the other person is doing before you start writing all over his/her screen. If someone sends a message and clutters up your screen, redraw the screen with ctrl-L.



mesg

When you are tired of random people asking to talk to you and writing messages across your screen, you can turn messages off with

mesg n

To turn them back on, use

mesg y




Pipes and Redirection

What is a Pipe (other than a blunt metal object)?


A pipe can be thought of as a bridge between two programs. More exactly, it is a mechanism used for sending (piping) the output of one program to the input of another. This is accomplished by using the '|' (a key that looks like a vertical bar and usually placed by the Enter key on the keyboard) such as:

<program 1> | <program 2>

For a more concrete example of how this works, take a look again at grep. As before, grep is normally called with a pattern to match and a file to search through:

grep <pattern to match> <file name>


That's not terribly exciting.... However, remember when you typed "who" and got that humongous listing of people that were logged onto the same acme as you? Now try this:

who | grep <your GT number>
Notice how the list of people is a lot smaller. What this does is run the who command, but instead of sending the output to your screen, the computer sends it to grep. Grep then takes that output and reads it as input. This effectively becomes the "file" it searches for some pattern - in this case your GT number. The result that grep returns is then output to your screen.

One technical note: For the pipe to work, the two programs must be compatible. This means that the program to the left of the pipe must send output to stdout (standard out) - for our purposes, this is the screen. Also, the program on the right side of the pipe must be willing to accept input from stdin - for our purposes, this is the keyboard.


What Does Redirection Mean?

There are two ways to redirect input and output. These are the '>' and '<' characters. For instance, when you type the 'w' command, you get a long listing of what people are doing online. For most of us, this listing goes by a bit too fast to read. Not to worry, using the '>' (out) redirect, we can send this output to a file.

Type this:

w > what.txt

Do not be alarmed by the lack of output. Yes, the program ran just fine. You didn't see any output because it was all sent to the file "what.txt". Now open up the file using pico or your favorite text editor. As you can see, all the output from the 'w' command is in this file. In general, the out redirect can be used to send output that would normally go to stdout (the screen), to a file.

Now type this:

grep <your GT number> < what.txt

This is a contrived example, but it means grep for your GT number in the file "what.txt". In general, the in redirect can be used to send input from a file to a program that accepts input from stdin (the keyboard).


What is the difference between the pipe and a redirect?
For the most part, the pipe is used to connect two programs. The redirects are used to send output to and read input from files.



Archiving and Compressing Files

Archiving Files
If you have lots of unused files that are too precious to delete but too cluttered for comfort, you can collect these files and stuff them into one file. For instance, if a directory had 200 files all containing quiz1 grades for the students that took the class last semester, we could put the directory and all its files into one file called "quiz1grades". Since all the files are together, we do not have to worry about losing any of them. One of the more popular file archiving utilities on UNIX systems is called tar.

To tar up a whole directory, type:
tar -cvf newFile.tar DirectoryToArchive

Giving the file a ".tar" extension lets you and others know that this file is actually an archive of numerous files.

To unpack files in a tarred archive, type:
tar -xvf file.tar

File Compression

In most cases, saving disk space is a good thing, and file compression will make your files smaller. File compression works by analyzing the file for repeated patterns and saving the patterns. For instance, if a file contained the following text:

107000000000000000000000000000000

a file compression utility might instead rewrite this as

107(30-0)

So, instead of writing thirty zero's, six characters were used to mean the same thing. Of course, a file in its compressed form is mostly unreadable. That is why we must decompress the file if we want to see it as it was originally. Decompression reverses the compression algorithm and explodes the file back to its normal size.

One of the most popular file compression utilities on UNIX systems is gzip.

To compress a file using gzip, type:

gzip -c FileToCompress > CompressedFile.gz

Like the ".tar" extension, the ".gz" extension tells you that this file needs to be decompressed before you can view it. To decompress a gzipped file, type:

gzip -d CompressedFile.gz


Tarred & Gzipped
Often times you'll see a file with an extension of ".tgz" or ".tar.gz". This means the file contains a bunch of files tarred together and was then gzipped to conserve space. To restore such a file to its normal state, type:

gzip -cd file.tar.gz | tar -xvf -

do not forget the hyphen after the "-xvf"! Of course, this is not the only way to decompress a tgz file. There are other switches and options you can use. If you are so inclined, ask your favorite Linux geek for his/her personal favorite.



Random Useful Commands

mkdir

One of the most basic and often used commands on a UNIX system is mkdir. This is short for "make directory". As you probably guessed, it makes a new directory inside your current one. To make a directory type:

mkdir <directory name>


rmdir
The opposite of mkdir is rmdir. Not suprisingly, this is short for "remove directory". To remove a directory, all files and subdirectories inside it must be removed first. To remove a directory, simply type:

rmdir <directory name>


du

Running low on disk space? To find out where all that space is being used up, use the "du" command. Du is short for "disk usage". It will report back the number of bytes (note that 1024 bytes is 1 Megabyte) being used in the current directory and all subdirectories. So, if you are in some high level directory like /usr and type du, expect to wait awhile! To see where all your quota space is going, type:

du ~


clear
When you are working on your computer and nosy people keep coming by trying to read what you are typing, a good way to discourage them (aside from physical assault), is with the "clear" command. As expected, clear wipes away all text currently on your screen. Just type:

clear


ping

If you try to telnet or FTP to a computer, and you end up waiting, and waiting, and waiting..., you can use the program "ping" to find out if the computer is up or just really slow in responding to your request. Ping is a popular network administration command that is used to find out if a remote computer is alive. In other words, ping sends packets of data to a computer on a network and waits for a response to come back. If no response comes back in a reasonable amount of time, ping reports that the computer is not available for network connections. Try this:

/usr/sbin/ping www.linux.org

You should get a message telling you that "www.linux.org is alive". This means, if you so desire, you can use netscape to bring up www.linux.org without worry that the site may be down.




Your Assignment


It looks long, but it is really not so bad.


  1. Make a directory called "lab7".
  2. cd into the lab7 directory.
  3. Run a session of tin.
  4. Suspend tin and put it in the background.
  5. Open up the man page for grep.
  6. Put this into the background.
  7. Open up a man page for man.
  8. Put this into the background.
  9. Run another session of tin.
  10. See what jobs you have running by using the "jobs" command.
  11. Run jobs again, but this time redirect the output to a file called "jobs.1"
  12. Run the ps command.
  13. Now run the ps command, but this time pipe your input into grep with the pattern "man".
  14. Do this one more time, but redirect your output to a file called "ps.1".
  15. Bring the first session of tin to the foreground.
  16. Quit this normally (you know, press "q").
  17. Run jobs again and redirect the output to a file called "jobs.2"
  18. Kill the remaining session of tin.
  19. Bring the "man grep" session of man into the foreground.
  20. Quit this normally (press "q").
  21. Run jobs again and redirect the output to a file called "jobs.3"

    Almost there...

  22. Run the who command.
  23. Run the who command, but this time pipe your output to grep with the pattern "res". This will give a listing of everyone logged in from a machine on resnet.
  24. Repeat step 23, except this time, pipe that output to grep with the pattern "gte". This will get you everyone logged in from acme whose gt number starts with "gte". (Hint: Use 2 pipes!).
  25. Repeat step 24, but this time redirect your output to a file called "who.1"
  26. Remember that other man session in the background? Bring it to the foreground and quit it normally.
  27. By now you should have five files in your lab7 directory. Unfortunately, turnin only lets you turn in one file. So, do "cd .." and tar up the entire lab7 directory into one file called "labStuff.tar".
  28. Gzip the "labStuff.tar" file and call it "labStuff.tgz".
  29. ftp the file back to your home machine (lab 2!).
  30. Turn in your labStuff.tgz file.
  31. Remove the lab7 directory.

Phew! Now do it blind-folded! Just kidding, just kidding.