Introduction to the Unix Operating System

W
Document Sample
scope of work template
							     Introduction to the Unix
        Operating System
Basic understanding of the Unix
Basic Unix commands
File structures
Directories
File and Directory Permissions
                                  May 2, 2005
        History of Unix
MULTICS (mid 1960s)
UNICS (1969)
First Version (1971)
- Co-authors: Ken
Thompson, Dennis
Ritchie, and Brian
Kernighan
Two branches
- System V vs. BSD
Unix Anatomy
    Basic Unix Elements
Commands
Files
Directories
Environment
Processes
Jobs
    Logging In and Out
Getting the login prompt
Entering your Userid and Password
Logging out
Changing your password
          The Unix Shell
C Shell
  Its syntax and constructs are similar to those in
  the C programming language
  Command prompt: %
  TC shell (tcsh) is C shell with default prompt: >
Bourne Shell
  Named for its author
  Default prompt: $
  The GNU Bourne-Again shell (bash) is the Bourne
  shell with default prompt: bash$
   About Unix Commands
Manipulate your files, data, and environment
The general syntax for a Unix command is:
command [-flag options] file/expression
Several rules of Unix commands
  Case-sensitive, but most are lowercase
  Can only be entered at the shell prompt
  Command lines must end with a RETURN
  Options often begin with a “-” (minus sign)
  More than one option can be included with many
  commands
Redirecting Input and Output
 Output redirection symbol: “>”
   e.g. date > file1 vs. date >> file1
 Input redirection symbol: ”<”
   e.g. program < datafile
 Combine both kinds of redirection
   e.g. program < datafile > outputfile
 Final I/O redirection symbol: “|”
   e.g. date | program
Getting On-line Help with
       Commands
Access electronic reference manuals (known as the
man pages) with the man command:
  man command-name
  man –k keyword
The Unix reference manual is divided into eight
numbered sections
  General User Commands
  System Calls
  User-level Library Functions
  Device Drivers, Protocols
  File Formats
  Games (rarely available)
  Document Preparation
  System Administration
 Setup and Status Commands
COMMAND PURPOSE
logout  end your UNIX session
passwd  change password by prompting for old and new
        passwords
stty    set terminal options
date    display or set the date
finger  display information about users
ps      display information about processes
env     display or change current environment
set     C shell command to set shell variables
alias   C shell command to define command
        abbreviations
history C shell command to display recent commands
File and Directory Commands
COMMAND PURPOSE
cat        concatenate and display file(s)
more       paginator - allows you to browse through a text file
less       more versatile paginator than more
mv         move or rename files
cp         copy files
rm         remove files
ls         list contents of directory
mkdir      make a directory
rmdir      remove a directory
cd         change working directory
pwd        print working directory name
du         summarize disk usage
chmod      change mode (access permissions) of a file or
           directory
file       determine the type of file
quota -v   displays current disk usage for this account
              Editing Tools
COMMAND PURPOSE
pico     simple text editor
diff     show differences between the contents of
         files
grep     search a file for a pattern
sort     sort and collate lines of a file (only works
         on one file at a time)
wc       count lines, words, and characters in a file
look     look up specified words in the system
         dictionary
awk      pattern scanning and processing language
gnuemacs advanced text editor
vi       screen oriented (visual) display editor
Program Controls, Pipes, and
         Filters (I)
COMMAND PURPOSE
CTRL-C  interrupt current process or command
CTRL-D  generate end-of-file character
CTRL-S  stop flow of output to screen
CTRL-Q  resume flow of output to screen
CTRL-Z  suspend current process or command
jobs    lists background jobs
sleep   suspend execution for an interval
kill    terminate a process
nice    run a command at low priority
renice  alter priority of running process
 Program Controls, Pipes, and
         Filters (II)
COMMAND PURPOSE
&       run process in background when placed at
        end of command line
>       redirect the output of a command into a file
>>      redirect and append the output of a
        command to the end of a file
<       redirect a file to the input of a command
>&      redirect standard output and standard error
        of a command into a file (C shell only)
|       pipe the output of one command into another
Other Tools and Applications
COMMAND PURPOSE
pine    electronic mail
dc      desk calculator
man     print UNIX manual page to screen
elm     another electronic mail program
         About Unix Files
Each file has a filename
  a filename can be up to 256 characters long,
  consisting of any alphanumeric character on the
  keyboard except the "/“
Certain standard extension conventions
  .h for header files
  .c for C source files
  .f for FORTRAN
  .p for PASCAL
  .s for assembler source files
          Creating Files
Using a text editor, such as vi, GNU Emacs,
Pico, or aXe
Using the cat command (short for
concatenate) and the ">" (redirect output)
symbol
  cat > new-filename
  When you reach the end of each line, press the
  RETURN key
  When you have completed the last line, press
  RETURN and type CTRL-D
          Displaying Files
Using the cat command
  cat filename
  Control the flow of text by using CTRL-S (stops the flow of
  text) and CTRL-Q (restarts it)
Using a program named more
  more filename
  Displays only one screen of information at a time and then
  wait for you to press the space bar before it displays the
  next page of text, until you reach the end of the file
Using a utility called less
  It is invoked the same way as more
  It allows reverse scrolling of files and other enhancements
              Listing Files
The ls command will list the files in the
current directory that do not begin with a
period
  ls   -a
  ls   -l
  ls   -s
  ls   | more (or less)
           Copying Files
To make a copy of a file, use the cp (copy)
command
  cp filename newfilename
  Note: if newfilename already exists, the cp
  command will overwrite the previous contents
  cp -i filename newfilename
  Use the -i option (cp -i) to get the confirmation
  prompt before it overwrites an existing file
         Renaming Files
To rename a file, use the mv (move)
command
  mv oldfilename newfilename
  Note: moving a file into an existing file overwrites
  the data in the existing file
  mv -i oldfilename newfilename
  Use the -i option (mv -i) to get the confirmation
  prompt before it overwrites an existing file
          Deleting Files
To delete a file, use the rm (remove)
command
  rm filename
  Important: rm can be very dangerous. Once a
  file has been removed you cannot get it back,
  except, possibly, from system backups (which may
  or may not contain the file)
  rm -i oldfilename newfilename
  Use the -i option to get the confirmation prompt
  before it removes a file
 Some useful commands
ls –trl
find . –name filename –print
grep
tail –f logfile
Creating Links between Files
 The ln command creates a link, which "points" to the
 file
    ln does not rename the file (as does mv) nor does it make a
    copy of the file (as does cp)
    It allows you to access the file from multiple directories
 To create a symbolic link to a file within the same
 directory
    ln -s originalFile linkName
 To create a link in a directory other than that of the
 original file
    ln -s originalFile differentDirectoryName/linkName
   About Unix Directories
All files and directories in
the UNIX system are
stored in a hierarchical
tree structure
root directory
a set of major
subdirectories such as
bin, dev, etc, lib, pub,
tmp, and usr
Your home directory
   Displaying Directories
The pwd command will display the full
pathname of the current directory you are in
  pwd
  /home/userid
To see every file and directory in the current
directory
  ls -a
To display the contents of your home
directory
  ls /home/userid
  ls ~ (using a shell other than the Bourne shell)
    Changing Directories
To change your current directory to another directory
in the directory tree, use the cd command
For example, to move from your home directory to
your protein directory
  cd protein (relative pathname from home directory)
  cd ~/protein (full pathname using ~)
  cd /home/userid /protein (full pathname)
To get back to the parent directory of protein, use
the special ".." directory abbreviation
  cd ..
If you get lost, issuing the cd command without any
arguments will place you in your home directory
       Moving Files Between
            Directories
To move a file into another directory, use the following
syntax for the mv command
   mv source-filename destination-directory
   e.g. mv sample.txt ~/protein
   it will move the file sample.txt into the protein directory
   Note: the mv command is capable of overwriting files, it would
   be prudent to use the -i option (confirmation prompt)
To move a file into another directory and rename it at
the same time
   mv source-filename destination-directory/newfilename
   e.g. mv sample.txt ~/protein/newsample.txt
   it will move the file sample.txt into the protein directory and
   rename it as newsample.txt at the same time
     Copying Files to Other
          Directories
To copy a file to another directory, use the cp command.
The new file will have the same name as the old one
  cp source-filename destination-directory
  e.g. cp sample.txt ~/protein
  it will copy the file sample.txt to the protein directory
To copy a file to another directory and change the name
while copying it
  cp source-filename destination-directory/newfilename
  e.g. cp sample.txt ~/protein/newsample.txt
  it will copy the file sample.txt to the protein directory and
  change the name as newsample.txt while copying it
     Renaming Directories

To rename an existing directory, use the mv
command
  mv oldDirectory newDirectory
The new directory name must NOT exist before
you use the command
The new directory need NOT be in the current
directory
        Removing Directories
To remove a directory, first be sure that you are in the
parent of that directory
Use the command rmdir along with the directory's
name
You cannot remove a directory with rmdir unless all the
files and subdirectories contained in it have been erased
The quickest way to remove a directory and all of its
files and subdirectories (and their contents) is to use the
rm -r (for recursive) command along with the
directory's name
   e.g. rm -r protein (empty and remove your protein directory)
File and Directory Permissions
              (I)
Every file or directory in a UNIX file system has three
types of permissions
   read (r)
   write (w)
   execute (x)
For each file and directory, the read, write, and execute
permissions may be set separately for each of the
following classes of users
   User (u)
   Group (g)
   Others (o)
File and Directory Permissions
              (II)
If you are the owner of a file (or if you are root), then
you can use the chmod (change mode) command to
change the permission of a file
   chmod [who] operator [permissions] filenames
   - who can be u (user, i.e. owner), g (group), o (others), a (all);
   - operator can be + (add a permission), - (remove a
   permission), = (set permission);
   - permission the most frequently used options are: r (read
   permission), w (write permission), x (execute permission)
File and Directory Permissions
             (III)
You can run chmod in absolute mode:
   chmod absolute_mode filename
   absolute_mode is an octal number. It is the sequence of “User's
   Digit,” “Group's Digit,” “Other's Digit.” Each digit can be
   computed by only adding the numbers below the options (r, w,
   x) which are to be set
User's Digit          Group's Digit               Other's Digit
r   w       x      r            w       x         r        w        x
4 + 2 + 1          4 +          2     + 1         4   +    2      + 1
   e.g. chmod 0752 abc
   will change the permissions of file abc as –rwxr-x-w-
You can also recursively change file permissions in a
directory with:
   chmod -R absolute mode directory
File and Directory Permissions
             (IV)
If you own a file/directory, then you can change its
ownership to another user or another group
   To change the ownership of file/dir to user, use the chown
   (change ownership) command
   chown (new owner) (file or directory name)
   To change the default group of file/dir to group, use the chgrp
   command
   chgrp (new group) (file or directory name)
     Formatting and Printing
          Commands
COMMAND PURPOSE
lpq      view printer queue
lpr      send file to printer queue to be printed
lprm     remove job from printer spooling queue
enscript converts text files to POSTSCRIPT format for
         printing
lprloc   locations, names of printers; printout costs
pacinfo  current billing info for this account
               Printing Files
To print a file, use the lpr command
   lpr filename
   lpr [-Pprintername] filename (for laser printers only)
To get a list of the printers available to your machine
   lprloc
To get some status information on the printers
   lpstat –p
To print accounting information
   pacinfo