9. Other Useful Commands

Viewing and Updating Personal Information

Users can see information about your account by typing:
     finger -m username
This information can be useful in reading and sending mail, to determine who sent you a message if the sender is identified only by login name, or to ensure that your own messages are directed to the right person.

If you want to add, delete, or revise information, use the nispasswd program:

     nispasswd -g
It displays, information it already has for you. For example, its first line might be
     Name [Pat Pending]
If ``Pat Pending'' is correct, just press Return. To change it, type the new value and press Return. To remove a value completely, type none and press Return. After you have responded to all the prompts, nispasswd gives the message ``Updating user information'' and exits. It may take one or two hours for the update to take effect.

Searching for Pattern Matches in Files

The grep (get regular expression) program searches in one or more files for a particular string of characters or pattern. There are three variants: grep, egrep (extended grep), and fgrep (fixed-string grep). Only grep and fgrep are discussed here. See the man pages for a discussion of egrep.

To use grep or fgrep, give the command

     grep [options] expression [file] ...
or
     fgrep [options] [string] [file]  ...
If you give no filenames, grep and fgrep search standard input (your terminal), and so they can be used at the end of a pipeline of commands (see chapter 4). The grep program expands special characters in the given expression. The fgrep program does not expand any characters, but searches for exact matches of the character string you specify. Normally, both grep and fgrep will search the files you specify and produce a list, on standard output, of every line containing the text string matching the string or expression you specified.

The grep command can expand special characters to find matches in much the way the shell expands special characters in file and directory names. Below is a simplified explanation of that expansion. The term ``special character'' here means one of the seven characters

     \    [    ]    .     ^    *    $
and a delimiter (such as apostrophe) used to mark the beginning and end of an expression. The delimiters are required if the expression contains any blanks or special characters, and they can be used even if the expression does not contain such characters.

Any nonspecial character matches itself. Thus

     grep 'man page' intro.draft 
searches through the file intro.draft for all occurrences of the expression ``man page''.

A period matches any character. So

     grep 'an.' intro.draft 
would find ``and'', ``any'', ``manual'', ``can'', etc. in the file ``intro.draft''.

If a set of characters is placed inside square brackets, each one is considered for matching in that position. Thus

     grep '[mh]an' intro.draft 
would match any words containing the sequence ``man'' or ``han'', but not ``ran'', ``can'' or `` and''.

You can use the hyphen within square brackets to denote an inclusive range. Thus

     grep '[a-c]r' intro.draft
would match strings containing ``ar'', ``br'' and ``cr''.

The character \ turns off the special nature of the character following it, provided that \ is not within square brackets. Inside square brackets, \ is an ordinary character. So

     grep 'manual\.' intro.draft
turns off the ``special'' nature of the period and matches ``manual.'', but not ``manual '', ``manuals'', or ``manually''.

The fgrep command does none of this expansion; however, you can search for more than one string of characters with a single fgrep command. To do this, place the possible strings, one per line, in a file. Then use the option -f file instead of the string option when you call fgrep, like this:

     fgrep -f list intro.draft 
If file ``list'' contains
     manual.
     manuals
     manually
fgrep would find all lines in intro.draft that contain ``manual.'' or ``manuals'' or ``manually''.

Here are some useful options to both grep and fgrep:

     -i    Ignores case, so that uppercase and lowercase characters
           match each other.
     -n    Displays the line number with each line containing a
           match.
     -l    Displays only the names of files that contain matching
           lines, but not the lines themselves.  This is useful if
           you are searching through a set of files to see which of
           them contain a particular pattern.
     -v    Displays lines that don't match a given pattern.

Comparing Contents of Files

The diff program is useful in determining how the contents of two files or directories differ. The simplest way to use it is
     diff oldfile newfile 
This produces, on standard output, a list of lines that must be changed (c), appended (a), or deleted (d) to make the first file match the second. Lines from the first file are prefixed by ``<'' and lines from the second are prefixed by ``>''.

If you only want to determine IF two files differ, and not HOW they differ, you can use cmp:

     cmp file1 file2 
The cmp program merely notifies you if the files don't match, stopping its comparison after it finds the first difference.

Transferring Files To and From Other Computer Systems

The FTP (File Transfer Protocol) program moves files from one computer to another. To use FTP, you call it, open a connection to the remote computer (remote host) and, within FTP, log in on that remote host. The remote host will be running its own version of FTP as well, with the local FTP (called the user interface) talking to the remote FTP (the server). Within this FTP environment, you can list the files in your remote directory, make local copies of remote files, make remote copies of local files, and delete remote files.
Note: It is generally not a good idea to transfer binary executable files from one computer architecture to another. Compiled programs will almost certainly not run on the receiving system without recompilation, so you should transfer source files of programs instead.
To illustrate a simple FTP session, the following example shows user joey logged in on a Hardlink UNIX system, using FTP to get file cprog.data from Hardlink and naming the local copy of it cdata.sun. What the user types is shown in boldface. Everything else is from the local FTP or, in the case of responses that begin with a number, remote Hardlink FTP.
  % ftp
  ftp> open ftp.hardlink.com                                       (1)
  Connected to bronco.hardlink.com.
  220 bronco FTP server (NcFTPd 1.9.6) ready..                    (2)
  Name (ftp.hardlink.com:joey): joey
  331 Password required for joey.
  Password: xxxxxx                                                  (3)
  230 User joey logged in.
  ftp> get cprog.data cdata.sun                                     (4)
  200 PORT command successful.
  150 Opening data connection for cprog.data (199.103.249.242,3686) (528 bytes).
  226 Transfer complete.
  local: cdata.sun remote: cprog.data
  544 bytes received in 0.025 seconds (21 Kbytes/s)
  ftp> quit
  221 Goodbye.
  1. In the open command, the user gave the full name of the Hardlink host, but in this instance just ``bronco'' would suffice.
  2. Notice that the response is from ``joey'', a specific member of the Hardlink cluster.
  3. The password is not actually displayed.
  4. The get command has the syntax
       get remotefile localfile
    
The remote file is assumed to be in your login directory on the remote machine. The local copy will be placed in your current working directory. However, you can give a pathname as part of either remotefile or localfile to transfer between different remote directories and local directories to which you have appropriate access.

Had user joey wanted to send, instead of get, a file, the command would be

     put localfile remotefile
Besides the man page, FTP has internal help that provides short explanations of all available commands. In response to the ftp> prompt, type
     help 
Two particularly useful commands are mget and mput, which enable you to specify wildcards to get or put multiple files. The FTP program prompts you for each matching file before it transfers it. For example, suppose you have five files that end in .txt on a remote machine, in a large directory, and you want to get only two of those five files. The files are:
     Document_list.txt
     file1.txt
     file2.txt
     myfile.txt
     notitle.txt
That part of your FTP session might look like this:
  ftp> mget *.txt
  mget Document_list.txt? n
  mget file1.txt? n
  mget file2.txt? y
  200 PORT command successful.
  150 Opening BINARY mode data connection for file2.txt (199.103.249.242,2784) (553 bytes).
  226 Transfer complete.
  553 bytes received in 0.09 seconds (0.011 Kbytes/s)
  mget myfile.txt? n
  mget notitle.txt? y
  200 PORT command successful.
  150 Opening BINARY mode data connection for notitle.txt (199.103.249.242,2786) (227 bytes).
  226 Transfer complete.
  227 bytes received in 0.065 seconds (0.015 Kbytes/s)
  ftp>

Reading USENET News

Several thousand electronic bulletin boards, called USENET newsgroups, contain discussions of topics ranging from specific sciences to computer languages to recreational pursuits. See
     http://www.hardlink.net/USENET/
for more information about USENET news.

A file in your login directory, named .newsrc, keeps track of all the newsgroup names and which messages in each group you have already seen. To read any of these newsgroups, use a news-reading program. One such program is called trn. The trn program examines your .newsrc file and displays unread articles in the same way that more displays a file--one screenful at a time. (Be sure your terminal type is defined correctly--see chapter 2.) When you first run trn, you will be ``subscribed to'' a newsgroup named news.announce.newusers, which contains useful information and guidelines for what is called ``netiquette''--common courtesy to use in posting messages.

The trn program has many more options than can be discussed here-- selecting articles by subject matter, subscribing and unsubscribing to newsgroups, saving articles, and searching for patterns within an article. You can also reply to the sender of an article or post a follow-up message. For a complete description, see the trn man page.

To post your own new message to a newsgroup, use the Pnews program. (Notice the capital ``P''.) It prompts you for necessary information.

Another popular news reader is called tin. It also uses the .newsrc file, but when you first use it, it shows you all available newsgroups--several thousand of them. You can go through the list, or search for keywords in newsgroup names, subscribing to the ones you want. For more information, see man tin.

Making a Record of Your Terminal Session

You can make a running log of your terminal session, recording everything you type and every response you receive. This can be especially useful in reporting some problem to the consultants, as you will have an exact record of your actions and their results. To make such a log, use the script command:
     script filename
Now everything you type and everything displayed on the screen will also be copied to the file filename. To stop the recording, use ^d or exit.

Disconnected or Runaway Jobs

If your terminal or workstation seems to lock up and you cannot log out, you might have a runaway or disconnected job. Follow these steps:
  1. Log in again on the same machine (if need be, from another terminal) and type the commands:
        tty
        ps -u username
    
    The first command identifies the terminal you are now logged into. The second shows you all processes you (username) own that are running on the system.

  2. Look for lines that say:
        csh
    
    in the COMMAND column. These are the top-level login shell processes for your jobs.

  3. If any of these lines where the terminal line shown in the TTY column is NOT your current terminal, look in the PID column for the process id number of that C shell. Send that C shell a hangup (HUP) signal:
        kill -HUP pid
    
    where pid is the process id number you just found.

  4. Give the ps -u username command again to make sure the other process is gone. If the unwanted process is still there, reissue the kill command like this:
        kill -9 pid
    
Here is a sample session, showing the prompts (a percent sign), commands, and responses:
     % tty                        # Determine current login terminal
     /dev/tty5c                   # Its TTY number is 5c
     % ps -u myname               # Look at all processes I am running
       PID TT STAT  TIME COMMAND
     27595 4a E     0:24 csh      # Shell running on another terminal
     28200 5c E     0:03 csh
     28275 5c R     0:01 ps xg
     % kill -HUP 27595            # Send that shell a hangup signal
     % ps -u myname               # Check to make sure it worked
       PID TT STAT  TIME COMMAND
     28200 5c IW    0:03 csh
     28350 5c R     0:01 ps xg

Go to next chapter

Go back to table of contents