Thursday, July 31, 2008

Unix and Vi

2.1.2 The shell

There are a number of di_erent \avors" of Unix available today. By di_erent

\avors" I mean di_erent command interpreters (called shells) which handle

your input in their own unique way. This manual covers the C shell only. Many

of the things found here will be identical with other shells, but don't count on

it. It is possible to determine which shell is in use by typing echo $SHELL. The

response for the C shell is /bin/csh which is what you should get. One other

popular shell is the Bourne shell which would respond with /bin/sh.

2.1.3 Command syntax

Unix commands begin with a command name, often followed by ags and arguments

some of which are optional. The generic syntax is:

command [flags] argument1 argument2 ...

Normally the ags are preceded by a hyphen to prevent them from being interpreted

as a _lename. For example, in the command line

ls -l avhrr

ls is the program called, -l is the ag, and avhrr is the argument. This command

tells the computer to list (in long format) the _le called avhrr or, if avhrr is a

directory, to list all the _les in the directory avhrr.

2.1.4 Correcting typos

There are three tools to destroy typos that occur on the command line. 5

<DEL> Erases the previous character.

Erases the previous word.

Erases the whole command line.

2.1.5 Controlling your terminal output

If output scrolls up on your terminal screen faster than you can read it, you can

suspend it by typing <ctrl -S>. To resume the display, type <ctrl -S>. Again

section 2.4.6 will discuss how to pass output through a paging program that will

automatically display only one screen at a time. While I'm at it, <ctrl -C> will

abort a process, and <ctrl -O> will discard the output until another <ctrl -O> is

entered. Be sure to note that although the output doesn't appear, the process

is still running. <ctrl -Z> suspends the current program. You can see its job

number by typing jobs. You can resume the suspended program by giving the

fg (foreground) command, or resume it in the background with bg.

5A couple notes on notation here. The delete key varies from keyboard to keyboard. Your

delete key may be labeled DEL, DELETE, BACKSPACE, RUB, or RUBOUT. Also, <ctrl-

W> means holding down the <ctrl> key and pressing W. Control <ctrl> character commands

are not case sensitive, i.e. <ctrl-w> is equivalent to <ctrl-W>.

2

2.2 Files and directories

When you start a Unix session on a computer, you are placed in a directory

that contains your _les. This directory is called your home directory. My home

directory is /home/cernan/taylor. You can create, copy, move, and remove _les

as well as create subdirectories from here (see section 2.4).

2.2.1 Pathnames

There are a number of methods for specifying which directory and _le you

are interested in. Pathnames (the directory speci_cation) can be relative or

absolute. Absolute pathnames begin with a slash, /, and start at the root

directory. Successive directories down the path are also separated by a slash.

In the previous paragraph I gave the absolute pathname of my home directory.

Each subdirectory is a branch in the directory tree.

A relative pathname begins with the directory you are in (commonly referred

to as working directory) and moves downward to a lower directory. Relative

pathnames begin with the name of the _rst directory below the working

directory. Each lower directory down the path should have a slash in front of

it. Assuming I was in the /home directory, cernan/taylor would be the relative

pathname to my home directory. A \." indicates the working directory, while

\.." indicates the directory one level up (known as the parent directory). If

I were in my home directory, the relative pathname for the /home directory

would be ../.. which says go to the \grandparent" directory two directories

higher than you are now.

2.2.2 Naming _les and directories

In general, _le and directory names should be composed only of letters of the

alphabet, digits, \." 's, and \ " 's. Be aware that _les that begin with a \." do

not appear in the directory list unless a special ag has been set when doing

the list command.

The period is often used to add a su_x on to a base _lename. For example,

the source code for C programs have a .c su_x added to them, e.g. prog.c. Separating

a _lename by a \." is particularly useful when using wildcard selections

(see section 2.7.1).

2.3 Online manual

All of the commands in Unix are described online in a collection of _les. They

are known as man pages because they were originally pages of the UNIX Pro-

grammer's Manual. There are eight sections of the man pages:

1. Commands

2. System calls

3

3. Library functions

4. Devices and device drivers

5. File formats

6. Games

7. Miscellaneous

8. System maintenance

If you know the name of a command, you can view its man pages by typing6

man [section] name

A program called apropos7 is available for those who don't know the name

of the command they want. The apropos program searches through the header

lines of the man pages for whatever keyword you supply and displays a list of

the man pages containing it. For example,

apropos copy

produces a list of all the man pages that contain copy in their header lines.

The list will contain commands followed by a number in parentheses, i.e. cp

(1). The number in parentheses is the section number. If the section number is

omitted when doing a man command, the man program searches through each

section until it _nds the named man page. This works _ne if the name is unique,

but a few names exist in more than one section. One example of this is intro.

There is an intro man page for each section. Typing man intro would get you

the intro man page for the _rst section, but the only way to get the intro man

page for section 5 is to type

man 5 intro

When the man pages are being displayed on your terminal, it pauses after

each screen full and displays a --More-- on the bottom line. This give you a

chance to read the information before you go on to the next screen full. Press

the space bar to scroll an entire screen forward.

2.4 Basic commands

The following few sections are devoted to many of the commands available in

Unix. The descriptions are by no means complete. The most useful commands

(at least to me) have descriptions that should su_ce for the average user. However,

less useful commands have rather terse summaries. If more information is

desired for any of the commands, check the man pages. See section 2.3 on how

to use the man pages.

6Portions of commands that are in square brackets, [ ], are optional.

7Typing apropos or man -k do the same thing.

4

2.4.1 Logging on (rlogin)

In order to use a computer operating under Unix you need to \log on". This

attempts to protect against unauthorized use of the computer equipment. It also

lets each user de_ne their own personalized working environment on the same

computer and even work on the same computer at the same time. The basic

Unix command for remotely logging onto a computer is rlogin (remote login).

To log onto a computer type rlogin computername. You are then asked to

enter your account name, password, and then your charge code. Workstation

consoles, as well as x-terminals, are ready for your account name, password, and

charge code. The rlogin command is not needed. The characters do not appear

when you type your password to promote con_dentiality.

2.4.2 Changing your password (passwd)

Passwords are an important security measure. Don't neglect creating a \good"

password. A good password should be easy to remember for you but hard for

others to guess. Words in the dictionary, nicknames, and common chemical

compound names are poor choices for a password. One way of generating a

password is to use the _rst letter of each word in a strange yet memorable

sentence. For example, fatIwrnf could be my password based on the sentence:

For a time I would recommend no forgery.8

When you _rst receive your account you will probably be given a temporary

password. You should change your password to something else. This is done

with the passwd command. After typing passwd, you will be prompted _rst

for your current password and then twice for a new password. Please note that

this only e_ects the computer you are logged onto. You will need to repeat this

ritual on every computer you have an account on.

2.4.3 Getting out (exit)

The command for ending a Unix session is exit. Another way to log out is to type

<ctrl-D>. To avoid accidentally ending your Unix session with an inadvertent

<ctrl-D>, type the command set ignoreeof at the beginning of each Unix

session. Most lazy, or should I say e_cient, people don't like doing this every

time they log in. In section 2.6 we will discuss how to get around this threat to

our slothfulness.

2.4.4 Listing _les (ls)

The names of _les and subdirectories can be displayed with the ls (list) command.

Typing ls lists the _les and subdirectories located in the working direc-

8This sentence is especially interesting because the number of letters in each word make

up the constant pi to eight signi_cant digits.

5

tory that don't begin with a \.". To see all the names, use the all ag, i.e. ls

-a.

Other interesting ags for the ls command are:

-F Marks directories with a trailing slash and executable files with

a trailing asterisk.

-l Lists in long format. Gives all sorts of information.

-R Recursively lists subdirectories encountered.

-s Gives the size of each file.

-t Sorts by time modified instead of by name.

It is possible to limit the scope of the _les and subdirectories by using the

wildcard characters discussed in section 2.7.1. For example, I would type

ls q*

if I wanted to list all the _les and subdirectories that began with a q.

Note that the ls command lists _les in the working directory only, unless you

include the pathname to another directory whose _lenames you want to list.

2.4.5 Catenate (cat)

Catenate means \to connect in a series." The cat command displays the contents

of a _le. If more than one _le is placed in the command line, i.e. cat yellow

blue, the _les are displayed in succession. It is here that cat derives its name.

With the use of the redirection operator (see section 2.7.2) two _les can be

placed into a single _le. Typing

cat yellow blue > green

will cause green to contain the contents of yellow followed by the contents of

blue.

2.4.6 Paging display system (more)

The more command provides a convenient alternative to displaying text on

your terminal. The more program takes the input text and displays one screen

full worth. The last line of the screen contains --More--. To scroll an entire

screen forward, press the space bar. To scroll forward one line at a time, press

<return>. To enter the vi text editor (see section 3), type v. To quit reading,

type q.

More can be used on a text _le by typing more filename or can be used to

display the output from another program with the use of the pipe symbol (see

section 2.7.3) by typing command | more.

6

2.4.7 Copying _les (cp)

The cp (copy) command lets you duplicate a _le of choice. Here is an explanation

by examples:

cp cocoon butterfly

makes a duplicate of the _le cocoon and gives it the name buttery. Note that

the _lenames can include pathnames as well.

cp /home/cernan/taylor/tex/contract ../contract.bak

makes a copy of the _le contract found in the /home/cernan/taylor/tex directory

and places it one directory level above the working directory in a _le called

contract.bak.

If /home/cernan/taylor/tex is a directory, then

cp report /home/cernan/taylor/tex

will place a copy of report in the /home/cernan/taylor/tex directory with the

name report.

cp /home/cernan/taylor/tex/headlines .

will copy the _le headlines in the /home/cernan/taylor/tex directory into the

working directory. The name will remain unchanged.

cp /home/cernan/taylor/tex/* .

will copy all the _les (but not the subdirectories) in /home/cernan/taylor/tex

into the working directory. You can copy all the subdirectories in the directory

and _les contained in them by using the -r (recursive) ag as follows:

cp -r /home/cernan/taylor/tex/* .

Another useful ag is the -i (interactive) ag which prompts you if you are

about to overwrite an existing _le.

2.4.8 Removing _les (rm)

The rm (remove) command deletes _les that you no longer want. Just type

rm filename to remove the _le _lename. If more than one _lename is on the

command line, i.e. rm archaeologists date anything, then the _les archae-

ologists, date, and anything are removed.

7

2.4.9 Renaming and moving _les and directories (mv)

The mv (move) command moves a _le from the _rst argument to the second

argument, e.g.

mv neatguy tidyguy

moves the contents of neatguy to the contents of tidyguy. This commandreminds

me of the time Chicago Bulls forward Stacey King said:

I'll always remember this as the night that Michael Jordan and I combined

to score 70 points.

after scoring one point in a game in which Jordan scored sixty-nine. All that

really happened was that the _le's name was changed. The reason it is called

the move instead of rename or something like that is that you can include

pathnames (just like you have done before in cp and rm). Including pathnames

allows you to move a _le into a di_erent directory, hence the name. The mv

command works on both _lenames and directory names exactly the same way.

The -i ag works here just like it worked with cp. Setting this ag will

prompt you before it moves a _le on top of one that already exists.

WARNING: for (i = 0 ; i <= 50 ; i++) printf(\Don't "); don't use wildcards

(see section 2.7.1) with the mv command unless the destination is a directory.

The mv command doesn't know what to do if you tell it to move a bunch of

_les into a single _lename and so it will move all the _les you selected on top of

each other.

2.4.10 Navigating the directory tree (cd)

The cd (change directory) command does just what it says. It changes your

working directory. The command syntax is

cd pathname

where the pathname can be either relative or absolute.9

2.4.11 Directory creation and destruction (mkdir and rmdir)

New directories are created with the mkdir (make directory) command and

removed with the rmdir (remove directory) command. The syntax is

mkdir directory

and

9If these words don't make sense to you, you are either not paying attention or aren't

reading this in order. I don't have a problem if you aren't reading this sequentially if you are

willing to deal with some of this terminology ambiguity, but if you are struggling with the

_rst problem, go back and reread section 1.2.

8

rmdir directory

The pwd (print working directory) command displays the absolute pathname

of your working directory.

2.5 Additional commands

A number of additional commands are listed in this section. If you have forgotten

what is in section 2.3, see section 1.2 and then section 2.3 for advice on how

to get more information about the commands in this section.

2.5.1 alias

The alias command allows you to de_ne shortcuts to save yourself time. In a

sense, alias creates a link between a requested set of keystrokes and another

set of keystrokes. For example, to use the rm command in interactive mode I

would type

rm -i

By typing

alias rm 'rm -i'

the alias command would allow me to avoid typing the interactive ag, -i ,

every time a called the rm command.

The alias command de_nes a link between the _rst and the second arguments

following the command. Whenever the _rst argument is entered at the command

prompt, the Unix shell substitutes it with the second argument. An alias link

stays in e_ect until the Unix session is ended or the link is \unaliased". To

destroy the link in my previous example I would type unalias x. The power of

this command is more easily realized when used in a login _le (see section 2.6).

2.5.2 ap

The ap (auto pilot) command has a deceptive name. It doesn't actually place

the computer on auto pilot. The ap command reads your mind and attempts

to perform the commands you want done. For example, thinking \I really wish

I had a backup copy of the tanana image." will cause ap to input

cp tanana.* tanana_bak.*

to the Unix shell. Preceding a thought with \ignore" will cause ap to ignore

your next thought. Although, with enough practice, the ap command can be a

signi_cant time saver, there are a few unresolved problems with this command.

9

1. I often change my mind while in the thinking process. In the previous

example I may have decided later that I wanted to call the backup copy

something else. No big deal here, ap just changes the _lename but it isn't

the most e_cient use of computer resources.

2. All of the commands are echoed to the screen so that you know exactly

what is going on. This is great as long as you remember to think \ignore"

before you read each command. If you forget, the command will

be executed again. This will continue until you remember to include the

\ignore" ag or you think, \What is going on here?" which will cause the

man pages for the particular command you are repeating to be displayed.

3. The ap command reads the strongest mind waves (known as grey waves)

that it _nds. If you have weak grey waves or your monitor is closer to

someone else in your o_ce, ap may listen to someone else's mind instead

of yours. Also, walls do not provide insulation from grey waves, so if your

monitor is near a wall, be prepared for some grey waves from minds on

the other side of the wall to occasionally sneak in.

4. As you probably know, humans (you included) don't use their brains to

their highest potential. In fact, many believe that we use as little as 5%

of our brain's capacity. The problem here is that ap is only able to read

around 80% of your mind. Unfortunately, many people use the 5% of their

mind that ap can't read. When ap is called it scans your mind for activity,

if none is found it prints the following cryptic error message:

ap: Command not found.

This indicates that it couldn't _nd a command in your head. Don't worry,

this doesn't mean that you aren't thinking, it just means that you use the

part of your brain that ap can't access.

2.5.3 bi_

The bi_ command runs in the background and lets you know when electronic

mail arrives. It was named after a dog at Berkeley that was known for barking

at the mailman. To tell bi_ to bark at the mailman, type biff y. To tell bi_

not to bark at the mailman, type biff n.

2.5.4 chmod

Your _les and directories have a number of attributes that are set when they

are created. Listing the _les with the -l ag, i.e. ls -l, displays the attributes

of each _le and directory in the working directory. Here is an example listing:

10

total 3

drwxr-xr-x 2 taylor 512 Aug 2 08:41 .

drwxrwxr-x 12 taylor 1024 Aug 2 08:41 ..

-rw-r--r-- 1 taylor 5 Aug 2 08:41 blue

-rw-r--r-- 1 taylor 12 Aug 2 08:41 green

-rw-r--r-- 1 taylor 7 Aug 2 08:41 yellow

To the far left of each _le or directory name are ten characters which show

the attributes. The _rst column indicates whether the entry is a directory (d)

or not (-). The other nine characters are organized into three groups of the

three. The _rst group pertains to the owner (that would be you for your _les).

The second group pertains to people in your group, if you are in a group. The

third group pertains to everyone else. Within each group of three are three

characters. The _rst indicates read (r) permission. The second indicates write

(w) permission. The third indicates execute (x) permission. If the permission is

not present, a \-" will replace the r, w, or x.

The chmod (change mode) command lets you change the attributes on a

_le or directory. There are a number of forms, but I have chosen to cover the

following syntax because of its similarity with umask.

chmod mode filename

where mode is a three digit octal number. The _rst digit pertains to the owners

privileges. The second pertains to the groups privileges, and the third pertains

to everyone elses privileges. Each octal digit is composed of the addition of

three components. The read component is worth 4, the write component worth

2, and the execute component worth 1. Suppose I wanted the owner to have

read, write, and execute privileges, the group to have read and write privileges,

and everyone else to have read privileges only. The octal number I would use

with chmod would be 764.

2.5.5 compress/uncompress

The compress and uncompress commands compress a selected _le using adaptive

Lempel-Ziv coding to help conserve disk space. This technique almost always

does a better job than the Hu_man coding technique used by the pack/unpack

commands. Typing compress edison would create a compressed _le called

edison.Z which could be resorted to its original condition by typing uncompress

edison or uncompress edison.Z.

2.5.6 df

The df (disk free) command displays the amount of free disk space. This is

often quite handy when determining if there is enough space to store an image

on a particular hard drive. A quick glance at the man pages should indicate

what ags should be set for the computer you are on.

11

2.5.7 di_

This program is useful in determining di_erences between two _les or directories.

It produces a list of lines that must be changed (c), appended (a), or deleted

(d) to make the _rst _le match the second. Lines from the _rst _le are pre_xed

by \<" and lines from the second are pre_xed by \>".

The -b option ignores trailing blanks and treats all other strings of blanks

as equivalent. The -i option removes case sensitivity so that uppercase and

lowercase letters match.

2.5.8 du

The du (disk usage) command displays the number of kilobytes consumed by

each _le and recursively provides results on directories. This can be useful for

determining who the big disk space hogs are when you need more room. Typing

du -s *

from the parent directory of your home directory, e.g. /home/cernan, gives a

grand total of the kilobytes consumed by each user.

2.5.9 echo

The echo command echos a string to the terminal. One use for this command

is in determining the contents of environment variables. Environment variables

are variables that Unix keeps track of at the shell level. Two common examples

are TERM and PATH. The TERM variable identi_es what kind of terminal you

are using. The PATH variable contains a list of pathnames to search through

when looking for commands. More information on environment variables can

be found in section 2.6.

To see the contents of the TERM variable type echo $TERM.

2.5.10 _nd

The _nd command recursively descends through the directory tree looking for

_les that match a logical expression. The _nd command has many options and

is very powerful. Rather than go into detail here, I encourage you to take a

look at the man pages for _nd. The _nd command does have a rather contorted

syntax which is not easily mastered, and if truth be written, that's why I'm not

spending more paper on it here.

2.5.11 _nger

The _nger command displays information about users. It can be used both

locally and across the internet. For example,

finger taylor@en.ecn.purdue.edu

12

will display information about me from my computer account at Purdue University.

2.5.12 ftp

The easiest way to copy _les from one disk to another is to use the cp command.

However, often I am interested in copying _les from one computer to another.

The ftp command uses the File Transfer Protocol (ftp) to transfer data over a

network connection.

To use ftp you open a connection to a remote computer and log onto that

computer that can't access each others hard drives. The remote computer runs

its own version of ftp, but you are in control of it. Within the ftp program

you can list the _les in your remote computer's directory, get copies of _les on

the remote computer, put copies of _les from your computer onto the remote

computer, and even delete _les on the remote computer.

Here is an example of a ftp session:

ftp baboon (1)

Connected to baboon.ecn.purdue.edu.

220 baboon.ecn.purdue.edu FTP server (Version 4.178 Tue Jun 18 13:30:39

EST 1991) ready.

Name (baboon:taylor): taylor (2)

331 Password required for taylor.

Password: xxxxx

230 User taylor logged in.

ftp> cd tex/manual (3)

250 CWD command successful.

ftp> get chap1.tex chap1.tex.bak (4)

200 PORT command successful.

150 ASCII data connection for chap1.tex (8612 bytes).

226 ASCII Transfer complete.

local: chap1.tex.bak remote: chap1.tex

8848 bytes received in 0.45 seconds (19 Kbytes/s)

ftp> quit (5)

221 Goodbye.

1. This starts the ftp program and tells it to open a connection with the

computer called baboon.10

2. Here you need to type in the name of your account on the remote computer.

If the name of your account on the remote computer is the same as

the account on your local computer, you don't need to type in the account

name but can just hit <enter>.

3. The cd command works like it does in Unix with one exception that we

won't go into here.

10If you are attempting to open a connection with a computer outside of the Engineering

Computer Network (ecn), you will need to include the entire internet address. In this case it

would be baboon.ecn.purdue.edu.

13

4. This copies the _le chap1.tex from the remote computer to chap1.bak

in your local working directory. If no destination _le is given the get

command gives the _le the same name on the local computer. The put

command will send a _le from the local computer to the remote computer.

The get and put commands don't like wildcards. (See section 2.7.1 for

a discussion of wildcards.) If you want to copy a number of _les that

have similar names but don't have the energy to type in all the names

individually the suggestion of mget and mput may make you very happy

that you read this manual.

5. Typing quit gets you out of the ftp program.

A short explanation of the available commands can be coaxed onto your

screen by typing help at the ftp> prompt.

2.5.13 grep

The grep (get regular expression) program searches for an expression in a _le

or group of _les. There are three versions: grep, egrep (extended grep), and

fgrep (_xed-string grep). The grep program expands wildcard characters in

the given expression. The egrep program searches for the expression including

alternations. The fgrep program searches for _xed-strings only and does not

expand wildcard characters. The egrep program has more sophisticated internal

algorithms, and is usually faster than grep or fgrep. The syntax for all three

versions is:

command [options] expression [file] ...

I have found these Unix commands to be very useful when programming.

Suppose I had a C program with a number of subroutines and a global variable

labeled chuck wivell. Suppose further that Chuck found out about this and

didn't like it. Of course I would change it immediately.

egrep chuck_wivell *.c

would give me a list of all _les where the o_ensive variable manifested itself. By

placing a -n option in the command line I could also obtain the line numbers

of the o_enses.

The wildcard characters that grep handles are

\ [ ] . ^ * $

and a delimiter used to mark the beginning and end of an expression. Delimiters

are necessary only if the expression contains blanks or wildcard characters. Here

are a few examples to help solidify this potential mumbo-jumbo:

grep 'Nostalgia is not what it used to be' fft.c

14

searches through the _le _t.c for the expression Nostalgia is not what it used to

be.

The wildcard character \." matches any character. Therefore,

grep 'eur.' fft.c

would _nd eureka, amateur, chau_eur, etc: : : in the _le _t.c.

Characters placed inside square brackets are each compared when searching.

grep '[cm]an' fft.c

would _nd any words with the sequence can or man, but would not locate

sequences like ran or and. More can be found on the wildcard characters in

section 2.7.1.

Preceding a wildcard character by a \n" turns o_ the wildcard character

feature and the character is treated normally, i.e. the expression eddien. would

yield all the eddie.'s but not eddies or eddieboy.

Here are some useful options for all three of the greps:

-f Matches all the expressions in a given file as opposed to

the one typed in the command line.

-i Removes case sensitivity so that uppercase and lowercase

letters match.

-n Displays the line numbers containing a match.

-l Displays the names of the files that contain a match but

not the lines that contained a match.

-v Displays the lines that don't match as opposed to those that do.

2.5.14 history

The history command displays a list of commands you have previously typed.

For this command to work correctly you must _rst type set history=n where

n is the letter before o and the number of commands that should be saved. A

peek at section 2.7.5 may help explain this.

2.5.15 kill

At times you may _nd that you have a job running that you don't want to

continue. It is at this point that your thoughts may turn to murder. Kill is the

hitman of choice for Unix users. Kill is quick and cheap (roughly 13 keystrokes).

To put kill to work just type

kill -9 processid

where the processid can be found with the ps command.

If the process was created by the current interactive shell, you can type

kill -9 %n

where n is the process index indicated by the jobs command.

15

2.5.16 look

The look command searches through the system dictionary or lines in some other

sorted list for a word. I often use look to check my spelling of a word. Suppose

I want to know if inoculate is spelled correctly. I would type look inoculate.

If inoculate is in the system dictionary (which it is) it is echoed to the terminal,

and I know that the spelling is correct. If it is not in the system dictionary, it

is not echoed to the terminal.

2.5.17 mail

Most users with access to computer accounts in a higher education setting and

many in a corporate environment have access to internet. Your email address is

your account name@hostname.domain where hostname is the name of the local

computer and domain is the name of the \system" you are on. For example,

taylor@sunp.cr.usgs.gov was my email address this past summer. In this case,

taylor was my account name, sunp was the local computer name, and cr.usgs.gov

was the name of the \system" I was on. I can read mail sent to me by logging

on to the sunp computer and typing mail. Mail is sent to others by typing:

mail internet_address

where internet address is the address of the person you wish to send a message

to. You are then thrown into a very crude line editor that lets you type your

message. Remember to hit <return> at the end of each line because it can't

handle word wrapping. Typing a \." or a <ctrl -D> on a line all by itself will

signal the computer that you are _nished with the message. The computer will

then send the message you just wrote. If you wish to send a _le rather than

typing the message, use the following command:

mail internet_address <>

where _lename is the name of the _le containing the message you wish to send.

Section 2.7.2 covers the redirection (<) operator in more detail.

2.5.18 ps

The ps (process status) command displays the status of current processes. If

no ags are set, the command displays only your processes. Take a look a the

man pages to see what ags might be of interest to you. I usually use -aux.

2.5.19 script

The script command records, in a speci_ed _le, everything you type and every

response you receive during your terminal session. To save the contents of your

session in a _le called logsession, type

script logsession

16

2.5.20 setenv

The setenv (set environment variable) command assigns values to environment

variables. Many environment variables are used by di_erent Unix programs.

We will see some of these in section 2.6. It is also possible to de_ne your own

variables. To either de_ne a new environment variable or change the value of

an existing variable type

setenv variablename newvalue

For example, setenv TERM vt100 assigns vt100 to the variable TERM.

2.5.21 source

The source command sends the contents of a text _le to the Unix shell. Suppose

I have (and I do) a number of alias commands that I want typed in. Rather

than typing them all in, I keep them stored in a _le called (oddly enough) .alias.

All I need to do is type

source .alias

and I have all my alias commands executed as if I had typed each one in

separately.

2.5.22 spell

The spell command checks the spelling of all the words in a desired _le against

those in the system dictionary or some other _le and outputs all the words

that it couldn't _nd in the system dictionary. To check the spelling of the _le

holy cow type

spell holy_cow

2.5.23 tar

The tar (tape archiver) program is useful for storing a bunch of _les in one _le

(usually on a magnetic tape, but it doesn't have to be). The syntax for this

command is

tar [key] [name ...]

where key is speci_ed by a plethora of options (see abridged list below and

unabridged list in the man pages) and name is either the _le name or device

name.

Here are some of the more commonly used keys:

17

c Creates a new tape.

f Used for taring to a tape.

t Lists the contents of a tar file.

v Turns verification on.

x Extracts selected files. If no file argument is given,

the entire contents of the tar file is extracted.

Here is the syntax I use to create and read tar _les:

tar cvf /dev/drivename directoryname <-- creates

tar xvf /dev/drivename directoryname <-- reads

2.5.24 telnet

The telnet command opens a connection to another computer via the internet

network. This command allows you to log onto machines around the world that

you have accounts on or that allow public access. For example, the University

of Michigan o_ers public telnet access to weather information. To access this

information type

telnet madlab.sprl.umich.edu 3000

2.5.25 umask

The umask command displays or sets the creation mask setting. The creation

mask setting de_nes the default attributes for new _les (see section 2.5.4). If

no argument is included, umask displays the current setting. To change the

creation mask setting type

umask value

where value is a three digit octal number similar to the one de_ned in section

2.5.4. It is important to note that this is a mask. This means that a umask

setting of 022 would give the owner full privileges while the group and all others

would not have write privileges. This is exactly opposite of what we saw in

section 2.5.4 on chmod.

2.5.26 who

The who command simply tells you who is on the computer. Just type who.

2.5.27 A bunch more commands

The rest of the this subsection is a terse description of a few more Unix commands

that you may _nd occasion to use.

awk | A pattern scanning and processing language.

bar | Creates a tape archive. (Similar to tar)

18

bg | Moves a job into the background.

cal | Displays a calendar.

cc | Compiles C code.

chfn | Changes _nger information.

clear | Clears your terminal's screen.

cmp | Preforms a byte-by-byte comparison of two _les.

cut | Removes selected _elds from each line of a _le.

date | Displays or sets the date.

ed | The most basic line editor.

ex | A simple line editor. Also know as e or edit.

fg |Moves a job into the foreground.

_le | Determines the type of a _le by examining its contents.

fmt | Formats text.

hostname | Sets or prints the name of the current host computer.

jobs | Lists the current jobs in the shell.

make | Maintains, updates, and regenerates related programs and _les.

mesg | Permits or denies messages on your terminal.

mt | Provides magnetic tape control.

od | Dumps octal, decimal, hexadecimal, or ascii representations of _les.

pack/unpack | Similar to compress/uncompress, but uses Hu_man coding.

paste | Joins corresponding lines of several _les.

rev | Reverses the order of characters in each line.

rcp | Copies a _le from a remote computer to the local computer.

rsh | Execute a remote shell command.

sed | A stream editor{quite powerful.

sort | Sorts and collates lines.

split | Splits a _le into pieces.

19

stty | Sets or alters the options for a terminal.

tr | Translates characters.

uname | Displays the name of the current system.

units | Converts a number into di_erent units.

uuencode/uudecode | Encodes/decodes a binary _le into strictly ascii characters.

(Useful for transmission via electronic mail)

write |Write a message to another user.

xget/xsend | Commands for sending/receiving secret mail.

2.6 Login _les

Every time you log in, the Unix shell searches your home directory for certain

_les and executes the commands in them. This allows you to customize your

Unix session. There are two initialization _les that I will discuss here. The .cshrc

(pronounced 'dot-see-shirk') _le and the .login (pronounced 'dot-login') _le. The

.cshrc _le is executed every time a new C shell is started. The .login is executed

after the .cshrc _le only when you initially log in. Generally, environment

variables should be set in the .login _le, and alias and set commands should be

in the .cshrc _le so that every new copy of the C shell will be able to use them.

2.6.1 The .cshrc _le

The following is an example .cshrc _le. The \#" character at the beginning

of a line tells the C shell to ignore the rest of the line. I don't expect you to

understand every command in this _le or in the example .login _le found in

the next section, but I don't care to explain them all either. This document is

getting too long as it is.

###############################################################################

# #

# Example .cshrc file #

# #

# by Chris Taylor #

# #

###############################################################################

# Set path shell variable

# (See description of path in the paragraph followin this example .cshrc)

set path = ( /usr/bin /usr/local /usr/local/bin /usr/bin/X11 \

/usr/ucb /usr/opt/bin ~ )

# Don't overwrite existing files with the redirection character ">"

set noclobber

20

# Don't create core dump files when a program blows up.

limit coredumpsize 0

# Check to see if this is an interactive shell.

# If not, skip the rest of this file.

if ($?USER == 0 || $?prompt == 0) exit

## Set C shell variables

# Remember my 40 most recent events

set history=40

# Save the most recent 40 events when I log out

set savehist=40

# Substitute the filename to be completed when I type an at

# the command line

set filec

# Tells the shell to ignore .o files when trying to complete filenames

# when filec is set. (This doesn't hold if the .o file is the only

# on that could be completed.

set fignore=.o

# Tells "filec" not to cry if it can't complete a file.

set nobeep

# Notify me when the status of background jobs change

set notify

# Don't let me log out by pressing

set ignoreeof

# Set TTY shell variable equal to the current terminal name

set TTY=`who am i | awk '{print $2}'`

# Allow others to send messages directly to my terminal

mesg y

# Set prompt to have the following form: [cmd#]cpu[directory]:

set cpu=`hostname | awk '{FS = "."; print $1}'` # set cpu = computer name

alias sp set prompt='\[!\]$cpu\[$cwd\]:\ ' # set sp to set the prompt

alias cd 'chdir \!* ; sp' # redefine cd command

alias pd 'pushd \!* ; sp' # redefine pd command

alias pp 'popd \!* ; sp' # redefine pp command

sp # set the prompt

# Shortcut aliases

alias c 'clear'

alias dict 'vi /usr/dict/words'

alias gv 'ghostview'

alias h 'history !* | head -39 | more'

alias laser 'lpr -Pmsa13 -h'

alias line 'lpr -Ped3'

alias ll 'ls -la'

21

alias ls 'ls -x'

alias mine 'chmod og-rwx'

alias pwd 'echo $cwd' # This is faster than executing the pwd command

alias safe 'chmod a-w'

alias tmp 'cd /tmp/taylor'

# end of .cshrc file

A number of commands, i.e. history, set, etc: : : are built in commands. The

rest of the commands must call an external program to execute it. Not all of

these other commands are stored in the same directory. They are spread into

a bunch of di_erent directories. The path variable is a shell variable that tells

the shell where to look for these commands. In the example .cshrc _le, the path

variable is set to

( . /usr/bin /usr/local /usr/local/bin /usr/bin/X11 /usr/ucb /usr/opt/bin ~ )

This tells the shell to look _rst in the working directory, then in the /usr/bin

directory, next in the /usr/local directory, and so on until the _le has been found

or all directories have been looked at.

2.6.2 The .login _le

The following is an example .login _le. The same rules apply here as did with

the .cshrc _le.

###############################################################################

# #

# Example .login file #

# #

# by Chris Taylor #

# #

###############################################################################

# Set erase, kill, and interrupt keys

stty crt erase '^H' kill '^U' intr '^C'

# Set the creation mask setting so that everyone can read my files

# but can't write to them

umask 022

## Set environment variables

# Set my terminal type to xterm

setenv TERM xterm

# Select vi as my editor of choice

setenv EDITOR /usr/ucb/vi

# Show the path to my mailbox

setenv MAIL /usr/spool/mail/$USER

# Set mail program

22

setenv MAILER /usr/ucb/mail

# Set paging program

setenv PAGER more

# Set default printer

setenv PRINTER hp

if (-f /bin/sun != 0) then

# Using a Sun

if ("`tty`" == "/dev/console") then

# Using console

setenv DISPLAY $cpu":0.0"

# Ask if I want to start X11

echo ""; echo -n "Start up X11? "

set ans = $<

if ("$ans" != "n" && "$ans" != "N") then

# Start X11

setenv DISPLAY $HOST\:0

stty -tostop

exec xinit .xstartup ; kbd_mode -a

clear

endif

unset ans

else

setenv DISPLAY `last | grep $USER | head -1 | \

awk '{print $3}' | awk '{FS=".";print $1 "." $2 ":0" }'`

endif

endif

# end of .login file

2.7 Special characters

2.7.1 Wildcards

A number of characters are interpreted by the Unix shell before any other action

takes place. These characters are known as wildcard characters. Usually these

characters are used in place of _lenames or directory names.

* An asterisk matches any number of characters in a filename,

including none.

? The question mark matches any single character.

[ ] Brackets enclose a set of characters, any one of

which may match a single character at that position.

- A hyphen used within [ ] denotes a range of characters.

~ A tilde at the beginning of a word expands to the name

of your home directory. If you append another user's login

name to the character, it refers to that user's home directory.

Here are some examples:

1. cat c* displays any _le whose name begins with c including the _le c, if

it exists.

23

2. ls *.c lists all _les that have a .c extension.

3. cp ../rmt?. copies every _le in the parent directory that is four characters

long and begins with rmt to the working directory. (The names will

remain the same.)

4. ls rmt[34567] lists every _le that begins with rmt and has a 3, 4, 5, 6,

or 7 at the end.

5. ls rmt[3-7] does exactly the same thing as the previous example.

6. ls ~lists your home directory.

7. ls ~hessen lists the home directory of the guy11 with the user id hessen.

2.7.2 Redirecting output

A program that normally reads its input from the terminal (standard input)

or normally writes its output to the terminal (standard output) may become

annoying if you would rather send the input from a _le instead of the keyboard

or send the output to a _le instead of the terminal. This annoyance can be

avoided if you happen to be swift with the redirection operators. The redirection

operators are \<", \>", and \>>". The _rst is used to send input to a

command. The second is used to create a _le and send the output to it. The

third is used to append the output to an existing _le.

An example of the _rst redirection operator was already given in section 2.5.17

on electronic mail. Suppose you wanted to put a list of all the people logged on

into a _le called neatguys with the current time listed at the top of the _le.

date > neatguys

would create a _le with the date and time in it, and

who >> neatguys

would append the list of users logged on.

2.7.3 Pipes

A pipeline is a convenient way to channel the output of one command into the

input of another without creating an intermediate _le. Let's say we wanted to

get an alphabetical listing of the current processes. From a thorough study of

the previous section and the man pages for ps and sort, we already know how

to do this:

ps -aux > processes

sort processes

11Throughout this paper guy is assumed to be gender neutral unless otherwise stated.

24

This works, but it gives us a _le (namely processes) which we don't want. The

pipe symbol, \j" lets us bypass this intermediate _le. The above two commands

can be replace with the following:

ps -aux | sort

It is possible to connect a series of commands by additional pipe symbols.

We could pass our previous output through the more paging program to obtain

a more pleasing display of the results. This is a accomplish by typing

ps -aux | sort | more

One important point to recognize is that if a command isn't capable of

reading from standard input, it cannot be placed to the right of a pipe symbol.

2.7.4 Quote characters

Sometimes it is necessary to place wildcards in the command line without having

the shell treat them as special characters. This can be done by either preceding a

single wildcard character with a backslash, n, or enclosing a sequence of wildcard

characters in apostrophes, ' '.

For example, if you wanted to set your C shell prompt to a question mark

and typed

set prompt=?

the question mark would be expanded to be the _rst single-character _lename in

the working directory. If one exists it will be your prompt. If no single-character

_lenames exist, you will get a \set: No match" error. You should have typed

set prompt=\?

2.7.5 Other special characters

If you have set the history option (see section 2.6), you can use special characters

to repeat those commands without retyping them. Here are some of them:12

!! On a line by itself will repeat the most recent event.

!com Will repeat the most recent event that begins with "com".

!?string Will repeat the most recent event that contained "string".

!-n Will repeat the nth previous event.

!n Will repeat the nth event. Type "history" to see the events

numbered.

^old^new^ Will substitute "new" for the first occurrence of "old" in the

most recent event, and repeats that event.

: Will select specific words from an event line so you

can repeat parts of an event, e.g.

12By event I mean one command line. This may be a single command, or it may include a

number of commands in a pipeline, or whatever.

25

!?adam:s/adam/eve/

will substitute "eve" for "adam" and repeat the last event

with "adam" in it.

The semicolon, \;", separates commands. Typing

clear ; ls

is equivalent to typing each command on a separate command line.

The \&" symbol tells the shell to execute the command in the background.

For example, typing xid & would execute xid in the background and give my

Unix commandline back so I could continue to use it even while xid was running.

The C shell also _nds special meaning in the following:

" ` { } #

Rather than explain the uses of these special characters, I caution you to

avoid using them in _lenames.

2.8 Miscellaneous tips

2.8.1 Removing _les with strange names

There may come a time that you will discover that you have somehow created a

_le with a strange name that cannot be removed through conventional means.

This section contains some unconventional approaches that may aid in removing

such _les.

Files that begin with a dash can be removed by typing

rm ./-filename

A couple other ways that may work are

rm -- -filename

and

rm - -filename

Now let's suppose that we an even nastier _lename. One that I ran across

this summer was a _le with no _lename. The solution I used to remove it was

to type

rm -i *

26

This executes the rm command in interactive mode. I then answered \yes" to

the query to remove the nameless _le and \no" to all the other queries about

the rest of the _les.

Another method I could have used would be to obtain the inode number of

the nameless _le with

ls -i

and then type

find . -inum number -ok rm '{}' \;

where number is the inode number.

The -ok ag causes a con_rmation prompt to be displayed. If you would

rather live on the edge and not be bothered with the prompting, you can use

-exec in place of -ok.

Suppose you didn't want to remove the _le with the funny name, but wanted

to rename it so that you could access it more readily. This can be accomplished

by following the previous procedure with the following modi_cation to the _nd

command:

find . -inum number -ok mv '{}' new_filename \;

2.8.2 Wildcards beyond the working directory

Let's say we want to perform some command on a set of _les in the working

directory and all the directories below it. What if there was a Hewlett-Packard

advertisement that asked, \What if I had a slew of subdirectories containing

mounds of C source code, and I wanted to copy all of the library _les (_les

with a .h extension) into a separate directory called library. How could I do

it?" If you had read the next line, you would respond immediately with the

following:13

cp `find . -name '*.h' -print` library

2.8.3 Terminal input in a shell script

To input text from your terminal into a C shell script use the following syntax:

while ( 1 )

set line = "$<"

if ( "$line" == "" ) break

...

end

Also, be advised that the C shell has no way of distinguishing between a

blank line and an end-of-_le.

13That is, if you talk to your television.

27

2.8.4 Remote shell trick

Here is the proper syntax to use the rsh (remote shell) command without having

the remote shell remain active until the remote command is completed.

rsh machine -n 'command >&/dev/null

where machine is the name of the remote computer and command is the remote

command to be performed.

This works because the -n ag attaches the rsh's standard input to /dev/null

so you can execute the complete rsh command in the background of the local

computer. Also, the input/output redirections on the remote computer (the

stu_ inside the single quotes) makes rsh think the session can be terminated

since there is no data ow. In all truth, you don't have to use /dev/null. Any

_lename will work.

2.8.5 Loops in scripts

Here is an example of a simple loop in a script. I use it to send out my biweekly

junkmail messages.14

#!/bin/sh

for i in `cat $HOME/jm/list`

do

mail -s 'Junkmail message number '$1 $i <>

done

The script takes one line at a time from the _le $HOME/jm/list and executes

the command

mail -s 'Junkmail message number '$1 $i <>

where $1 the the _rst argument on the command line calling the script and $i

is the line from the _le $HOME/jm/list.

2.8.6 More tricks

Every word of a _le can be placed on a separate line by typing

cat old_filename | tr -cs A-Za-z '\012' > new_filename

The following lists all words in _lename in alphabetical order.

cat filename | tr -cs A-Za-z '\012' | tr A-Z a-z | sort | uniq

You can _nd out when the _le .rhosts was last modi_ed by typing

echo .rhosts last modified on `/bin/ls -l .rhosts | cut -c33-44`

Typing head -n displays the _rst n lines of a _le. And typing last lists the

last logins.

14Send me mail at taylor@ecn.purdue.edu for more information on this service.

28

2.9 Things to try

Just for kicks, I have included a few commands for you to try typing in at the

shell prompt. Make sure you type each line exactly as it appears here.

1. If I had a ( for every $ Congress spent, what would I have?

2. [Where are all those MIAs?

3. echo '[q]sa[ln0=aln256%Pln256/snlbx]sb3135071790101768542287578439snlbxq' |dc

4. got a light?

5. man: Why did you get a divorce?

6. make 'heads or tails of all this'

Note: The auto pilot command found in section 2.5.2 doesn't exist.

29

No comments:

Search

My Blog List