<?xml version="1.0" encoding="ISO-8859-1"?><html xmlns="http://www.w3.org/1999/xhtml" xmlns:xh="http://www.w3.org/1999/xhtml" xmlns:axle="http://www.cs.odu.edu/~zeil/axle" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ns0="http://www.cs.odu.edu/~zeil/axle" ns0:xlinkuri="open/unix.dbk" ns0:xpath="/html" ns0:page="unix" foo="0 " ns0:last="/cocoon/~cs252/open/unix/section-8.2.html"><head><title>An Introduction to Unix</title><link type="text/css" href="_axle/docbookstyle.css" rel="stylesheet"/><link type="text/css" href="_axle/cppformat.css" rel="stylesheet"/><link type="text/css" href="_axle/overrides.css" rel="stylesheet"/><script src="_axle/docbook.js"/><link href="_axle/allPages.css" type="text/css" rel="stylesheet"/><script>function gotoNext() {}
function gotoPrev() {}
function gotoUp() {}
</script><xhtml:link xmlns:xhtml="http://www.w3.org/1999/xhtml" rel="stylesheet" href="_axle/navigation.css" type="text/css"/><xhtml:script xmlns:xhtml="http://www.w3.org/1999/xhtml" src="_axle/navigation.js"/></head><body><table width="100%" class="navigationBar"><tr class="navigationBar"><td class="navigationBar"/><td width="5%" class="navigationBar"><a class="navigationBar" title="CS252 Home" href="http://cocoon.cs.odu.edu:80/cocoon/~cs252/directory/announcements.html"><img class="navigationBar" src="_axle/home.gif" alt="CS252 Home"/></a></td><td width="5%" class="navigationBar"><a class="navigationBar" title="discuss this page" href="http://cocoon.cs.odu.edu:80/cocoon/~cs252/forum/getXPath/open/unix/allPages.html"><img class="navigationBar" src="_axle/forum.gif" alt="discuss this page"/></a></td></tr></table><br/><div class="info_article"><div class="title_article">An Introduction to Unix</div><div class="author_article"><span class="firstname">Steven</span> <span class="othername" role="mi">J</span> <span class="surname">Zeil</span></div><div class="affiliation"><span class="orgname">Old Dominion University</span><span class="orgdiv">Dept. of Computer Science</span></div><table width="100%"><tr><td align="left"><span class="copyright">©<span class="year">2000-2007</span> <span class="holder">Steven J. Zeil, Old Dominion University</span></span></td><td/><td align="right"><span class="date"/></td></tr></table></div><div class="TOC">Contents:<div class="TOC-section0"><a href="unix.html#section-1">1 Getting Started</a></div><div class="TOC-section1"><a href="mainmini.html#mainmini">1.1 Mainframe &amp; Minicomputers</a></div><div class="TOC-section1"><a href="taleof2.html#taleof2">1.2 A Tale of Two Operating Systems</a></div><div class="TOC-section1"><a href="reflections.html#reflections">1.3 Reflections on MS Windows &amp; Unix</a></div><div class="TOC-section1"><a href="ssh.html#ssh">1.4 Making a Connection: ssh</a></div><div class="TOC-section1"><a href="login.html#login">1.5 Logging In</a></div><div class="TOC-section1"><a href="termtypes.html#termtypes">1.6 Setting Your Terminal Type</a></div><div class="TOC-section1"><a href="section-1.7.html#section-1.7">1.7 Changing Your Password</a></div><div class="TOC-section1"><a href="section-1.8.html#section-1.8">1.8 Logging Out</a></div><div class="TOC-section0"><a href="section-2.html#section-2">2 The Basics</a></div><div class="TOC-section1"><a href="section-2.html#section-2.1">2.1 Files and Directories</a></div><div class="TOC-section1"><a href="section-2.2.html#section-2.2">2.2 Basic Commands</a></div><div class="TOC-section1"><a href="section-2.3.html#section-2.3">2.3 Modifying and Combining Commands</a></div><div class="TOC-section1"><a href="section-2.4.html#section-2.4">2.4 Protection</a></div><div class="TOC-section0"><a href="section-3.html#section-3">3 Editing Files (via Telnet)</a></div><div class="TOC-section1"><a href="section-3.1.html#section-3.1">3.1 In Search of Meta</a></div><div class="TOC-section1"><a href="section-3.2.html#section-3.2">3.2 The .emacs Startup File</a></div><div class="TOC-section1"><a href="section-3.3.html#section-3.3">3.3 Emacs Modes</a></div><div class="TOC-section1"><a href="markregion.html#markregion">3.4 The Mark and the Region</a></div><div class="TOC-section1"><a href="section-3.5.html#section-3.5">3.5 Where's the Documentation?</a></div><div class="TOC-section1"><a href="section-3.6.html#section-3.6">3.6 For the emacs-phobes</a></div><div class="TOC-section0"><a href="section-4.html#section-4">4 The X Window System</a></div><div class="TOC-section1"><a href="section-4.html#section-4.1">4.1 X Servers - Viewing Unix Windows from your PC</a></div><div class="TOC-section1"><a href="section-4.2.html#section-4.2">4.2 Editing Files via X</a></div><div class="TOC-section0"><a href="section-5.html#section-5">5 EMail</a></div><div class="TOC-section1"><a href="section-5.html#section-5.1">5.1 Basic mail processing</a></div><div class="TOC-section1"><a href="section-5.2.html#section-5.2">5.2 pine (optional)</a></div><div class="TOC-section0"><a href="section-6.html#section-6">6 File Transfer</a></div><div class="TOC-section1"><a href="xfermode.html#xfermode">6.1 Before You Start: Understanding Transfer Modes</a></div><div class="TOC-section1"><a href="xfersamba.html#xfersamba">6.2 Transferring Files Across the ODU Local Network: Samba</a></div><div class="TOC-section1"><a href="internetftp.html#internetftp">6.3 Transferring Files Across the Internet: ftp</a></div><div class="TOC-section1"><a href="xferproblems.html#xferproblems">6.4 Problems and Inconsistencies</a></div><div class="TOC-section0"><a href="section-7.html#section-7">7 Program Development</a></div><div class="TOC-section1"><a href="section-7.html#section-7.1">7.1 Program Development via telnet</a></div><div class="TOC-section1"><a href="section-7.2.html#section-7.2">7.2 Program Development via X</a></div><div class="TOC-section0"><a href="section-8.html#section-8">8 Shell Games</a></div><div class="TOC-section1"><a href="section-8.html#section-8.1">8.1 Environment Variables</a></div><div class="TOC-section1"><a href="section-8.2.html#section-8.2">8.2 Scripting</a></div></div><div class="epigraph"><p class="para" align="center">
          (A printable version of this document is available 
          <a url="allPages.html" href="allPages.html">here</a>.)
        </p></div><div class="sect1" axle:nopage="1" id="section-1"><div class="info_sect1"><div class="title_sect2">1. Getting Started</div></div><div class="TOC">Contents:<div class="TOC-section1"><a href="mainmini.html#mainmini">1.1 Mainframe &amp; Minicomputers</a></div><div class="TOC-section2"><a href="mainmini.html#section-1.1.1">1.1.1 CPU and process support</a></div><div class="TOC-section2"><a href="mainmini.html#displays">1.1.2 Display Technology</a></div><div class="TOC-section2"><a href="mainmini.html#networks">1.1.3 Network Technology</a></div><div class="TOC-section1"><a href="taleof2.html#taleof2">1.2 A Tale of Two Operating Systems</a></div><div class="TOC-section2"><a href="taleof2.html#section-1.2.1">1.2.1 Unix</a></div><div class="TOC-section2"><a href="taleof2.html#pcrev">1.2.2 The PC Revolution</a></div><div class="TOC-section2"><a href="taleof2.html#mswin">1.2.3 MSDOS &amp; Windows</a></div><div class="TOC-section1"><a href="reflections.html#reflections">1.3 Reflections on MS Windows &amp; Unix</a></div><div class="TOC-section1"><a href="ssh.html#ssh">1.4 Making a Connection: ssh</a></div><div class="TOC-section2"><a href="ssh.html#puttyConnect">1.4.1 Connecting via PuTTY</a></div><div class="TOC-section2"><a href="ssh.html#sshConnect">1.4.2 Connecting via Command-Line SSH</a></div><div class="TOC-section1"><a href="login.html#login">1.5 Logging In</a></div><div class="TOC-section1"><a href="termtypes.html#termtypes">1.6 Setting Your Terminal Type</a></div><div class="TOC-section1"><a href="section-1.7.html#section-1.7">1.7 Changing Your Password</a></div><div class="TOC-section1"><a href="section-1.8.html#section-1.8">1.8 Logging Out</a></div></div><p class="para">You are reading the beginning of the 
lecture notes for
<a url="http://www.cs.odu.edu/~cs252" href="http://www.cs.odu.edu/~cs252">CS 252</a>, 
Introduction to Unix for
Programmers, a course offered by the ODU Computer Science Department.  

The course is designed to introduce students to the basic Unix
skills that they will need to work productively on the ODU CS Dept.'s
network of Sun workstations and Linux PC's. 
</p><p class="para">
Whether you are enrolled in that course or not, you are welcome to
peruse these notes. 
</p><p class="para">You can tackle the topics in this course in different orders, depending upon your own preferences. <a href="#dependencies"/> shows how the topics that make up this course are related to one another.</p><p class="para">
An arrow from one topic to another means that you should complete the first topic before attempting the second. For example, before looking at "Program Development (via X)" you must have completed both "Program Development (via telnet)" and "Editing Files (via X)". On the other hand, you could do "Shell Games" either before or after doing "X Windows", whichever you prefer. </p><p class="para">In addition to those seeking access to the CS Dept.'s Unix network,
some people may be interested in running Linux on a home PC. Most Linux distributions will offer a terminal/command window that runs a Unix command shell called <span class="quote">
      &#8220;<span class="command">bash</span>&#8221;
    </span>. Others who don't want to abandon Windows entirely may be interested in
the
<a url="http://cygnus.com/cygwin/" href="http://cygnus.com/cygwin/">Cygwin project's</a> free
port of the GNU C/C++ compilers for use on Windows 95/98/NT/2000
machines. Along with the compilers, this package provides a Unix
emulation layer including the <span class="command">bash</span> shell.
Most of what is described in this website applies to
working in <span class="command">bash</span> as well.
</p><p class="para">
We can actually
interpret the title question in a number of different ways.
Do we mean, <span class="quote">
      &#8220;why does the CS department use
Unix?&#8221;
    </span>, or <span class="quote">
      &#8220;why is this course about Unix?&#8221;
    </span>, or <span class="quote">
      &#8220;why was
unix invented?&#8221;
    </span>, or even <span class="quote">
      &#8220;why does Unix behave the way that it
does?&#8221;
    </span></p><p class="para">It's actually the last of these interpretations that I want to
address, although understanding the answer to that question will go
long ways towards explaining why the CS department uses Unix in most
of its courses and, therefore, the very reason for the existence of
this course.</p><p class="para">I think that, to really understand a number of the fundamental
behaviors of Unix, it helps to consider how Unix is different from
what is probably a much more familiar operating system to most of you,
Microsoft Windows.  Furthermore, to understand the differences between
these operating systems, you need to look at the history of computer
hardware and system software evolution in effect at the time when each
of these operating systems was designed. In particular, I want to
focus on three ideas: evolution of CPU's and process support,
evolution in display technology, and evolution in network and
technology.</p><div class="sect2" id="mainmini" axle:xpath="//*[@id='mainmini']" axle:page="mainmini" axle:next="/cocoon/~cs252/open/unix/taleof2.html" axle:prev="/cocoon/~cs252/open/unix/unix.html" axle:up="/cocoon/~cs252/open/unix/unix.html" foo="0 " axle:last="/cocoon/~cs252/open/unix/section-8.2.html" axle:first="/cocoon/~cs252/open/unix/unix.html"><div class="info_sect2"><div class="title_sect3">1.1. Mainframe &amp; Minicomputers</div></div><div class="TOC">Contents:<div class="TOC-section2"><a href="mainmini.html#section-1.1.1">1.1.1 CPU and process support</a></div><div class="TOC-section2"><a href="mainmini.html#displays">1.1.2 Display Technology</a></div><div class="TOC-section2"><a href="mainmini.html#networks">1.1.3 Network Technology</a></div></div>
    
    <div class="sect3" axle:nopage="1" id="section-1.1.1"><div class="info_sect3"><div class="title_sect4">1.1.1. CPU and process support</div></div>
      
      <p class="para">Computer historians are fond of pointing out that mainframe computers
were huge behemoths, occupying massive rooms, drawing large amounts of
electrical power for their operation, and often requiring cooling
systems fully as large as the processor itself. For some time,
processors continued to be physically large, although the processing
power squeezed into that space grew tremendously.</p>
      <p class="para">On the early machines, only a single program could be run at any given
time. As processors became more powerful, both hardware and system
software evolved to permit more than one program to run simultaneously
on the same processor. This is called multiprocessing. The initial
reason for doing multiprocessing was to allow programs from many
different users (programmers) to run at once. This, in turn, is called
multiprogramming. At first, it was assumed that a single user had no
need for more than one process at a time.</p>
      <p class="para">Interactive programs are characterized by long periods of idleness, in
which they are awaiting the next input from the user. In an
interactive environment, it becomes natural for users to switch
attention from one process that is awaiting input or, in some cases,
conducting a lengthy calculation, to another process that has become
more interesting. For example, someone using a word processor might
want to switch over to their calendar to look up an important date
before returning to the word processor and typing that date into their
document. Fortunately, once you have support for multiprogramming, you
have most of which you need for combined multiprogramming and
multiprocessing.</p>
      <p class="para">In fact, there is a definite advantage to having started with
multiprogramming. In a multiprogramming environment, there is a great
danger that one programmer's buggy software could crash and, by
rewriting portions of memory or resetting machine parameters, take
down not only that programmer's program but other programs that
happened to be running on the machine at the same time. Consequently,
multiprogramming systems place a heavy emphasis on security, erecting
hardware and software barriers between processes that make it very
difficult for one process to affect others in any way.</p>
      <p class="para">The trend toward multiprogramming in multiprocessing persisted, not
only across families of mainframe computers, but also across the
increasing number of desk-size <span class="quote">
      &#8220;minicomputers&#8221;
    </span>. It is in this
context that Unix was developed. From the very beginning, Unix was
therefore envisioned as an operating system that would provide support
for both multiprocessing and multiprogramming.</p>
    </div>
    <div class="sect3" id="displays"><div class="info_sect3"><div class="title_sect4">1.1.2. Display Technology</div></div>
      
      <p class="para">During the heyday of the mainframe, most data was entered on punch
cards and most output went directly to a printer. Most of these
systems had a <span class="quote">
      &#8220;console&#8221;
    </span> where commands could be entered directly
from a keyboard, and output received directly on an electric
typewriter-like printer, but such input was slow and inexact. Prior to
multiprocessing, it would have been economic folly to tie up an
expensive CPU waiting for someone to type commands and read output at
merely human speeds. So the system console generally saw use only for
booting up the system, running diagnostics when something was going
wrong, or issuing commands to the computer center staff (e.g., <span class="quote">
      &#8220;Please
mount magnetic tape #4107 on drive 2.&#8221;
    </span>).</p>
      <p class="para">The advent of multiprocessing and the subsequent rise of interactive
computing applications meant that the single system console, hidden
away in the computer room where only the computer center staff ever
touched it, was replaced with a number of <span class="firstterm">computer terminals</span>
accessible to the programmers and data entry staff. An early computer
terminal was, basically, a keyboard for input and an electric
typewriter for output.</p>
      <p class="para">Terminals were not cheap, but their lifetime cost was actually
dominated by the amount of paper they consumed. In fairly short order,
the typewriter output was replaced by a CRT screen. This opened up new
possibilities in output. A CRT screen can be cleared, output to it can
be written at different positions on the screen, and portions of the
screen can be rewritten without rewriting the entire thing.  These
things can't be done when you are printing directly onto a roll of
paper.  Terminal manufacturers began to add control sequences,
combinations of character codes that, instead of printing directly,
would instruct the terminal to shift the location where the next
characters would appear, to change to bold-face or underlined
characters, to clear the screen, etc. All of this wizardry was
hard-wired -- there were no integrated-circuit CPUs that could be
embedded into the box and programmed to produce the desired results.
Consequently, terminals were quite expensive (the fancier ones costing
as much as a typical new car). Different manufacturers selected their
control sequences as much based upon what they could wire in easily as
upon any desire for uniformity or compatability. Consequently, there
were eventually hundreds of models of CRT-based computer terminals,
<span class="emphasis">all of which used incompatible sets of control sequences</span>.</p>
      <p class="para">Embedded microprocessors eventually simplified the design of computer
terminals considerably (sinking a number of companies along the way
that had made their money leasing the older expensive models), and the
capabilities of computer terminal began to grow, including adding
graphics and color capabilities. Eventually, PCs became cheap enough
that the whole idea of a dedicated box serving merely as a terminal
came into question, and the computer terminal now exists as a separate
entity only in very special circumstances, although there are periodic
attempts to revive the idea (e.g., so-called internet appliances).</p>
    </div>
    <div class="sect3" id="networks"><div class="info_sect3"><div class="title_sect4">1.1.3. Network Technology</div></div>
      
      <p class="para">Before there was a World-Wide Web, there was an Internet.  The
internet grew out of a deliberate attempt to allow researchers all
around the country access to the limited number of highly expensive
mainframe CPU's. Internet traffic originally was dominated by telnet,
a protocol for issuing text commands to a computer via the internet,
and ftp, a protocol for transferring files from machine to machine via
the Internet. Email came along later.</p>
      <p class="para">In imitation of (and perhaps in jealousy of) the internet,
UseNet evolved as an anarchic collection of mainframe and minicomputers that
each knew a handful of telephone numbers of other UseNet computers and could
pass email and news (a.k.a. bulletin board) entries along those connections.</p>
      <p class="para">As the idea of long range networking took hold, more and more sites
began installing local area networks to enable communication among
their own machines.</p>
    </div>
  </div><div class="sect2" id="taleof2" axle:xpath="//*[@id='taleof2']" axle:page="taleof2" axle:next="/cocoon/~cs252/open/unix/reflections.html" axle:prev="/cocoon/~cs252/open/unix/mainmini.html" axle:up="/cocoon/~cs252/open/unix/unix.html" foo="0 " axle:last="/cocoon/~cs252/open/unix/section-8.2.html" axle:first="/cocoon/~cs252/open/unix/unix.html"><div class="info_sect2"><div class="title_sect3">1.2. A Tale of Two Operating Systems</div></div><div class="TOC">Contents:<div class="TOC-section2"><a href="taleof2.html#section-1.2.1">1.2.1 Unix</a></div><div class="TOC-section2"><a href="taleof2.html#pcrev">1.2.2 The PC Revolution</a></div><div class="TOC-section2"><a href="taleof2.html#mswin">1.2.3 MSDOS &amp; Windows</a></div></div>
    
    <div class="sect3" axle:nopage="1" id="section-1.2.1"><div class="info_sect3"><div class="title_sect4">1.2.1. Unix</div></div>
      
      <p class="para">Unix evolved for minicomputers in an historical context where
<div class="itemizedlist"><ul><li>Multiprocessing
     was expected, and the hardware provided safeguards for protecting
     one running process from affecting or being affected by other
     processes on the same CPU.</li><li>The most common displays were computer terminals, which came in
      many different models, all of which used mutually incompatable
      control sequences. Most of these could display text only, or
      text with simple vertical &amp; horizontal line graphics. <span class="quote">
      &#8220;True&#8221;
    </span>
      graphics terminals were not unknown, and were clearly on their
      way, but were so expensive as to be comparatively rare.</li><li>Networking
     was common. In fact, it was normal, perhaps even the rule, for
     users to be controlling, via the network, machines that were
     remote from the users' actual location.</li></ul></div></p>
    </div>
    <div class="sect3" id="pcrev"><div class="info_sect3"><div class="title_sect4">1.2.2. The PC Revolution</div></div>
      
      <p class="para">When personal-computer (PC's) came on the scene, they represented a
revolution in terms of both decreased size and decreased cost, but
they represented a step backwards in terms of total computing power
and in terms of the sophistication of the hardware support for many
systems programming activities.</p>
      <p class="para">Oddly enough, PC systems seemed to recap the entire history of
computing up till that time, thoug at a somewhat faster pace:
<div class="itemizedlist"><ul><li><span class="quote">
      &#8220;One user -- One CPU&#8221;
    </span> was a rallying
     cry of the early PC proponents. They argued that, although an
     individual PC presented limited CPU power compared to mainframe
     or mini machines, the individual PC could still provide a single
     user with more CPU power than that person would receive as their
     <span class="emphasis">share</span> of a mainframe when split over a large number of
     simultaneous users. So early PC operating systems returned to the
     single-user, single-process<a onmouseover="DBshowBriefly('fn_fref1_live')" name="fn_fref1" id="fn_fref1" class="footnotemark" href="taleof2.html#fn_fnote1">1</a><div class="footnote_live" id="fn_fref1_live">because, after all, that
     single user didn't really need to split those precious CPU cycles
     among more than one application at a time, right?</div> model that had
     gone out of fashion decades before in the world of larger
     computers.</li><li>Display technology reverted
     initially to the electric-typewriter style system console. This
     was quickly supplanted by a <span class="quote">
      &#8220;dumb terminal&#8221;
    </span> CRT-and-keyboard,
     though early printers were still electric typewriter
     based.<a onmouseover="DBshowBriefly('fn_fref2_live')" name="fn_fref2" id="fn_fref2" class="footnotemark" href="taleof2.html#fn_fnote2">2</a><div class="footnote_live" id="fn_fref2_live">In fact, I recall seeing ads for a device
     consisting of a panel of solenoids and control circuits that
     could be placed over the keyboard of an electric typewriter. Send
     the panel the ASCII code for an <span class="quote">
      &#8220;a&#8221;
    </span>, and a solenoid <span class="quote">
      &#8220;finger&#8221;
    </span>
     would punch down right where the <span class="quote">
      &#8220;a&#8221;
    </span> key would be on a
     typewriter. 
      Send it the ASCII code for
     <span class="quote">
      &#8220;Z&#8221;
    </span>, and a pair of solenoids would strike the typewriter's
     <span class="quote">
      &#8220;shift&#8221;
    </span> and <span class="quote">
      &#8220;z&#8221;
    </span> keys.</div>

<p class="para">     The very existence of integrated circuit CPUs, however, lowered
     the cost of CRT displays to the point where more elaborate,
     graphics-capable displays were soon available.</p></li><li>Network technology was initially
     spurned. <span class="quote">
      &#8220;One user -- One CPU&#8221;
    </span>, remember? Why would anyone
     <span class="emphasis">need</span> access to other computers. Email and net news could be
     handled by modem connection without full-fledged networking. It
     took a surprisingly long time before PC operating systems and
     applications began to acknowledge that not every bit of
     information and not every hardware/software resource could
     economically be replicated on every PC
     system.</li></ul></div></p>
    <div name="fn_fnote1" id="fn_fnote1" class="footnote">1. because, after all, that
     single user didn't really need to split those precious CPU cycles
     among more than one application at a time, right? (<a href="taleof2.html#fn_fref1">back</a>)
    </div><div name="fn_fnote2" id="fn_fnote2" class="footnote">2. In fact, I recall seeing ads for a device
     consisting of a panel of solenoids and control circuits that
     could be placed over the keyboard of an electric typewriter. Send
     the panel the ASCII code for an <span class="quote">
      &#8220;a&#8221;
    </span>, and a solenoid <span class="quote">
      &#8220;finger&#8221;
    </span>
     would punch down right where the <span class="quote">
      &#8220;a&#8221;
    </span> key would be on a
     typewriter. 
      Send it the ASCII code for
     <span class="quote">
      &#8220;Z&#8221;
    </span>, and a pair of solenoids would strike the typewriter's
     <span class="quote">
      &#8220;shift&#8221;
    </span> and <span class="quote">
      &#8220;z&#8221;
    </span> keys. (<a href="taleof2.html#fn_fref2">back</a>)
    </div></div>
    <div class="sect3" id="mswin"><div class="info_sect3"><div class="title_sect4">1.2.3. MSDOS &amp; Windows</div></div>
      
      <p class="para">MSDOS was
developed in a context where
<div class="itemizedlist"><ul><li><span class="quote">
      &#8220;One user -- One CPU&#8221;
    </span> was the rule.
     Multiple processes for a single user were not deemed necessary.</li><li>Most PCs had a CRT display with
     limited character and graphics capabilities.</li><li>Networking was deemed unnecessary.</li></ul></div></p>
      <p class="para">As MSDOS evolved
into Windows, it did so in response to changes in the HW/SW context:
<div class="itemizedlist"><ul><li><span class="quote">
      &#8220;One user -- One CPU&#8221;
    </span> remained the
     rule, but a single user might have multiple processes.</li><li>PC displays could show characters in
     a variety of fonts, and graphics capabilities were more common.</li><li>Some people might want local
     networking, but it was supplied by third-party add-ons with minimal
     support from the operating system itself. As for the internet, why would
     anyone with a PC want to communicate with all those mainframe
     dinosaurs?</li></ul></div></p>
    </div>
  </div><div class="sect2" id="reflections" axle:xpath="//*[@id='reflections']" axle:page="reflections" axle:next="/cocoon/~cs252/open/unix/ssh.html" axle:prev="/cocoon/~cs252/open/unix/taleof2.html" axle:up="/cocoon/~cs252/open/unix/unix.html" foo="0 " axle:last="/cocoon/~cs252/open/unix/section-8.2.html" axle:first="/cocoon/~cs252/open/unix/unix.html"><div class="info_sect2"><div class="title_sect3">1.3. Reflections on MS Windows &amp; Unix</div></div>
    
    <p class="para">Of course, both MS Windows and Unix continued to evolve past their
earliest forms, but the contexts in which they have evolved helped
establish their fundamental philosophy and continues to influence how
they work today.
<div class="itemizedlist"><ul><li>Unix
     users tend to do a lot more typing than Windows users. Graphics
     capabilities were rare when Unix was developed, so commands had
     to be typed out rather than always working through a window GUI,
     and that practice of typing commands continues to influence the
     <span class="quote">
      &#8220;look and feel&#8221;
    </span> of Unix. Unix does have a windowing GUI, but the
     Unix approach is to launch an application via a typed command,
     then let that application open up windows if it needs
     them. Compare to MS Windows, where most users never type a
     command, and may not even realize that many common Windows
     applications offer a variety of command line options. (Try, for
     example, creating shortcuts on your Windows desktop with the
     Target: 
<div class="screen"><span class="userinput">
C:\WINDOWS\EXPLORER.EXE
</span></div>
 and another with the Target:
<div class="screen"><span class="userinput">
C:\WINDOWS\EXPLORER.EXE /n,/e,c:\
</span></div>
     Try them each and see what they do. The difference is potentially
     useful, but this possibility is unknown to most Windows users.</li><li>Because
     graphics capabilities were rare when Unix was developed, many
     Unix applications are text based. The canonical Unix application
     reads a stream of text in (from <span class="quote">
      &#8220;standard input&#8221;
    </span>) and produces a
     stream of text as output (on <span class="quote">
      &#8220;standard output&#8221;
    </span>). Because the output is
     often considered a slightly modified version of the input, such
     programs are called <span class="firstterm">filters</span>. Unix users are more likely to
     string together a bunch of filters that each do something simple
     than to hunt for a massive dedicated application that does the
     whole job at once. For example, if you wanted to know how many
     statements occurred in some file of C++ code, an MS Windows
     programmer would load that file into a visual C++ programming
     environment, then search through the menu bars for a <span class="quote">
      &#8220;properties&#8221;
    </span>
     or <span class="quote">
      &#8220;number of statements&#8221;
    </span> item that might convey the required
     information. A Unix programmer would, in a single line of text
     commands, feed that file into a program that extracted each line
     containing a <span class="quote">
      &#8220;;&#8221;
    </span> (because most C++ statements end with semicolons) and
     then feed that set of <span class="quote">
      &#8220;;&#8221;
    </span>-containing lines through a
     line-counting filter.</li><li><span class="quote">
      &#8220;One user -- One CPU&#8221;
    </span> thinking dominates
     Windows applications even though networking is widely
     available. MS Windows has never offered the same level of
     protection against one process affecting others on the same
     machine. In Windows, it's still all too common for a single
     crashing application to lock up the machine to the point where a
     reboot is required. In Unix, such reboots are quite rare.

<p class="para">     In part, this is because Unix allows processes to interact in
     only two ways:
     <div class="itemizedlist"><ul><li>By having one process write out files that the other reads
     </li><li>By having one process write characters into a <span class="quote">
      &#8220;pipeline&#8221;
    </span>
           that is read by another process.
     </li></ul></div></p><p class="para">     By contrast, MS Windows has a variety of communication
     mechanisms, some of which allow one process to directly
     manipulate the data of another. This allows different applications to
     work together in interesting and valuable ways (e.g., embedding charts
     from a spreadsheet directly inside a word-processor document), but at
     an inevitable cost to overall system security and stability.</p></li><li>Unix
     applications are often designed to be run remotely, via a network. Even
     the Unix windowing GUI, called <span class="firstterm">X</span>, is based on the idea that an
     application can be run on whatever machine it is installed upon, but will
     actually display its windows on and accept its mouse clicks and other
     inputs from any machine on the network. MS Windows, on the other hand,
     assumes that the application is running on the machine where its outputs
     should be displayed. If you want to use, say, a paint program that isn't
     installed on the PC you are sitting at but is installed on another PC in
     the same room, you can't run that program without actually getting up and
     moving to the other PC.</li><li>Another instance of this bias, one that is, I suspect, much
      closer to the hearts and pocketbooks of Windows applications
      developers, can be seen
     in the deliberate ignorance of many applications to the realities
     of the networking world. Although Windows will provide support for different users
     logging in (one at a time!) to given PC, the majority of
     application software that a user might install on that PC will assume
     that a single data area and a single set of option and preference
     settings are enough for that PC. Therefore every time you change
     a setting, you affect the behavior of that application not only
     for yourself, but for all other people who use that same
     machine. Unix applications, on the other hand, are far more
     likely to be distributed under site licenses permitting all users
     at a site equal access.<a onmouseover="DBshowBriefly('fn_fref3_live')" name="fn_fref3" id="fn_fref3" class="footnotemark" href="reflections.html#fn_fnote3">3</a><div class="footnote_live" id="fn_fref3_live">Indeed, Unix applications are far more
     likely to be distributed with source code.  It's worth noting
     that both the Free Software movement and the Open Source movement
     originated in the Unix community. By contrast, Microsoft has
     recently contended that any software that is distributed free of
     charge or that is distributed with source code is <span class="quote">
      &#8220;potentially
     viral&#8221;
    </span> and <span class="quote">
      &#8220;anti-American&#8221;
    </span>.</div>

<p class="para">     If you have PCs arranged on a
     network, so that they can access a common pool of disk drives,
     when you purchase a new windows application, it is likely to try
     to install itself onto a single PC, not allowing itself to be run
     from the other PCs that can access the drive where you install the
     software. And even if you could run it by from a different PC,
     you will often find that all of your preferences or options
     settings and all of your accumulated data is squirreled away in
     the registry or systems area of the PC on which the software was
     installed, making it unusable from other
     PCs.</p></li></ul></div></p>
    <p class="para">All this may help to explain why the CS Dept. makes such heavy use of
Unix. It's not that we dislike MS WIndows. But if we require a
particular software package (say, a compiler) in our classes, under
Unix we install it on a few Unix machines and let students run it from
remote locations via the internet. Under Windows we have to install it
on <span class="emphasis">every</span> CS Dept machine, ask that it be installed on every
machine in the ODU laboratories (and at the distance learning
sites). And when an updated version of that package comes out, we have
to go through the entire process all over again. It's just far, far
easier to maintain a consistent working envoronment for all students
under Unix.</p>
  <div name="fn_fnote3" id="fn_fnote3" class="footnote">3. Indeed, Unix applications are far more
     likely to be distributed with source code.  It's worth noting
     that both the Free Software movement and the Open Source movement
     originated in the Unix community. By contrast, Microsoft has
     recently contended that any software that is distributed free of
     charge or that is distributed with source code is <span class="quote">
      &#8220;potentially
     viral&#8221;
    </span> and <span class="quote">
      &#8220;anti-American&#8221;
    </span>. (<a href="reflections.html#fn_fref3">back</a>)
    </div></div><p class="para" axle:xpath="//*[@id='section-1']/p[9]" axle:page="reflections" axle:next="/cocoon/~cs252/open/unix/ssh.html" axle:prev="/cocoon/~cs252/open/unix/taleof2.html" axle:up="/cocoon/~cs252/open/unix/unix.html" foo="0 " axle:last="/cocoon/~cs252/open/unix/section-8.2.html" axle:first="/cocoon/~cs252/open/unix/unix.html">There are two ways to interact with Unix and Unix programs: through a
<span class="emphasis">text-only</span> interface, or via a <span class="emphasis">windows</span> interface --- the Unix
windowing system is called <span class="emphasis">X</span>.  
Which mode of interaction you use depends upon how you are accessing to
the network, the software on the machine you are sitting at, and how
fast your connection to the network is.  But even if you are running
X, one of the first things you are likely to open is an <span class="quote">
      &#8220;xterm&#8221;
    </span>, a
text-only command window. 
Unix users tend to launch
programs from the text-only interface. If the program itself supports
windows, mice, etc., then they can point and click to their heart's content.</p><p class="para" axle:xpath="//*[@id='section-1']/p[10]" axle:page="reflections" axle:next="/cocoon/~cs252/open/unix/ssh.html" axle:prev="/cocoon/~cs252/open/unix/taleof2.html" axle:up="/cocoon/~cs252/open/unix/unix.html" foo="0 " axle:last="/cocoon/~cs252/open/unix/section-8.2.html" axle:first="/cocoon/~cs252/open/unix/unix.html">In this section, we'll concentrate on access via
<span class="code">ssh</span>, an internet protocol for issuing interactive commands to
remote machines. Window-based X access will be introduced
<a href="xwinlaunch">later</a>.</p><p class="para" axle:xpath="//*[@id='section-1']/p[11]" axle:page="reflections" axle:next="/cocoon/~cs252/open/unix/ssh.html" axle:prev="/cocoon/~cs252/open/unix/taleof2.html" axle:up="/cocoon/~cs252/open/unix/unix.html" foo="0 " axle:last="/cocoon/~cs252/open/unix/section-8.2.html" axle:first="/cocoon/~cs252/open/unix/unix.html">
  There is an older text-based protocol for giving commands to Unix
  called "telnet". telnet is still used for a variety of purposes, but
  has fallen out of favor because it sends everything (including your
  login name and password) in plain-text format, leaving you vulnerable to someone else on your network eavesdropping via "packet sniffers".
</p><p class="para" axle:xpath="//*[@id='section-1']/p[12]" axle:page="reflections" axle:next="/cocoon/~cs252/open/unix/ssh.html" axle:prev="/cocoon/~cs252/open/unix/taleof2.html" axle:up="/cocoon/~cs252/open/unix/unix.html" foo="0 " axle:last="/cocoon/~cs252/open/unix/section-8.2.html" axle:first="/cocoon/~cs252/open/unix/unix.html">
  By contrast, ssh encrypts all your communications, so you are safe even if someone is eavedropping. As of Summer 2008, 
  the ODU CS dept will no longer support telnet connections.
</p><div class="sect2" id="ssh" axle:xpath="//*[@id='ssh']" axle:page="ssh" axle:next="/cocoon/~cs252/open/unix/login.html" axle:prev="/cocoon/~cs252/open/unix/reflections.html" axle:up="/cocoon/~cs252/open/unix/unix.html" foo="0 " axle:last="/cocoon/~cs252/open/unix/section-8.2.html" axle:first="/cocoon/~cs252/open/unix/unix.html"><div class="info_sect2"><div class="title_sect3">1.4. Making a Connection: ssh</div></div><div class="TOC">Contents:<div class="TOC-section2"><a href="ssh.html#puttyConnect">1.4.1 Connecting via PuTTY</a></div><div class="TOC-section2"><a href="ssh.html#sshConnect">1.4.2 Connecting via Command-Line SSH</a></div></div>
    
    <p class="para">If you are a student registered for a CS course, and have never had a Unix
account on the CS Dept system, you can get your account by going to
the <a url="http://www.cs.odu.edu" href="http://www.cs.odu.edu">CS Dept home page</a> and clicking on
the <span class="quote">
      &#8220;Account Creation&#8221;
    </span> link (under <span class="quote">
      &#8220;Online Services&#8221;
    </span>).</p>
    <p class="para">If you have had an account in the recent past, it should be
regenerated for you in any semester when you are registered for a CS course.
Otherwise, you will need to contact your instructor or from the CS systems
staff to get your account.</p>
    <p class="para"><span class="quote">
      &#8220;<span class="code">ssh</span>&#8221;
    </span> (secure shell) is an internet protocol that
allows a person connected to the internet to log into other machines
on the internet and to issue commands to those machines. To use
ssh, you must have an <span class="quote">
      &#8220;ssh client&#8221;
    </span> program on the
machine where you are seated, and you must know the name of a machine
elsewhere that is running a <span class="quote">
      &#8220;ssh server&#8221;
    </span>, the program
that accepts logins and subsequent commands from the client.
<a onmouseover="DBshowBriefly('fn_fref4_live')" name="fn_fref4" id="fn_fref4" class="footnotemark" href="ssh.html#fn_fnote4">4</a><div class="footnote_live" id="fn_fref4_live">
<p class="para">
  This is a very common pattern when working over networks. <span class="firstterm">Server</span> programs on one machine offer a service, that <span class="firstterm">client</span> programs, usually on different machines, can request.  Each different kind of service requires a differnet server &amp; client program. <span class="em">ssh</span> provides text and command services. We will later see other client-server arrangements for file transfer, email, and graphics display.
</p>
</div>


</p>
    <p class="para">To choose an ssh server in our Dept.,  select a
machine from <a url="http://system.cs.odu.edu/?page=faq&amp;id=fastmachines" href="http://system.cs.odu.edu/?page=faq&amp;id=fastmachines">this list</a>
at <span class="uri">http://system.cs.odu.edu/?page=faq&amp;id=fastmachines</span>.  </p>

    <p class="para">
      Next, you need to run an ssh client program.  If you are on your
      own PC, you can choose the client program to install and run. If
      you are on a lab PC, at work, or for some other reason using
      someone else's machine, you either need to use whatever they
      have installed or bring your own client program, probably on a
      USB flash drive.

      <div class="itemizedlist"><ul><li>
	  <p class="para">
	    From your own Windows PC: Install <a url="http://www.chiark.greenend.org.uk/~sgtatham/putty/" href="http://www.chiark.greenend.org.uk/~sgtatham/putty/"><span class="firstterm">PuTTY</span></a>. Then proceed to <a url="#puttyConnect" href="ssh.html#puttyConnect">Connecting via PuTTY</a>.
	  </p>
	</li><li>
	  <p class="para">
	    From Windows PCs in CS Dept Labs: Most, if not all,
	will have PuTTY. Run it and proceed to <a url="#puttyConnect" href="ssh.html#puttyConnect">Connecting via PuTTY</a>. In some
	cases, you may find <span class="firstterm">OpenSSH </span> instead.
	  </p>
	</li><li>
	  <p class="para">
	    From Windows PCs in the ODU OCCS Labs: As of May 2008,
	    OCCS machines do not have an ssh client installed. This
	    will probably change soon. In the meantime, install <a url="http://socialistsushi.com/portaputty/" href="http://socialistsushi.com/portaputty/">Portable
	    PuTTY</a> on a USB flashdrive and proceed to <a url="#puttyConnect" href="ssh.html#puttyConnect">Connecting via PuTTY</a>.
	  </p>
	</li><li>
	  <p class="para">
	    From Linux PCs or Windows PCs with CygWin (and probably
	    for Apple machines running OS/X): These machines will have 
	    (or, in the case of CygWin, can easily have installed) a 
	    command-line ssh program. Proceed to  <a url="#sshConnect" href="ssh.html#sshConnect">Connecting via SSH Command Line</a>.
	  </p>
	</li></ul></div>
    </p>


    <div class="sect3" id="puttyConnect"><div class="info_sect3"><div class="title_sect4">1.4.1. Connecting via PuTTY</div></div>
      

      <p class="para">
	  <span class="inlinemediaobject">
	    <img src="puttyConnect.gif" align="left" fileref="puttyConnect.gif"/>
	  </span>

	Run the <span class="code">PuTTY</span> program. You will be presented 
	with a session dialog box like the one shown here. For the "Host Name", fill in the name of the ssh server machine that you chose earlier. For the "Connection Type", make sure that "SSH" is selected. Then click "Open".
      </p>

      <p class="para">
	The first time you connect to any machine, you will get a rather intimidating  warning that "The server's host key is not cached in the registry" followed by a lot of details that uniquely identify the machine you are connecting to. This is a way ssh tries to protect you against people trying to capture your login info by "spoofing" or impersonating a legitimate machine. In practice, you're not likely to know whether this info is correct or not, so click "Yes" to proceed with the connection.
      </p>

      <p class="para">
	If you get a similar message later when reconnecting to a
	machine you have previously used, that might be suspicious (or
	it may just mean that the original machine crashed and has
	been replaced by a different one that was given the same name
	on the network.
      </p>

      <p class="para">
	You should now be prompted for your login name. You are ready to <a url="#login" href="login.html#login">log in</a>.
      </p>

    </div>


    <div class="sect3" id="sshConnect"><div class="info_sect3"><div class="title_sect4">1.4.2. Connecting via Command-Line SSH</div></div>
      

      <p class="para">
	Open a terminal window (or a CygWin command window).
      </p>

      <p class="para">
	Give the command
<div class="screen"><span class="userinput">ssh -l <span class="emphasis-italic" role="italic">yourLoginName</span> <span class="emphasis-italic" role="italic">sshServerName</span> 
</span></div>
filling in your CS Unix login name and the name of the ssh server machine you chose earlier (including the <span class="code">.cs.odu.edu</span> ending).
      </p>

      <p class="para">
        If you are running a Linux or OS/X machine, you might want to try a variation on that command:
<div class="screen"><span class="userinput">ssh -Y -f -l <span class="emphasis-italic" role="italic">yourLoginName</span> <span class="emphasis-italic" role="italic">sshServerName</span> xterm 
</span></div>
filling in your CS Unix login name and the name of the ssh server machine you chose earlier (including the <span class="code">.cs.odu.edu</span> ending). This will usually open up a separate window for your login session.
      </p>

      <p class="para">
	The first time you connect to any machine, you will get a rather intimidating  warning such as  "The authenticity of host ...  can't be established" followed by a lot of details that uniquely identify the machine you are connecting to. This is a way ssh tries to protect you against people trying to capture your login info by "spoofing" or impersonating a legitimate machine. In practice, you're not likely to know whether this info is correct or not, so click "Yes" to proceed with the connection.
      </p>

      <p class="para">
	If you get a similar message later when reconnecting to a
	machine you have previously used, that might be suspicious (or
	it may just mean that the original machine crashed and has
	been replaced by a different one that was given the same name
	on the network.
      </p>

      <p class="para">
	You should now be prompted for your login name. You are ready to <a url="#login" href="login.html#login">log in</a>.
      </p>



    </div>


  <div name="fn_fnote4" id="fn_fnote4" class="footnote">4. 
<p class="para">
  This is a very common pattern when working over networks. <span class="firstterm">Server</span> programs on one machine offer a service, that <span class="firstterm">client</span> programs, usually on different machines, can request.  Each different kind of service requires a differnet server &amp; client program. <span class="em">ssh</span> provides text and command services. We will later see other client-server arrangements for file transfer, email, and graphics display.
</p>
 (<a href="ssh.html#fn_fref4">back</a>)
    </div></div><div class="sect2" id="login" axle:xpath="//*[@id='login']" axle:page="login" axle:next="/cocoon/~cs252/open/unix/termtypes.html" axle:prev="/cocoon/~cs252/open/unix/ssh.html" axle:up="/cocoon/~cs252/open/unix/unix.html" foo="0 " axle:last="/cocoon/~cs252/open/unix/section-8.2.html" axle:first="/cocoon/~cs252/open/unix/unix.html"><div class="info_sect2"><div class="title_sect3">1.5. Logging In</div></div>
    
<div style="clear:right;"/><table class="sidebar" border="1" width="40%"><tr><td>
  <p class="para">Note that as you are typing your password, nothing will happen on your screen. Unix does not echo your password back to you. Most programs and operating systems will not do that with passwords, as a basic security precaution against someone snooping your password from over your shoulder or even electronically.
  </p>
<p class="para">What you may find unusual, however, is that Unix will not even echo back blank characters or *s to show how many characters you have typed. This seems to disturb some people, but it's a a further security precaustion. Just knowing how many characters are in your password would be valuable information to someone trying to crack your account.</p>
</td></tr></table>
    <p class="para">Now that you have a login prompt, enter your login name. At the
<span class="quote">
      &#8220;password:&#8221;
    </span> prompt, enter your password.
</p>
    <p class="para">After a few moments, you should receive a command prompt.</p>
  </div><div class="sect2" id="termtypes" axle:xpath="//*[@id='termtypes']" axle:page="termtypes" axle:next="/cocoon/~cs252/open/unix/section-1.7.html" axle:prev="/cocoon/~cs252/open/unix/login.html" axle:up="/cocoon/~cs252/open/unix/unix.html" foo="0 " axle:last="/cocoon/~cs252/open/unix/section-8.2.html" axle:first="/cocoon/~cs252/open/unix/unix.html"><div class="info_sect2"><div class="title_sect3">1.6. Setting Your Terminal Type</div></div>
    
    <p class="para">Remember that Unix evolved in a time when many manufacturers made many
different models of computer terminals, each with its own set of
command codes for clearing the screen, moving the cursor to different
screen positions, setting bold face, underline and other text
characteristics, and so on.
A typical Unix installation will be equipped to communicate with any
of a few hundred different types of terminals.</p>
    <p class="para">Now, you're not using a terminal, but you are using a program that simulates one. <span class="code">telnet</span> was originally intended to allow terminals to
issue commands to remote CPUs, and <span class="code">ssh</span> is intended as a replacement for the older <span class="code">telnet</span> protocol, so your ssh client program actually
works by simulating an <span class="quote">
      &#8220;old-fashioned&#8221;
    </span> computer terminal. The
authors of your client program chose one or more kinds of terminal
that they would simulate. For Unix to manipulate your screen
appropriately, it most know what kind of terminal command codes your
ssh client program is prepared to accept.</p>
    <p class="para">Find out the kind of terminal
being emulated by your telnet program. You may need to consult the
program documentation  or help files for this. You may also be able to
deduce this information from the program's <span class="quote">
      &#8220;Options&#8221;
    </span> or
<span class="quote">
      &#8220;Preferences&#8221;
    </span> menus.
</p>

<p class="para">
  Here are some common choices:
  <table border="1">
    <colgroup xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:m="http://www.w3.org/1998/Math/MathML">
      <col align="left"/>
      <col align="center"/>
      <col align="left" width="150"/>
    </colgroup>
    <tr xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:m="http://www.w3.org/1998/Math/MathML">
      <th>SSH client program</th>
      <th>TERM type</th>
      <th>Comments</th>
    </tr>
    <tr xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:m="http://www.w3.org/1998/Math/MathML">
      <td>PuTTY</td><td>xterm</td><td/>
    </tr>
    <tr xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:m="http://www.w3.org/1998/Math/MathML">
      <td>OpenSSH</td><td>xterm?</td><td>(Need to check this)</td>
    </tr>
    <tr xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:m="http://www.w3.org/1998/Math/MathML">
      <td>Command-line SSH, basic form, Linux or OS/X</td><td>xterm or linux</td><td/>
    </tr>
    <tr xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:m="http://www.w3.org/1998/Math/MathML">
      <td>Command-line SSH, basic form, CygWin</td><td>xterm or rxvt</td><td>Depends on what type of window you type the command in.</td>
    </tr>
    <tr xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:m="http://www.w3.org/1998/Math/MathML">
      <td>Command-line SSH, <span class="code">xterm</span> variant</td><td>xterm</td><td/>
    </tr>
</table>

</p>
    <p class="para">Now, there is a protocol by which Unix systems can ask a terminal,
<span class="quote">
      &#8220;What kind are you?&#8221;
    </span>, and the terminal responds with an
identification code. Many simpler clients don't implement this
feature, however, so you can't take it for granted.</p>
    <p class="para">Look at the messages that you received after
after logging in. If it includes a line saying something like
<span class="quote">
      &#8220;Terminal type is&#8230;&#8221;
    </span> <span class="emphasis">and</span> the terminal type that it names
makes sense for your ssh client program, you're all set and you can skip
the next step. But it there terminal type seems wrong, or you see a
message indicating that you are on a <span class="quote">
      &#8220;dumb terminal&#8221;
    </span><a onmouseover="DBshowBriefly('fn_fref5_live')" name="fn_fref5" id="fn_fref5" class="footnotemark" href="termtypes.html#fn_fnote5">5</a><div class="footnote_live" id="fn_fref5_live">A
<span class="firstterm">dumb terminal</span> is one that displays lines of text but has no
command codes for moving things to specific locations on the screen or
doing other basic operations. Many Unix applications, such as text editors,
will not work with dumb terminals.</div>, you need to tell the Unix system what
kind of terminal you are <span class="emphasis">really</span> emulating.
 The command to do so is
<div class="screen"><span class="userinput">
   setenv TERM <span class="emphasis">xxxx</span>
</span></div>
where <span class="emphasis">xxxx</span> is the kind of terminal (e.g,
<span class="code">setenv TERM vt100</span>).
</p>
    <p class="para">Some people also recommend that you follow this command with
<div class="screen"><span class="userinput">
   tset -Q
</span></div>
which resets the terminal. In my own experience, this is usually
unnecessary,and I have 
found that many communications programs don't deal well with this, but
try it if your terminal seems to be misbehaving.</p>
    <p class="para">Most <span class="quote">
      &#8220;dumb&#8221;
    </span> terminals provide for 24 lines of text. Many
telnet programs, however, allow more. If yours is one of
these, you should tell Unix how many lines you are using by giving the
command 
<div class="screen"><span class="userinput">
   stty rows <span class="emphasis">nn</span>
</span></div>
where <span class="emphasis">nn</span> is the number of rows/lines.</p>
  <div name="fn_fnote5" id="fn_fnote5" class="footnote">5. A
<span class="firstterm">dumb terminal</span> is one that displays lines of text but has no
command codes for moving things to specific locations on the screen or
doing other basic operations. Many Unix applications, such as text editors,
will not work with dumb terminals. (<a href="termtypes.html#fn_fref5">back</a>)
    </div></div><div class="sect2" id="section-1.7" axle:xpath="//*[@id='section-1.7']" axle:page="section-1.7" axle:next="/cocoon/~cs252/open/unix/section-1.8.html" axle:prev="/cocoon/~cs252/open/unix/termtypes.html" axle:up="/cocoon/~cs252/open/unix/unix.html" foo="0 " axle:last="/cocoon/~cs252/open/unix/section-8.2.html" axle:first="/cocoon/~cs252/open/unix/unix.html"><div class="info_sect2"><div class="title_sect3">1.7. Changing Your Password</div></div>
    
    <p class="para">        Whether we like it or not, we need to worry about the security
of our computing environment.  There are people who would take
advantage of this computer system if they had any, or more complete,
access to it.  This could range from the use of computer resources
they have no right to, to the willful destruction and/or appropriation
of the information we all have online.  In order to maintain the level
of security in our computing environment that we need, there are some
things we all have to take responsibility for.  Even though you may
not feel like you personally have much to lose if someone had access
to your account or files, you have to realize that as soon as someone
gains ANY access to our system, it's 100 times easier for them to gain
access to ALL of it.  So when you are lax with your own account, you
are endangering the work and research of everyone else working here.</p>
    <p class="para">        Your password is the fundamental element of security not only
for your personal account, but for the whole UNIX system that we
share.  Without an account and password a person has NO access to our
system.  If someone discovers (or you tell someone) your password, not
only will they have access to your personal files, but they will have
a much better chance to launch attacks against the security of the
entire system.</p>
    <p class="para">Your account password is the key to accessing and modifying all of
your files.  If another user discovers your password, he or she can
delete all your files, modify important data, read your private
correspondence, and send mail out in your name.  You can lose much
time and effort recovering from such an attack.  If you practice the
following suggestions, you can minimize the risk.</p>
    <p class="para">
      <div class="orderedlist"><ol><li><span class="emphasis-bold" role="bold">NEVER</span> give another user your password.  There is no reason to do
    this.  You can change permissions and have groups set up if you need
    to share access with other individuals.  Your account should be yours
    alone.</li><li>Never write down your password.  Another person can read it from your
    blotter, calendar, etc. as easily as you can.</li><li>Never use passwords that can be easily guessed. Personal
    information about you (birth date, etc.) may be known to the attacker
    or may be recorded in on-line databases that the attacker has
    already obtained.

<p class="para">    Passwords should not be single words (in any
    language) because on-line dictionaries are widely available for
    use in spelling checkers. A common approach to cracking passwords
    is to compile a set of such words 
    and to run a program that
    tries each one on each 
    account on the machine.      Consider inserting punctuation and other 
    <span class="quote">
      &#8220;odd&#8221;
    </span> characters into your password to foil such attacks.</p>

<p class="para">    A person with local knowledge can also try
    your spouse's name, pets' names, etc.  Your account is vulnerable to
    this type of cracking unless you choose your password carefully.</p></li><li>Change your password the very first time you log in, and every
    few months thereafter.  Security
    problems are often traceable to stale passwords and accounts.  These
    are accounts that have become inactive for one reason or another or
    the password has not changed for a long time.  In our particular
    environment we have had break-ins via such stale accounts.  A password
    that remains the same for a long time provides an intruder the
    opportunity to run much more advanced and longer running programs to
    break such passwords.</li><li>Vary the system by which you choose a password.  For example, don't
    repeatedly use combinations like BLUEgreen and REDyellow.  If an
    intruder discovers your pattern, he or she can guess future passwords.</li></ol></div>
    </p>
    <p class="para">The command to change your password is
<div class="screen"><span class="userinput">
   passwd
</span></div>
This command will first prompt you for your old password (just to
check that you really are you!) and then will ask you to type your new
password (twice, so that an inadvertent typing mistake won't leave you
with a password that even you don't know!).</p>
  </div><div class="sect2" id="section-1.8" axle:xpath="//*[@id='section-1.8']" axle:page="section-1.8" axle:next="/cocoon/~cs252/open/unix/section-2.html" axle:prev="/cocoon/~cs252/open/unix/section-1.7.html" axle:up="/cocoon/~cs252/open/unix/unix.html" foo="0 " axle:last="/cocoon/~cs252/open/unix/section-8.2.html" axle:first="/cocoon/~cs252/open/unix/unix.html"><div class="info_sect2"><div class="title_sect3">1.8. Logging Out</div></div>
    
    <p class="para">To leave our Unix systems, type
<div class="screen"><span class="userinput">
  exit
</span></div>
(not <span class="code">logout</span>, as indicated in the textbook.)</p>
  </div></div><div class="sect1" id="section-2" ns0:xpath="//*[@id='section-2']" ns0:page="section-2" ns0:next="/cocoon/~cs252/open/unix/section-2.2.html" ns0:prev="/cocoon/~cs252/open/unix/section-1.8.html" ns0:up="/cocoon/~cs252/open/unix/unix.html" foo="0 " ns0:last="/cocoon/~cs252/open/unix/section-8.2.html" ns0:first="/cocoon/~cs252/open/unix/unix.html"><div class="info_sect1"><div class="title_sect2">2. The Basics</div></div><div class="TOC">Contents:<div class="TOC-section1"><a href="section-2.html#section-2.1">2.1 Files and Directories</a></div><div class="TOC-section2"><a href="section-2.html#basicFileCmds">2.1.1 Getting Started: Navigating Your Directories</a></div><div class="TOC-section2"><a href="section-2.html#cmdlist">2.1.2 Some Common Unix Commands</a></div><div class="TOC-section1"><a href="section-2.2.html#section-2.2">2.2 Basic Commands</a></div><div class="TOC-section2"><a href="section-2.2.html#paths">2.2.1 Command Arguments</a></div><div class="TOC-section2"><a href="section-2.2.html#specialChar">2.2.2 Special Characters</a></div><div class="TOC-section2"><a href="section-2.2.html#cmdpatterns">2.2.3 One Pattern, Many Instances</a></div><div class="TOC-section2"><a href="section-2.2.html#quoting">2.2.4 Quoting</a></div><div class="TOC-section1"><a href="section-2.3.html#section-2.3">2.3 Modifying and Combining Commands</a></div><div class="TOC-section2"><a href="section-2.3.html#redirect">2.3.1 Redirection</a></div><div class="TOC-section2"><a href="section-2.3.html#piping">2.3.2 Pipes</a></div><div class="TOC-section2"><a href="section-2.3.html#xargs">2.3.3 xargs</a></div><div class="TOC-section2"><a href="section-2.3.html#find">2.3.4 find</a></div><div class="TOC-section1"><a href="section-2.4.html#section-2.4">2.4 Protection</a></div><div class="TOC-section2"><a href="section-2.4.html#section-2.4.1">2.4.1 Protection and Permissions</a></div><div class="TOC-section2"><a href="section-2.4.html#section-2.4.2">2.4.2 chmod</a></div><div class="TOC-section2"><a href="section-2.4.html#umask">2.4.3 Beware the umask!</a></div><div class="TOC-section2"><a href="section-2.4.html#section-2.4.4">2.4.4 Planning for Protection</a></div></div><div class="sect2" axle:nopage="1" id="section-2.1"><div class="info_sect2"><div class="title_sect3">2.1. Files and Directories</div></div><div class="TOC">Contents:<div class="TOC-section2"><a href="section-2.html#basicFileCmds">2.1.1 Getting Started: Navigating Your Directories</a></div><div class="TOC-section2"><a href="section-2.html#cmdlist">2.1.2 Some Common Unix Commands</a></div><div class="TOC-section3"><a href="section-2.html#session">2.1.2.1 Session Control</a></div><div class="TOC-section3"><a href="section-2.html#filemanip">2.1.2.2 File and Directory Manipulation</a></div><div class="TOC-section3"><a href="section-2.html#textmanip">2.1.2.3 Text File Manipuation</a></div><div class="TOC-section3"><a href="section-2.html#printing">2.1.2.4 Printing Files</a></div></div><p class="para">Files in Unix are organized by listing them in <span class="emphasis">directories</span>.
Directories are themselves files, and so may appear within other
directories. The result is a tree-like hierarchy. At the root of this
tree is a directory known simply as <span class="quote">
      &#8220;/&#8221;
    </span>.<a onmouseover="DBshowBriefly('fn_fref6_live')" name="fn_fref6" id="fn_fref6" class="footnotemark" href="section-2.html#fn_fnote6">6</a><div class="footnote_live" id="fn_fref6_live">It may be more
precise to say that this directory's name is the empty string <span class="quote">
      &#8220;&#8221;
    </span>.</div>
This directory lists 
various others:</p><p class="para">
    <p class="para" align="center">
      <span class="inlinemediaobject">
        <img src="unixroot.jpg" fileref="unixroot.jpg"/>
      </span>
    </p>
  </p><p class="para">The <span class="code">bin</span> directory contains many of the programs for performing
common Unix commands. The <span class="code">usr</span> directory contains many of the
data files that are required by those and other commands. Of
particular interest, however, is the <span class="code">home</span> directory, which
contains all of the files associated with individual users like you
and me.
Each individual user gets a directory within <span class="code">home</span> bearing their
own login name. My login name is <span class="code">zeil</span>.</p><p class="para">We can expand our view of the Unix files then as:</p><p class="para">
    <p class="para" align="center">
      <span class="inlinemediaobject">
        <img src="unixroot2.jpg" fileref="unixroot2.jpg"/>
      </span>
    </p>
  </p><p class="para"><span class="code">cd</span> and <span class="code">ls</span> are two common Unix commands, as will be
explained later.</p><p class="para">Within my own home directory, I have a directory also named <span class="quote">
      &#8220;bin&#8221;
    </span>,
containing my own personal programs. Two of these are called <span class="quote">
      &#8220;clpr&#8221;
    </span>
and <span class="quote">
      &#8220;psnup&#8221;
    </span>. So these files are arranged as:</p><p class="para">
    <p class="para" align="center">
      <span class="inlinemediaobject">
        <img src="unixroot3.jpg" fileref="unixroot3.jpg"/>
      </span>
    </p>
  </p><p class="para">The full name of any file is given by listing the entire path from the
root of the directory tree down to the file itself, with <span class="quote">
      &#8220;/&#8221;
    </span>
characters separating each directory from what follows. For example,
the full names of the four programs in the above diagram are
<div class="programlisting">
   /bin/cd
   /bin/ls
   /home/zeil/bin/clpr
   /home/zeil/bin/psnup
</div></p><p class="para">There are some common abbreviations that can be used to shorten file
names. 
<div class="itemizedlist"><ul><li>You can refer to the home directory of someone with
login name <span class="emphasis">name</span> as
<span class="code">~<span class="emphasis">name</span></span>.</li><li>You can refer to your own home directory simply as
<span class="code">~</span>.

<p class="para">So you could refer to the file containing my <span class="code">clpr</span> program
as either <span class="code">/home/zeil/bin/clpr</span> or 
<span class="code">~zeil/bin/clpr</span>.</p>

<p class="para">When I
myself am logged in, I can refer to this program by either of those two
names, or simply as
<span class="code">~/bin/clpr</span>.</p></li><li>At all times when
entering Unix commands, you have a <span class="quote">
      &#8220;working&#8221;
    </span> directory. If the file
you want is within that directory (or within other directories
contained in the working directory), the name of the working directory
may be omitted from the start of the filename. When you first log in,
your home directory is your working directory. For example, when I
have just logged 
in, I could refer to my program simply as <span class="code">bin/clpr</span>, dropping the
leading <span class="code">/home/zeil/</span> because that would be my working directory at
that time.</li><li>The working directory itself can be referred to as simply <span class="quote">
      &#8220;.&#8221;
    </span>.</li><li>The <span class="quote">
      &#8220;parent&#8221;
    </span> of the working directory (i.e., the directory
containing the working directory) can be referred to as <span class="quote">
      &#8220;..&#8221;
    </span>.</li></ul></div></p><p class="para">Unix filenames can be almost any length and may contain almost any
characters. As a practical matter, however, you should avoid using
punctuation characters other than the hyphen, the underscore, and the
period. Also, avoid blanks, and non-printable characters within
file names. All of these have special meanings when you are typing
commands and so would be very hard to enter within a filename.</p><p class="para">Some things to keep in mind about Unix file names that may be
different from other file systems you have used:
<div class="itemizedlist"><ul><li>Unix file names are often very long so that they describe their
contents.<a onmouseover="DBshowBriefly('fn_fref7_live')" name="fn_fref7" id="fn_fref7" class="footnotemark" href="section-2.html#fn_fnote7">7</a><div class="footnote_live" id="fn_fref7_live">As we will see, one almost never needs to type an
entire filename in a Unix command, so long file names are no harder to
work with than short ones.</div> The rather perverse exception to this rule
is that program/command names are, by tradition, very short, often
confusingly so.</li><li>Upper and lower case letters are distinct in Unix filenames.
<span class="quote">
      &#8220;<span class="code">MyFile</span>&#8221;
    </span> and <span class="quote">
      &#8220;<span class="code">myfile</span>&#8221;
    </span> are different names.</li><li>Periods (<span class="quote">
      &#8220;.&#8221;
    </span>) are not treated by Unix as a special character.
<span class="quote">
      &#8220;<span class="code">This.Is.a.legal.name</span>&#8221;
    </span> is perfectly acceptable as a Unix
filename. Many programs, however, expect names of their data files to
end in a period followed by a short <span class="quote">
      &#8220;standard&#8221;
    </span> extension indicating
the type of data in that file. Thus data files with names like
<span class="quote">
      &#8220;<span class="code">arglebargle.txt</span>&#8221;
    </span> for text files or <span class="quote">
      &#8220;<span class="code">nonsense.cpp</span>&#8221;
    </span>
for C++ source code are common.

<p class="para">By convention, files containing executable programs generally do not
receive such an extension.</p></li><li>Keep in mind that directories are separated by <span class="quote">
      &#8220;/&#8221;
    </span> in file
names, not by <span class="quote">
      &#8220;<span class="inlineequation"><span class="mathphrase">\</span></span>&#8221;
    </span> as is common in some other operating
systems. </li></ul></div></p><div class="sect3" axle:nopage="1" id="basicFileCmds"><div class="info_sect3"><div class="title_sect4">2.1.1. Getting Started: Navigating Your Directories</div></div>
    

  <p class="para">If you have not yet done so, log in now so that you can work though
the following commands.</p>


<div style="clear:right;"/><table class="sidebar" border="1" width="40%"><tr><td>
<p class="para">
Throughout this course you will encounter sections labelled "Try This:". That means that I <span class="em">really</span> want you to log in, try the commands or procedures I describe, and observe and think about the results.
</p>
<p class="para">
If you don't understand the output you receive, please use the <span class="inlinemediaobject"><img src="_axle/forum.gif" align="top" fileref="_axle/forum.gif"/></span> symbol at the top of the page to post a question in the Forum.
</p>
<p class="para">Some of the <span class="quote">
      &#8220;Try This:&#8221;
    </span> exercises will build on the earlier ones, so skipping these early examples may lead to problems later on.
</p>
</td></tr></table>

<div class="informalexample"><div class="title_example">Try This:</div><p class="para">Upon logging in, your <span class="emphasis">working directory</span> should be your home
directory. The command <span class="code">pwd</span> will <span class="emphasis-bold" role="bold">p</span>rint the <span class="emphasis-bold" role="bold">w</span>orking <span class="emphasis-bold" role="bold">d</span>irectory. Give the command
<div class="screen"><span class="userinput">
   pwd
</span></div>
You should see something like
<div class="screen"><span class="computeroutput">
/home/yourname
</span></div></p><p class="para">Now, let's make a place to play in. <span class="code">mkdir</span> will make a new
directory.
Enter the command
<div class="screen"><span class="userinput">
   mkdir playing
</span></div>
to create a directory named <span class="quote">
      &#8220;playing&#8221;
    </span>.</p><p class="para">The command <span class="code">ls</span> lists the contents of the working directory.
More generally, <span class="code">ls <span class="emphasis">directoryname</span></span> will list the contents
of any directory.<a onmouseover="DBshowBriefly('fn_fref8_live')" name="fn_fref8" id="fn_fref8" class="footnotemark" href="section-2.html#fn_fnote8">8</a><div class="footnote_live" id="fn_fref8_live">Well, not really <span class="emphasis">any</span> directory.
People can 
    <a href="../fileprot/">protect their own directories</a>
from the
prying eyes of others, in which case <span class="code">ls</span> will fail.</div></p><p class="para">Give the command 
<div class="screen"><span class="userinput">
   ls
</span></div>
and you should see <span class="code">playing</span> listed. In fact, it may be the only
thing listed.</p><p class="para">The command <span class="code">cd</span> is used to <span class="emphasis-bold" role="bold">c</span>hange the working <span class="emphasis-bold" role="bold">d</span>irectory. Give the command sequence
<div class="programlisting">
   pwd
   cd playing
   pwd
   cd ..
   pwd
   cd ./playing
   pwd
</div>
to see this in action.</p></div>

    <p class="para">The <span class="code">cp</span> command copies one or more files. You can give this
command as <span class="code">cp file1 file2</span> to make a copy of file <span class="code">file1</span>,
the copy being named <span class="code">file2</span>. Alternatively, you can copy one or
more files into a directory by giving the command as
<div class="screen"><span class="userinput">
cp file<span class="inlineequation"><span class="mathphrase"><sub>1</sub></span></span> file<span class="inlineequation"><span class="mathphrase"><sub>2</sub></span></span>&#8230;file<span class="inlineequation"><span class="mathphrase"><sub>n</sub></span></span> directory
</span></div></p>

<div class="informalexample"><div class="title_example">Try This:</div><p class="para">Now try the following:
<div class="programlisting">
   ls /usr/include
</div>
You should see a large number of files, including many ending in
<span class="quote">
      &#8220;.h&#8221;
    </span>. 
Copy two of
these files and check to see that the copy was successful, as follows:
<div class="programlisting">
   cp /usr/include/math.h /usr/include/stdio.h ~/playing
   ls ~/playing
</div>
</p></div>

  <div name="fn_fnote8" id="fn_fnote8" class="footnote">8. Well, not really <span class="emphasis">any</span> directory.
People can 
    <a href="../fileprot/">protect their own directories</a>
from the
prying eyes of others, in which case <span class="code">ls</span> will fail. (<a href="section-2.html#fn_fref8">back</a>)
    </div></div><div class="sect3" id="cmdlist"><div class="info_sect3"><div class="title_sect4">2.1.2. Some Common Unix Commands</div></div><div class="TOC">Contents:<div class="TOC-section3"><a href="section-2.html#session">2.1.2.1 Session Control</a></div><div class="TOC-section3"><a href="section-2.html#filemanip">2.1.2.2 File and Directory Manipulation</a></div><div class="TOC-section3"><a href="section-2.html#textmanip">2.1.2.3 Text File Manipuation</a></div><div class="TOC-section3"><a href="section-2.html#printing">2.1.2.4 Printing Files</a></div></div>
    
    <p class="para">Here are some common Unix commands. Some of these will be discussed
in more detail later, but if this list at least makes you aware of the
existence of a command that does something you want, then you can
always get the details on the command by reading its on-line manual
page via the <span class="code">man</span> command:
<div class="programlisting">
  man <span class="emphasis">command-name</span>
</div>
</p>
    <p class="para">
You might try experimenting with these in 
your <span class="code">~/playing</span> directory.
</p>
    <div class="sect4" id="session"><div class="info_sect4"><div class="title_sect5">2.1.2.1. Session Control</div></div>
      
      <p class="para">
        <div class="variablelist"><dl><dt class="varlistentry"><span class="term">exit</span></dt><dd class="varlistitem"> Shut down the current shell. If this shell
is the one you got at log-in, this command logs you
out.</dd><dt class="varlistentry"><span class="term">rlogin <span class="emphasis">machine</span></span></dt><dd class="varlistitem"><span class="anchor" id="rlogin"/>Logs you in to another machine on the
network. Use this if the machine you are on seems to be running slowly
and the <span class="code">who</span> command indicates that there are lots of others on
the same machine.

<p class="para"/></dd><dt class="varlistentry"><span class="term">who</span></dt><dd class="varlistitem"> Lists everyone logged into the same machine that you are
using.</dd></dl></div>
      </p>
    </div>
    <div class="sect4" id="filemanip"><div class="info_sect4"><div class="title_sect5">2.1.2.2. File and Directory Manipulation</div></div>
      
      <p class="para">
        <div class="variablelist"><dl><dt class="varlistentry"><span class="term">cd <span class="emphasis">directory</span></span></dt><dd class="varlistitem"><span class="anchor" id="cdcmd"/> Changes your current working directory to the indeicated <span class="emphasis">directory</span>, which may be absolute or relative.</dd><dt class="varlistentry"><span class="term">find <span class="emphasis">directory</span> <span class="emphasis">instructions</span></span></dt><dd class="varlistitem"><span class="anchor" id="findcmd"/>Searches the indicated directory and any subdorectories
within it for files. The instructions may serve to limit the files
found (e.g., search for files with a given name or that have been
modified after a given date) or may indicate commands to run on those files.
</dd><dt class="varlistentry"><span class="term">ls <span class="emphasis">path</span></span></dt><dd class="varlistitem">Lists all the files matching the indicated <span class="emphasis">path</span>. If that path is a directory, lists all the files within that directory. You can provide multiple paths in the same command, or omit the path entirely (in which case the contents of your current working directory will be listed). <span class="code">ls</span> can be modified with a number of <span class="quote">
      &#8220;flags&#8221;
    </span>. Some of the more common are:
<div class="variablelist"><dl><dt class="varlistentry" id="lsa"><span class="term">ls -a</span></dt><dd class="varlistitem">By default, filenames beginning with <span class="quote">
      &#8220;.&#8221;
    </span> are considered
  <span class="quote">
      &#8220;hidden&#8221;
    </span> and not shown by the <span class="code">ls</span> command. The <span class="code">-a</span> (for
  <span class="quote">
      &#8220;<span class="emphasis-bold" role="bold">a</span>ll&#8221;
    </span>) option causes these files to be shown as well.</dd><dt class="varlistentry"><span class="term">ls -l</span></dt><dd class="varlistitem"> This is the <span class="quote">
      &#8220;long&#8221;
    </span> form of <span class="code">ls</span>. It displays
  additional information about each file, such as the size and date on
  which the file was last modified.

  <p class="para">Note that options can be combined. For example, you can say
  <span class="code">ls -la</span> to get extra information including normally hidden files.</p></dd><dt class="varlistentry"><span class="term">ls -F</span></dt><dd class="varlistitem"><span class="anchor" id="lsF"/> Adds a little bit of extra information to each
  file name. 
  If the file is an executable program, its name is marked with an
  <span class="quote">
      &#8220;*&#8221;
    </span>. If the file is a directory, its name is marked with <span class="quote">
      &#8220;/&#8221;
    </span>.</dd></dl></div>
</dd><dt class="varlistentry"><span class="term">mkdir <span class="emphasis">directory</span></span></dt><dd class="varlistitem">  Creates a directory.</dd><dt class="varlistentry"><span class="term">mv <span class="emphasis">file<span class="inlineequation"><span class="mathphrase"><sub>1</sub></span></span></span> <span class="emphasis">file<span class="inlineequation"><span class="mathphrase"><sub>2</sub></span></span></span></span></dt><dd class="varlistitem">     Renames <span class="emphasis">file<span class="inlineequation"><span class="mathphrase"><sub>1</sub></span></span></span> as
<span class="emphasis">file<span class="inlineequation"><span class="mathphrase"><sub>2</sub></span></span></span>. <span class="emphasis">file<span class="inlineequation"><span class="mathphrase"><sub>2</sub></span></span></span> may be in a different directory.</dd><dt class="varlistentry"><span class="term">mv <span class="emphasis">file<span class="inlineequation"><span class="mathphrase"><sub>1</sub></span></span></span> <span class="emphasis">directory</span></span></dt><dd class="varlistitem">    Moves <span class="emphasis">file<span class="inlineequation"><span class="mathphrase"><sub>1</sub></span></span></span> to
the given directory.</dd><dt class="varlistentry"><span class="term">pwd</span></dt><dd class="varlistitem"><span class="anchor" id="pwdcmd"/> Prints your current working directory.</dd><dt class="varlistentry"><span class="term">rm <span class="emphasis">file<span class="inlineequation"><span class="mathphrase"><sub>1</sub></span></span></span>&#8230;<span class="emphasis">file<span class="inlineequation"><span class="mathphrase"><sub>n</sub></span></span></span></span></dt><dd class="varlistitem"> Deletes the listed
files. Be very careful using wildcards with this command.
<span class="code">rm *</span> will delete everything in the current working
directory!</dd><dt class="varlistentry"><span class="term">rm -i <span class="emphasis">file<span class="inlineequation"><span class="mathphrase"><sub>1</sub></span></span></span>&#8230;<span class="emphasis">file<span class="inlineequation"><span class="mathphrase"><sub>n</sub></span></span></span></span></dt><dd class="varlistitem"> Deletes the listed
files, but firsts asks permission to delete each one.</dd><dt class="varlistentry"><span class="term">rm -r <span class="emphasis">file<span class="inlineequation"><span class="mathphrase"><sub>1</sub></span></span></span>&#8230;<span class="emphasis">file<span class="inlineequation"><span class="mathphrase"><sub>n</sub></span></span></span></span></dt><dd class="varlistitem"> Deletes the listed
files. If any of these files is a directory, it deletes that directory
and everything in it as well.</dd><dt class="varlistentry"><span class="term">rmdir <span class="emphasis">directory</span></span></dt><dd class="varlistitem">  Deletes a directory
(if empty).</dd></dl></div>
      </p>
    </div>
    <div class="sect4" id="textmanip"><div class="info_sect4"><div class="title_sect5">2.1.2.3. Text File Manipuation</div></div>
      
      <p class="para">
        <div class="variablelist"><dl><dt class="varlistentry"><span class="term">cat <span class="emphasis">file<span class="inlineequation"><span class="mathphrase"><sub>1</sub></span></span></span>&#8230;<span class="emphasis">file<span class="inlineequation"><span class="mathphrase"><sub>n</sub></span></span></span></span></dt><dd class="varlistitem"><span class="anchor" id="catcmd"/> Lists the
contents of each of the listed files on your screen.</dd><dt class="varlistentry"><span class="term">grep <span class="emphasis">text-pattern</span>
<span class="emphasis">file<span class="inlineequation"><span class="mathphrase"><sub>1</sub></span></span></span>&#8230;<span class="emphasis">file<span class="inlineequation"><span class="mathphrase"><sub>n</sub></span></span></span></span></dt><dd class="varlistitem">
Searches the files for lines matching the indicated text pattern and
prints the matching lines.</dd><dt class="varlistentry" id="morecmd"><span class="term">more <span class="emphasis">file<span class="inlineequation"><span class="mathphrase"><sub>1</sub></span></span></span>&#8230;<span class="emphasis">file<span class="inlineequation"><span class="mathphrase"><sub>n</sub></span></span></span></span></dt><dd class="varlistitem">  Lists files one
screen at a time, pausing after each screen-full.  Hit the space bar to
advance to the next screen.

<p class="para">A related program is <span class="code">less</span>, which also allows you to move
backwards through the files by hitting <span class="quote">
      &#8220;b&#8221;
    </span>.</p></dd><dt class="varlistentry"><span class="term">sed <span class="emphasis">editing-instructions</span> <span class="emphasis">file</span></span></dt><dd class="varlistitem">
Applies the editing instructions to each line of the file, writing out
the resulting changed version of the file.
</dd></dl></div>
      </p>
    </div>
    <div class="sect4" id="printing"><div class="info_sect4"><div class="title_sect5">2.1.2.4. Printing Files</div></div>
      
      <p class="para">
        <div class="variablelist"><dl><dt class="varlistentry"><span class="term">cancel <span class="emphasis">request</span></span></dt><dd class="varlistitem"> Remove a file from the printer queue, so
that it won't get printed. The
<span class="emphasis">request</span> identifier is found using the <span class="code">lpstat</span> command.</dd><dt class="varlistentry"><span class="term">lp <span class="emphasis">file</span></span></dt><dd class="varlistitem">  Send file to printer for printing. Most sites
have multiple printers, each having its own name. One of these will be
the <span class="quote">
      &#8220;default&#8221;
    </span> printer used by the <span class="code">lp</span> command. For the
others, you must give the printer name as part of the printer command:
<div class="screen"><span class="userinput">
lp -d <span class="emphasis">printer</span> <span class="emphasis">file</span>
</span></div>
For example, at the Norfolk ODU campus, <span class="code">lp</span> by itself prints to
a fast printer in the public workstation lab for text only. <span class="code">lp -dcookie</span>
prints to <span class="quote">
      &#8220;cookie&#8221;
    </span>, a printer in the same room that offers the
extra capability of printing Postscript graphics.

<p class="para">You will need to consult your local staff to see what printers are
available at other sites.</p></dd><dt class="varlistentry"><span class="term">lpstat</span></dt><dd class="varlistitem"> Shows the list of files you have <span class="quote">
      &#8220;queued up&#8221;
    </span> awaiting
their turn on printers. Entered by itself, 
<div class="screen"><span class="userinput">
lpstat
</span></div>
it lists only your own print jobs, giving a unique identifier for each
one.

<p class="para">To see the entire list of print jobs on some printer, enter
<div class="screen"><span class="userinput">
lpstat -o <span class="emphasis">printer</span>
</span></div></p></dd></dl></div>
      </p>
    </div>
  </div><p class="para">As you explore Unix, you are bound to have questions. Some ways to get
answers include:
<div class="itemizedlist"><ul><li>The entire Unix manual is on-line.
<div class="screen"><span class="userinput">
man <span class="emphasis">command</span>
</span></div>
displays the manual page for the given command.

<p class="para"><div class="screen"><span class="userinput">
man -k <span class="emphasis">keyword</span>
</span></div>
looks up the given keyword in an index and lists the commands that may
be relevant.</p></li><li>The CS Department systems staff has collected a variety of additional
help documents. You can find them by going to the Dept home page
(<span class="uri">http://www.cs.odu.edu/</span>) and selecting <span class="quote">
      &#8220;Frequently Asked Questions&#8221;
    </span> under the <span class="quote">
      &#8220;Systems Group&#8221;
    </span> heading.
</li><li>A staff member is generally on duty or on call in the public CS lab in Hughes Hall (on the  Norfolk campus) whenever that room is open.</li><li>If none of the above help, then send e-mail to <span class="quote">
      &#8220;root@cs.odu.edu&#8221;
    </span>. This is
also how you report bugs, machine failures, etc.</li></ul></div></p><div name="fn_fnote6" id="fn_fnote6" class="footnote">6. It may be more
precise to say that this directory's name is the empty string <span class="quote">
      &#8220;&#8221;
    </span>. (<a href="section-2.html#fn_fref6">back</a>)
    </div><div name="fn_fnote7" id="fn_fnote7" class="footnote">7. As we will see, one almost never needs to type an
entire filename in a Unix command, so long file names are no harder to
work with than short ones. (<a href="section-2.html#fn_fref7">back</a>)
    </div></div><div class="sect2" id="section-2.2" ns0:xpath="//*[@id='section-2.2']" ns0:page="section-2.2" ns0:next="/cocoon/~cs252/open/unix/section-2.3.html" ns0:prev="/cocoon/~cs252/open/unix/section-2.html" ns0:up="/cocoon/~cs252/open/unix/section-2.html" foo="0 " ns0:last="/cocoon/~cs252/open/unix/section-8.2.html" ns0:first="/cocoon/~cs252/open/unix/unix.html"><div class="info_sect2"><div class="title_sect3">2.2. Basic Commands</div></div><div class="TOC">Contents:<div class="TOC-section2"><a href="section-2.2.html#paths">2.2.1 Command Arguments</a></div><div class="TOC-section2"><a href="section-2.2.html#specialChar">2.2.2 Special Characters</a></div><div class="TOC-section2"><a href="section-2.2.html#cmdpatterns">2.2.3 One Pattern, Many Instances</a></div><div class="TOC-section3"><a href="section-2.2.html#wildcards">2.2.3.1 Wild Cards</a></div><div class="TOC-section3"><a href="section-2.2.html#regexp">2.2.3.2 Regular Expressions</a></div><div class="TOC-section2"><a href="section-2.2.html#quoting">2.2.4 Quoting</a></div></div><p class="para">To run a Unix command (or any program, for that matter), you normally
must type the name of the command/program file followed by any
arguments.  There is actually a program running that accepts your
keystrokes and launches the appropriate program. The program that
reads and interprets your keystrokes is called the <span class="emphasis">shell</span>.
There are many shells available, all of which offer different
features. The default shell for ODU CS is called tcsh, and we'll
concentrate on that.</p><p class="para">The command/program name is usually not given as a full
file name. Instead, certain directories, such as <span class="code">/bin</span>, are
automatically searched for a program of the appropriate name. This set of directories is referred to as your <span class="quote">
      &#8220;execution path&#8221;
    </span>
New accounts are set up so that the directories holding the most commonly used  Unix commands and programs are already in the execution path.<a onmouseover="DBshowBriefly('fn_fref9_live')" name="fn_fref9" id="fn_fref9" class="footnotemark" href="section-2.2.html#fn_fnote9">9</a><div class="footnote_live" id="fn_fref9_live">You can see your path by giving the command
<div class="programlisting">
echo $PATH
</div>
And you can <a href="../custom/">modify your <span class="code">$PATH</span></a>, if desired, to add additional directories.
</div>
Thus,
one can invoke the <span class="code">ls</span> command as
<div class="screen"><span class="userinput">
   /bin/ls
</span></div>
but it's usually simpler to say
<div class="screen"><span class="userinput">
   ls
</span></div></p><div class="sect3" id="paths"><div class="info_sect3"><div class="title_sect4">2.2.1. Command Arguments</div></div>
    
    <p class="para">Of course, most Unix commands consist not only of the program name but also require one or more command arguments. The command arguments indicate the details of the desired operation, including files to work with, text to use, etc.
</p>
    <p class="para">Command arguments tend to come in four varieties:
<div class="orderedlist"><ol><li>File names: If a command needs to operate on one or more files,
then we specify those files by giving a <span class="firstterm">path</span> to the file -
a sequence of directories that we can follow to reach the file, ending
with the file name itself.
<p class="para">A path may be <span class="firstterm">absolute</span>, meaning that it begins with the
root directory <span class="code">/</span> or with someone's home directory
(starting with <span class="code">~</span>). Alternatively, a path can be
<span class="firstterm">relative</span>, meaning that it is interpreted in terms of your
current working directory.
</p>

<div class="informalexample"><div class="title_example">Try This:</div><p class="para">Log in and give the command
<div class="programlisting">
cd ~/playing
</div>
</p><p class="para">Now your current
working directory should be <span class="code">~/playing</span> and one of the files
in that directory should be <span class="code">math.h</span>. If you wanted to
refer to that file in a command argument, you could do so using of relative paths or absolute paths.
</p><p class="para">Give the commands
<div class="programlisting">
ls -l /home/<span class="emphasis">yourLoginName</span>/playing/math.h
ls -l ~/playing/math.h
ls -l ~<span class="emphasis">yourLoginName</span>/playing/math.h
ls -l math.h
ls -l ./math.h
ls -l ../playing/math.h
</div>
The first three are absolute paths. The last two are relative paths. Note that they all refer to the same file.<a onmouseover="DBshowBriefly('fn_fref10_live')" name="fn_fref10" id="fn_fref10" class="footnotemark" href="section-2.2.html#fn_fnote10">10</a><div class="footnote_live" id="fn_fref10_live">In fact, there are an infinite
number of relative paths for most files, though most are pretty silly,
e.g., <span class="code">../playing/../playing/./././math.h</span>.</div></p><p class="para">If you aren't convinced that all of the above referred you to the same file, repeat the above commands using <span class="code">more</span> instead ot <span class="code">ls -l</span>.</p></div>


<p class="para">
If you change your working directory, e.g., 
<div class="programlisting">
cd ~
</div>
then you can refer to that same file using the same absolute paths
<span class="code">/home/<span class="emphasis">yourLoginName</span>/playing/math.h</span>,
<span class="code">~/playing/math.h</span>, or
<span class="code">~<span class="emphasis">yourLoginName</span>/playing/math.h</span> because
absolute paths do not depend on your current directory. You can also
use relative paths, but these would be different than before because
your current directory is different:  e.g.,
<span class="code">playing/math.h</span>.
</p>

<div class="informalexample"><div class="title_example">Try This:</div><p class="para">
<div class="programlisting">cd ~
ls -l ~/playing/math.h
ls -l math.h
ls -l ../playing/math.h
ls -l playing/math.h
</div>
The first <span class="code">ls</span> still works because it uses an absolute path. Absolute paths are unaffected by your current working directory. The next two <span class="code">ls</span> commands fail, because they were relative paths that made sense when your current working directory was <span class="filename">~/playing</span>, but not when your current working directory is <span class="filename">~</span>. The last <span class="code">ls</span> works as it uses a relative path that makes sense from your current working directory. (<span class="emphasis">Why?</span> Use <span class="code">ls</span> to explore your current directory and make sure you understand why some of these relative paths work and others fail.)
</p></div>


</li><li>
  <p class="para">
    Directory names: these are also specified using absolute and
relative paths. In fact, a directory in Unix is also a file - it's
just a file whose special contents include a list of other files. This
means that most Unix commands that expect a file can be given a
directory as well. But in many cases that won't do what we want.
</p>

<div class="informalexample"><div class="title_example">Try This:</div><div style="clear:right;"/><table class="sidebar" border="1" width="40%"><tr><td>
<p class="para">
The <span class="code">more</span> command lists the contents of a file, a page at
a time. Use the space bar to move forward a page, 'b' to move back a page, and use 'q' to quit.
</p>
</td></tr></table><p class="para">
<div class="programlisting">cd ~
more playing/math.h
more playing
</div>

As you can see, trying to list the contents of a directory file is
not useful.
</p></div>



<p class="para">
There are a few commands that work only on
directories. <span class="code">mkdir</span> (create an empty directory) and
<span class="code">rmdir</span> (remove a directory, if empty) are the most common.
</p>
<p class="para">In other cases, commands may work with either files or directories,
but have slightly different behaviors in the two cases. The
<span class="code">cp</span> command is a good example of this. 

<div class="informalexample"><div class="title_example">Try This:</div><div class="programlisting">cd ~/playing
cp yesno.h g1
ls
cp math.h g1
ls
rm g1
mkdir g1
cp math.h g1
ls
ls g1
</div><p class="para">
The three <span class="code">cp</span> commands are almost identical. In the first
case, we copied a file to a path that did not indicate an existing
file (<span class="code">g1</span>). So <span class="code">cp</span> created a copy with that
name. The second <span class="code">cp</span> copies to a path (<span class="code">g1</span>)
that now indicates an existing file. <span class="code">cp</span> removes that
existing file and then creates a new copy with that name. The third
<span class="code">cp</span>, however, names an existing file that happens to be a
directory. In this case, <span class="code">cp</span> does not replace
<span class="code">g1</span> but writes a copy (named <span class="code">math.h</span>)
inside the directory <span class="code">g1</span>.
</p></div>
</p>

</li><li>Text: some commands receive text as an argument. The simplest example is <span class="code">echo</span>, which simply prints its arguments:

<div class="informalexample"><div class="title_example">Try This:</div><div class="programlisting">echo Hello world
echo I am $user
</div></div>


<p class="para">A more interesting example is the <span class="code">grep</span> command, which
searches files for lines containing desired text. For example,
<div class="informalexample"><div class="title_example">Try This:</div><p class="para">
<div class="programlisting">more /usr/include/math.h
grep cos /usr/include/math.h
</div>
The <span class="code">grep</span> command lists all lines inside <span class="filename">/usr/include/math.h</span> containing
the string <span class="quote">
      &#8220;cos&#8221;
    </span>. We'll shortly see ways to write more complex search patterns.
</p></div>
</p>


</li><li>
<p class="para">
Flags: these are special arguments used to alter or control the behavior of
a command. Flags for Unix commands are usually written beginning with
a <span class="quote">
      &#8220;-&#8221;
    </span> or occasionally <span class="quote">
      &#8220;--&#8221;
    </span>. 
<div class="informalexample"><div class="title_example">Try This:</div><p class="para">
<div class="programlisting">ls ~
ls -a ~
ls /usr/include
ls -l /usr/include
</div>
to see the difference in behavior caused by the <span class="code">-l</span> flag.
</p></div>
</p>
<p class="para">
How do you know what kind of arguments can be accepted by each
command? You pretty much have to deal with this on a individual
basis, though for any specific command you can consult the on-line
manual for that command via <span class="code">man</span>, e.g.
<div class="programlisting">
man ls
man grep
</div>
</p>
</li></ol></div>
</p>
  <div name="fn_fnote10" id="fn_fnote10" class="footnote">10. In fact, there are an infinite
number of relative paths for most files, though most are pretty silly,
e.g., <span class="code">../playing/../playing/./././math.h</span>. (<a href="section-2.2.html#fn_fref10">back</a>)
    </div></div><div class="sect3" id="specialChar"><div class="info_sect3"><div class="title_sect4">2.2.2. Special Characters</div></div>
    
    <p class="para">As you type, some characters have a special meaning. For example, if
you have entered the first few letters of a file name and hit the 
<span class="quote">
      &#8220;Tab&#8221;
    </span> key, the shell will examine what you have typed so far and
attempt to complete the filename by filling in the remaining
characters. If the shell is unable to complete the filename, a bell or
beep sound will be given. Even in this case, the shell will fill in as
many characters as it can.</p>
    <p class="para">Most special characters are entered by holding down the <span class="quote">
      &#8220;Control&#8221;
    </span>
key while typing a letter. By convention, we designate this by placing
the symbol <span class="quote">
      &#8220;^&#8221;
    </span> in front of the name of the second key. For
example, if you have typed zero or more letters of a filename and want
to see a list of what filenames begin with what you have typed, you
could type ^D, i.e., hold down the <span class="quote">
      &#8220;Control&#8221;
    </span> key and type
<span class="quote">
      &#8220;d&#8221;
    </span>.</p>
    <p class="para">Some other useful special keys are:
<div class="itemizedlist"><ul><li>^C is used to abort a program/command that is running too
long or working incorrectly. Beware: aborting a program that is
updating a file may leave garbage in that file.</li><li>^D is used when a command/program is reading many lines of
input from the keyboard and you want to signal the end of the input.</li><li>^H, the <span class="quote">
      &#8220;Backspace&#8221;
    </span> key, and the <span class="quote">
      &#8220;Delete&#8221;
    </span> key all
delete the most recently typed character.</li><li>^B moves the cursor <span class="emphasis-bold" role="bold">B</span>ackwards over what you have
just typed, without deleting those characters. This is useful in 
correcting typing mistakes.
The <span class="quote">
      &#8220;left&#8221;
    </span> arrow on your keyboard may also do the same thing.</li><li>^F moves the cursor <span class="emphasis-bold" role="bold">F</span>orwards over what you have
just typed, without deleting those characters. 
The <span class="quote">
      &#8220;right&#8221;
    </span> arrow on your keyboard may also do the same thing.</li><li>^P retrieves the <span class="emphasis-bold" role="bold">P</span>revious command that you had typed
in. Repeated ^P's may be used to look back through a number of
commands that you have issued recently.

<p class="para">The <span class="quote">
      &#8220;up&#8221;
    </span> arrow on your keyboard may also do the same thing.</p></li><li>^N is the opposite of ^P. After one or more ^
P's, a ^N allows you to move back to the <span class="emphasis-bold" role="bold">N</span>ext more recent
command.

<p class="para">The <span class="quote">
      &#8220;down&#8221;
    </span> arrow on your keyboard may also do the same thing.</p></li><li>In many programs, ^Z pauses the program and returns you
temporarily to the shell. To return to the paused program, give the
command: <span class="code">fg</span></li></ul></div></p>
  </div><div class="sect3" id="cmdpatterns"><div class="info_sect3"><div class="title_sect4">2.2.3. One Pattern, Many Instances</div></div><div class="TOC">Contents:<div class="TOC-section3"><a href="section-2.2.html#wildcards">2.2.3.1 Wild Cards</a></div><div class="TOC-section3"><a href="section-2.2.html#regexp">2.2.3.2 Regular Expressions</a></div><div class="TOC-section4"><a href="section-2.2.html#grepregexps">2.2.3.2.1 Regular Expressions and <span class="code">grep</span></a></div><div class="TOC-section4"><a href="section-2.2.html#sedregexps">2.2.3.2.2 Regular Expressions and <span class="code">sed</span></a></div></div>
    
    <p class="para">
In the command examples we have used so far, we have always written a
single file path or a single text string. In many cases, however, we
want to supply commands with a whole list of files or text
strings. Typing out the whole list, one at a time, would be tedious,<a onmouseover="DBshowBriefly('fn_fref11_live')" name="fn_fref11" id="fn_fref11" class="footnotemark" href="section-2.2.html#fn_fnote11">11</a><div class="footnote_live" id="fn_fref11_live"><p class="para">Don't forget, though, that you can use the <a url="#specialChar" href="section-2.2.html#specialChar">tab</a> character to speed up typing of individual file names.</p></div>
so we usually write some kind of pattern that describes multiple items
instead.
</p>
    <div class="sect4" id="wildcards"><div class="info_sect4"><div class="title_sect5">2.2.3.1. Wild Cards</div></div>
      

<p class="para">Whenever we have a command that can take multiple filenames, we can often write a single pattern for several files. Patterns for file names use <span class="firstterm">wildcard</span> characters, the most common of which is <span class="quote">
      &#8220;*&#8221;
    </span>, which tells the shell<a onmouseover="DBshowBriefly('fn_fref12_live')" name="fn_fref12" id="fn_fref12" class="footnotemark" href="section-2.2.html#fn_fnote12">12</a><div class="footnote_live" id="fn_fref12_live"><p class="para">The program that reads your keyboard input, determines what command or program you want to run, then launches that program.</p></div> to substitute any combination of zero or
more characters that results in an existing filename.
</p>
      <p class="para">
<div class="informalexample"><div class="title_example">Try This:</div><p class="para">
<div class="programlisting">  ls ~/playing
  rm ~/playing/*
  ls ~/playing
</div>
What files were matched by the wildcard pattern in the <span class="code">rm</span> command?
</p><p class="para">
<div class="programlisting">  ls /usr/include
</div>
             Notice that there are a number
                                of files ending with .h
<div class="programlisting">  cp /usr/include/s*.h ~/playing
  ls ~/playing
</div>
Again, note the use of the wildcard to form a pattern for multiple file names.
  In cases, like this, where
there are multiple possibile matched, the shell forms a
list of all the matches. So the <span class="code">rm</span> command actually saw a
list of all the files in the <span class="code">~/playing</span> directory, and the
<span class="code">cp</span> command saw all the files in the 
in the <span class="code">/usr/include</span> directory whose
names began with <span class="quote">
      &#8220;s&#8221;
    </span> and
ended with <span class="quote">
      &#8220;.h&#8221;
    </span>.</p></div>
      </p>

      <p class="para">One good way to figure out what files will match a wildcard pattern is to use the <span class="code">echo</span> command. <span class="code">echo</span> simply prints out its arguments. But since the arguments in the command line  are  processed by the shell <span class="emphasis">before</span> invoking the <span class="code">echo</span> program, any wildcard patterns will have already been expanded.
<div class="informalexample"><div class="title_example">Try This:</div><p class="para">

<div class="programlisting">   ls /usr/include
   echo /usr/include
   echo /usr/include/*.*
   echo /usr/include/*
</div>
The difference between the last two may be subtle. The <span class="quite">*.*</span> pattern will match only files that contain a <span class="quote">
      &#8220;.&#8221;
    </span>. Unlike Windows, Unix does not require file names to end with a period and a three-letter extension. Some sort of period and extension is common, but directory names and executable programs often have no extension and no period. (In Windows, you can create a file with an empty extension, but Windows insists on adding a period at the end.)
<div class="programlisting">
   echo /usr/include/f*.*
   echo /usr/include/*f*.*
</div>
</p></div>
</p>
    <div name="fn_fnote12" id="fn_fnote12" class="footnote">12. <p class="para">The program that reads your keyboard input, determines what command or program you want to run, then launches that program.</p> (<a href="section-2.2.html#fn_fref12">back</a>)
    </div></div>
    <div class="sect4" id="regexp"><div class="info_sect4"><div class="title_sect5">2.2.3.2. Regular Expressions</div></div><div class="TOC">Contents:<div class="TOC-section4"><a href="section-2.2.html#grepregexps">2.2.3.2.1 Regular Expressions and <span class="code">grep</span></a></div><div class="TOC-section4"><a href="section-2.2.html#sedregexps">2.2.3.2.2 Regular Expressions and <span class="code">sed</span></a></div></div>
      
      <p class="para">
If wildcards provide a way to write patterns for file and directory
paths, can we also write patterns for text strings? Yes, but this is
not built into the shell for use by every command, the way that
wildcards are. Instead, most Unix programs and commands that do some
kind of searching or matching for text will
share a common notation for patterns of text to be matched. This
notation is called <span class="firstterm">regular expressions</span>. For example, almost
every text editor in any operating system will allow you to search a
file for a given string. But most Unix text editors (including the
<span class="code">emacs</span> editor we'll study <a href="../emacs/">later</a>) will allow you to
search for any string matching a regular expression
<span class="quote">
      &#8220;pattern&#8221;
    </span>. <span class="code">sed</span>, a useful utility for doing
simple changes to text files, is most often invoked to use its
<span class="quote">
      &#8220;substitute&#8221;
    </span> command, which replaces any text matching a
regular expression by some desired replacement text. The
<span class="code">csplit</span> command splits a single file into multiple pieces, where
the point of division is most often indicated via a regular
expression.  <span class="code">Perl</span> and <span class="code">awk</span>, available on most
Unix systems but not covered in this course, are scripting
(programming) languages with a heavy emphasis on text manipulation,
which is accomplished largely through matching on regular expressions.
</p>

<div class="sect5" id="grepregexps"><div class="info_sect5"><div class="title_sect6">Regular Expressions and <span class="code">grep</span></div></div>
  
      <p class="para">
In an earlier example, we saw that the program <span class="code">grep</span> can
be used to list all lines of a file that match a given string. For
example,
<div class="programlisting">  grep 'cos' /usr/include/math.h
</div>
would list all lines in the indicated file that contain the string
<span class="quote">
      &#8220;cos&#8221;
    </span>. 
</p>
      <p class="para">
expression. The above example works because of the way that regular
expressions are composed. The first rules of regular expressions are:

<div class="itemizedlist"><ul><li>
    <p class="para">
      <span class="emphasis-italic" role="italic">A regular expression consisting of a single
      <span class="quote">
      &#8220;non-special&#8221;
    </span> character will match any string
      containing that character.</span>
    </p>
    <p class="para">
      As it happens, none of the alphabetic and numeric characters
      are <span class="quote">
      &#8220;special&#8221;
    </span>, so the regular
      expression <span class="code">c</span>
      would match any string containing a <span class="quote">
      &#8220;c&#8221;
    </span>. 
      <div class="informalexample"><div class="title_example">Try This:</div><p class="para">
	  <div class="programlisting">grep c /usr/include/math.h
</div>

          Since <span class="code">grep</span> works line by line, this 
	  would select every line containing a <span class="quote">
      &#8220;c&#8221;
    </span>.
	</p></div>
    </p>
  </li><li>
    <span class="emphasis-italic" role="italic">If a set of regular expressions
      <span class="inlineequation"><span class="mathphrase">r<sub>1</sub></span></span>,
      <span class="inlineequation"><span class="mathphrase">r<sub>2</sub></span></span>, &#8230;,
      <span class="inlineequation"><span class="mathphrase">r<sub>k</sub></span></span> are concatenated together to
      form a single larger regular expression
      <span class="inlineequation"><span class="mathphrase">r<sub>1</sub>r<sub>2</sub>&#8230;r<sub>k</sub></span></span>,
      it matches any string that contains a substring formed from a
      concatenation of strings  
      <span class="inlineequation"><span class="mathphrase">s<sub>1</sub>s<sub>2</sub>&#8230;s<sub>k</sub></span></span>,
       each of which matches the corresponding regular expression.</span><p class="para">So when we write
<div class="programlisting">  grep cos /usr/include/math.h
</div>
         the <span class="code">cos</span> is actually the concatenation of three
         regular expressions <span class="code">c</span>, <span class="code">o</span>, and
         <span class="code">s</span>, and matches any string that contains a
         substring matching <span class="code">c</span> followed immediately by a
         substring <span class="code">o</span> followed immediately by a substring 
         matching <span class="code">s</span>.
      </p><p class="para">That may seem an unnecessarily complex way to get to the
      original idea of <span class="quote">
      &#8220;matches the string `cos',&#8221;
    </span> but
      this idea of concatenation is a general one that becomes more
      important as we consider other combinations of 
      smaller regular expressions.
      </p>
</li></ul></div>
The real power of regular expressions comes into play when we consider
the various <span class="quote">
      &#8220;special&#8221;
    </span> characters that serve as regular
expression operators.
<div class="itemizedlist"><ul><li>
    <span class="emphasis-italic" role="italic">Regular expressions can be grouped in
    parentheses, written as
    <span class="code">(</span>&#8230;<span class="code">)</span>.</span><p class="para">So these two
    commands do exactly the same  thing:<a onmouseover="DBshowBriefly('fn_fref13_live')" name="fn_fref13" id="fn_fref13" class="footnotemark" href="section-2.2.html#fn_fnote13">13</a><div class="footnote_live" id="fn_fref13_live"><p class="para"><span class="code">egrep</span>
    is an <span class="quote">
      &#8220;extended&#8221;
    </span><span class="code">grep</span> that supports a
    richer set of regular expression operations than does regular
    <span class="code">grep</span>.</p></div>
    <div class="programlisting">  egrep 'cos' /usr/include/math.h
  egrep '(cos)' /usr/include/math.h
</div>
    </p>
    <div class="informalexample"><div class="title_example">Try This:</div><div class="programlisting">grep '(' /usr/include/math.h
egrep '(' /usr/include/math.h
egrep '\(' /usr/include/math.h
</div><p class="para">
      <span class="quote">
      &#8220;(&#8221;
    </span> is special to <span class="code">egrep</span>, but not to
      <span class="code">grep</span>. To take away the <span class="quote">
      &#8220;special-ness&#8221;
    </span>, we
      can quote the parenthesis character when presenting it to
      <span class="code">egrep</span>. 
    </p></div>
  </li><li>
    <p class="para">
      <span class="emphasis-italic" role="italic"><span class="code">[</span>&#8230;<span class="code">]</span>
      brackets containing any set of characters not beginning with ^
      will match a string containing any one of those
      characters.</span>
    </p>
    <p class="para">
      <div class="informalexample"><div class="title_example">Try This:</div><div class="programlisting">  grep sqrt /usr/include/math.h
  grep '[sS][qQ][rR][tT]' /usr/include/math.h
</div><p class="para">
	  The regular expression <span class="code">[sS]</span> matches any single
	  character that is either <span class="quote">
      &#8220;s&#8221;
    </span> or
	  <span class="quote">
      &#8220;S&#8221;
    </span>, and so on.<a onmouseover="DBshowBriefly('fn_fref14_live')" name="fn_fref14" id="fn_fref14" class="footnotemark" href="section-2.2.html#fn_fnote14">14</a><div class="footnote_live" id="fn_fref14_live"><p class="para">Actually, doing
	  case-insensitive matches is so common that <span class="code">grep</span>
	  provides a shortcut. The <span class="code">-i</span> flag makes all
	  character matches case insensitive, so
	  <div class="programlisting">  grep -i 'sqrt' /usr/include/math.h
  grep '[sS][qQ][rR][tT]' /usr/include/math.h
</div>
           are identical.</p></div>
	</p></div>
    </p>
    <p class="para">
      <div class="informalexample"><div class="title_example">Try This:</div><p class="para">
	  Compare the output from
<div class="programlisting">  grep '(' /usr/include/math.h
  grep ' (' /usr/include/math.h
  grep '[ (]' /usr/include/math.h
  grep '((' /usr/include/math.h
  grep '[ (](' /usr/include/math.h
</div>
	</p></div>
    </p>
    <p class="para">
      The character set inside a regular expression square brackets can
      be abbreviated by giving a range of characters separated by a
      hyphen. For example, <span class="code">[a-z]</span> would match all the
      lower-case alphabetic characters.
      <div class="informalexample"><div class="title_example">Try This:</div><p class="para">
	    What lines are <span class="emphasis">not</span> listed by the following?
<div class="programlisting">
  grep '_[a-zA-Z]' /usr/include/math.h
</div>
If you can't figure it out, you can check with the command
<div class="programlisting">
  grep -v '_[a-zA-Z]' /usr/include/math.h
</div>
The <span class="code">-v</span> option causes <span class="code">grep</span> to list only those lines that do <span class="emphasis">not</span> match the pattern.
	</p></div>
    </p>
  </li><li>
    <p class="para">
      <span class="emphasis-italic" role="italic"><span class="code">[</span>&#8230;<span class="code">]</span>
      brackets containing any set of characters beginning with ^ will
      match a string containing any character <span class="emphasis">not</span>
      in that set.</span>
    </p>
    <p class="para">
      <div class="informalexample"><div class="title_example">Try This:</div><div class="programlisting">  grep 'cos' /usr/include/math.h
  grep '[^a]cos' /usr/include/math.h
</div></div>
    </p>
  </li><li>
    <p class="para">
      <span class="emphasis-italic" role="italic"><span class="code">.</span> matches any single 
      printable character, including blanks, but not including the
      end-of-line character.</span>
    </p>

    <p class="para">
      <div class="informalexample"><div class="title_example">Try This:</div><div class="programlisting">  grep 'd.f' /usr/include/math.h
</div></div>
    </p>
  </li><li>
    <p class="para">
      <span class="emphasis-italic" role="italic">
	If <span class="inlineequation"><span class="mathphrase">r</span></span>
	is a regular expression, then
	<span class="code"><span class="inlineequation"><span class="mathphrase">r</span></span>*</span> 
	matches zero or more successive strings, each of which matches 
	<span class="inlineequation"><span class="mathphrase">r</span></span>.</span>
    </p>
    <p class="para">
      <div class="informalexample"><div class="title_example">Try This:</div><div class="programlisting">  grep 'ER*N' /usr/include/math.h
  grep '#.*if' /usr/include/math.h
</div><p class="para">
	  Note that the behavior of <span class="code">*</span> is very different
	  in regular expressions than it is in file path wildcards.
	</p></div>
    </p>
  </li><li>
    <p class="para">
      <span class="emphasis-italic" role="italic">If
      <span class="inlineequation"><span class="mathphrase">r</span></span> is a
      regular expression, then
      <span class="code"><span class="inlineequation"><span class="mathphrase">r</span></span>+</span>
      matches one or more successive strings, each of which matches
      <span class="inlineequation"><span class="mathphrase">r</span></span>.</span>
    </p>
    <div class="informalexample"><div class="title_example">Try This:</div><p class="para">
	<div class="programlisting">  grep 'ER*_' /usr/include/math.h
  egrep 'ER+_' /usr/include/math.h
</div>
      </p></div>
  </li><li>
    <p class="para">
      <span class="emphasis-italic" role="italic">If
      <span class="inlineequation"><span class="mathphrase">r</span></span> is a
      regular expression, then
      <span class="code"><span class="inlineequation"><span class="mathphrase">r</span></span>?</span>
      matches zero or one strings matching 
      <span class="inlineequation"><span class="mathphrase">r</span></span>.</span>

	<div class="informalexample"><div class="title_example">Try This:</div><div class="programlisting">  egrep 'def' /usr/include/math.h
  egrep 'define' /usr/include/math.h
  egrep 'def(ine)?' /usr/include/math.h
  egrep 'def(ine)?d' /usr/include/math.h
</div><p class="para">
	     In essence, the <span class="code">?</span> makes the preceding item
	     optional. (So the first and third commands in this
	     example are actually equivalent.)
	   </p></div>
      </p>
    </li><li>
      <p class="para">
	<span class="emphasis-italic" role="italic">
	  If
	  <span class="inlineequation"><span class="mathphrase">r</span></span>
	  is a regular expression, then
	  <span class="code">^<span class="inlineequation"><span class="mathphrase">r</span></span></span>
	  matches any string that begins with a substring matching
	  <span class="inlineequation"><span class="mathphrase">r</span></span>.
	</span>
      </p>

      <div class="informalexample"><div class="title_example">Try This:</div><div class="programlisting">  grep 'ex' /usr/include/math.h
  grep '^ex' /usr/include/math.h
</div></div>
    </li><li>
      <p class="para">
	<span class="emphasis-italic" role="italic">
	  If <span class="inlineequation"><span class="mathphrase">r</span></span>
	  is a regular expression, then
	  <span class="code"><span class="inlineequation"><span class="mathphrase">r</span></span>$</span>
	  matches any string that ends with a substring matching
	  <span class="inlineequation"><span class="mathphrase">r</span></span>.
	</span>
      </p>

      <div class="informalexample"><div class="title_example">Try This:</div><div class="programlisting">  grep '_' /usr/include/math.h
  grep '_$' /usr/include/math.h
</div></div>
    </li></ul></div>
  With so many special characters, you might wonder just what you're
  supposed to do if you really want to search for lines containing a
  <span class="quote">
      &#8220;*&#8221;
    </span>, or a <span class="quote">
      &#8220;?&#8221;
    </span>, or a &#8230; The answer is
  given in our final rule:
  <div class="itemizedlist"><ul><li>
      <p class="para">
	<span class="emphasis-italic" role="italic">
	  If <span class="code">c</span> is a special character, then
	  <span class="code">\c</span> matches that character in a string.
	</span>
      </p>
      <div class="informalexample"><div class="title_example">Try This:</div><div class="programlisting">  grep '3.' /usr/include/math.h
  grep '3\.' /usr/include/math.h
</div></div>
    </li></ul></div>
</p>
      <p class="para">
	This is by no means an exhaustive list of all the regular expression
	operations, but it's probably enough for most purposes. The textbook
	has some additional information, and you can get an exhaustive
	description via the command
	<div class="programlisting">man regexp
</div>
      </p>
    <div name="fn_fnote13" id="fn_fnote13" class="footnote">13. <p class="para"><span class="code">egrep</span>
    is an <span class="quote">
      &#8220;extended&#8221;
    </span><span class="code">grep</span> that supports a
    richer set of regular expression operations than does regular
    <span class="code">grep</span>.</p> (<a href="section-2.2.html#fn_fref13">back</a>)
    </div><div name="fn_fnote14" id="fn_fnote14" class="footnote">14. <p class="para">Actually, doing
	  case-insensitive matches is so common that <span class="code">grep</span>
	  provides a shortcut. The <span class="code">-i</span> flag makes all
	  character matches case insensitive, so
	  <div class="programlisting">  grep -i 'sqrt' /usr/include/math.h
  grep '[sS][qQ][rR][tT]' /usr/include/math.h
</div>
           are identical.</p> (<a href="section-2.2.html#fn_fref14">back</a>)
    </div></div>

    <div class="sect5" id="sedregexps"><div class="info_sect5"><div class="title_sect6">Regular Expressions and <span class="code">sed</span></div></div>

<p class="para">As noted earlier, many other commands besides <span class="code">grep</span>
will use regular expressions. <span class="code">sed</span>, for example, allows
you to enter a variety of editing commands that will be applied to
every line of a file. A common use of <span class="code">sed</span> is to scan each line of the file for a pattern and to replace that pattern, wherever it occurs, by some string. The <span class="code">sed</span> command to do this is
<div class="programlisting">  sed s/<span class="emphasis" mode="italic">pattern</span>/<span class="emphasis" mode="italic">replacement</span>/g <span class="emphasis" mode="italic">filename</span>
</div>
where <span class="emphasis" mode="italic">filename</span> is the file whose
contents we want to scan and replace, <span class="emphasis" mode="italic">pattern</span> is a regular expression describing
the text to search for in each line, and <span class="emphasis" mode="italic">replacement</span> is the text by which we wish to
replace any thing that matches the pattern.<a onmouseover="DBshowBriefly('fn_fref15_live')" name="fn_fref15" id="fn_fref15" class="footnotemark" href="section-2.2.html#fn_fnote15">15</a><div class="footnote_live" id="fn_fref15_live">The '/'
characters are simply necessary to indicate the beginning and end of
the pattern and replacement strings. They can be replaced by any
character that does not appear in either the pattern or replacement
strings.</div>
</p>

<p class="para">
  <div class="informalexample"><div class="title_example">Try This:</div><div class="programlisting">  cd ~/playing
  cp ~cs252/Assignments/ftpAsst/alas.txt .
  more alas.txt
</div><p class="para">
      Now try operating on that file with <span class="code">sed</span>:
    </p><div class="programlisting">  sed s/e/Q/g alas.txt
</div><p class="para">
      for a simple text-to-text substitution, and
    </p><div class="programlisting">
  sed 's/[aeiou]/X/g' alas.txt
</div><p class="para">
      to see that the pattern really is interpreted as a regular expression.<a onmouseover="DBshowBriefly('fn_fref16_live')" name="fn_fref16" id="fn_fref16" class="footnotemark" href="section-2.2.html#fn_fnote16">16</a><div class="footnote_live" id="fn_fref16_live"><p class="para">The quotes are required because, as explained in the next section, the '[' and ']' characters are normally considered special characters by the shell, and the quotes tell the shell to leave those characters alone, so that <span class="code">sed</span> can actually see them.</p></div>
    </p></div>
    <p class="para">
      The 'g' at the end of each of the prior examples indicates that
      the change should be applied <span class="emphasis">every</span> time a
      match is found. If the 'g' is dropped, only the first match in
      each line will be replaced.  
    </p>
      <div class="informalexample"><div class="title_example">Try This:</div><div class="programlisting">  sed 's/[aeiou]/X/' alas.txt
</div><p class="para">
	  to see the effect of dropping the 'g'.
	</p></div>
    </p>
  <div name="fn_fnote15" id="fn_fnote15" class="footnote">15. The '/'
characters are simply necessary to indicate the beginning and end of
the pattern and replacement strings. They can be replaced by any
character that does not appear in either the pattern or replacement
strings. (<a href="section-2.2.html#fn_fref15">back</a>)
    </div><div name="fn_fnote16" id="fn_fnote16" class="footnote">16. <p class="para">The quotes are required because, as explained in the next section, the '[' and ']' characters are normally considered special characters by the shell, and the quotes tell the shell to leave those characters alone, so that <span class="code">sed</span> can actually see them.</p> (<a href="section-2.2.html#fn_fref16">back</a>)
    </div></div>
    </div>
  <div name="fn_fnote11" id="fn_fnote11" 