Seminar on UNIX

Enquiry : Mr.Jason Chan (mwchan@phy.cuhk.edu.hk, Rm348)


Topics


Introduction to UNIX

Objectives

The UNIX Operating System

UNIX Original Philosophy

  1. Portability;
  2. Unification of file, device and interprocess communication;
  3. Ability of creating asynchronous multiple processes;
  4. Hierarchial file system;
  5. Algoriths were selected for simplicity, not for speed or sophistication;
  6. Replaceable and programmable shell command interpreter;
  7. Ability to build complex programs from simpler programs;
  8. Power of UNIX comes from the relations between the programs rather than the programs themselves.

Reasons for its Success

Obstacles

A Brief History

Unix Structure

Kernel

What is a Shell ?

The Shell - User Interface to UNIX

Logging in and out

Command Format

Command Line

    $ command      option(s)     argument(s)
    $ ls           -l            /u

The - always indicates the next character is an option, or flag, for the command

There are exceptions to this command format

Command Format (cont.)

Separation

       right                                wrong
   $ ls  -l  /usr                       $ ls  -l/usr
   $ ls  -s  /bin                       $ ls-s  /bin

Order

        right                                 wrong
    $  cat  -s  file                    $ cat  file  -s
    $  ls  -a                           $  -a  ls

(There are exceptions !)

Command Format (cont.)

Multiple Options

       right                                  wrong
   $ ls  -l  -d  -t                       $ ls  -l-d-t
   $ ls  -ldt                             $ ls -l  d  t

Multiple Arguments

         right                                 wrong
    $  cat   file1  file2             $ cat  file1file2
    $  cat   file1  file2             $ cat  file1, file2

The cal Command

cal month year

Examples

  1. Display a calendar for October 1994 at your work station
  2. Display a calendar for the year 1994

The cat Command

Examples

  1. Display the file file1 at the work station
  2. Concatenate several files

The date Command

Example : Display current date and time,

Super user can change the system date

The echo Command

Example : Write message to standard output

The lp Command

Example : Print the file file1 on system printer

The lprm Command

Example : Remove job number 8 from the default printer queue

The lpstat Command

Examples

  1. Display the status for all print queues
  2. Display the status of the default queue

The mail Command (Sending Mail)

The mail Command (Receiving Mail)

Some useful commands at the ‘&’ prompt

The man Command

Examples

  1. Obtain information about the ls command
  2. Display one line synopsis of each manual section whose listing in the table of contents contains stat

The passwd Command

Example

> passwd [press Enter]

> nhychi’s Old password:
> nhychi’s New password:
> Enter the new password again:

Password will not be echoed on the screen

The wc Command

Options

Examples

  1. Display line, word, & character counts of a file
  2. Display only character and word counts

The whatis Command

Example : whatis ls will produce the following output:
ls(1) -Display the contents of a directory

The who Command

Examples

  1. Display information about who is using the local system node
  2. Display your user name


Exercise 1

  1. Display the system’s date
  2. Count the number of lines in the /etc/motd file
  3. Display the whole calendar for the year 2000
  4. Display the whole calendar for the year 1752
  5. How many users are there in the system ?
  6. Display the contents of the /etc/motd file


Using Files

Objectives

File

File Type

Ordinary

Directory

Special Files

File Name in UNIX

Some Naming Conventions

filename.c

C Source code

filename.h

‘include’ file (header data) for C

filename.f

FORTRAN source code

filename.p

PASCAL source code

filename.s

Assembler source code

filename.o

Object code

.

Current directory

..

Parent directory

Pathnames

Change Directory

Examples

  1. Change to your home directory
  2. Change to other directory
  3. Go down one level of the directory tree
  4. Go up one level of the directory tree cd ..

Create Directory

Example : Create the subdirectory data in /a1/cc/nhychi

      mkdir  /a1/cc/nhychi/data    full path name

        or

      mkdir  data                         relative path name

Print Working Directory

Remove Directory

Example : Empty and remove a directory

Copy Files

Examples

  1. Make another copy of a file in the current directory
  2. Copy to another directory
  3. Copy a directory, its files and its subdirectories to another directory
  4. This copies the directory test, its files subdirectories etc to the directory prod

Display Files

List Files

Examples

  1. List all files in the current directory
  2. Display detail information
  3. List the files in order of modification time

Output Format

(1)          (2) (3)    (4)   (5)    (6)               (7)
drwx------    2   cc   staff   104   Oct 20   09:00    bin
-rwxrwxr      1   cc   staff   542   Oct 18   10:45    prog.sas
field 1 : file or directory, permission bits
field 2 : link count
field 3 : user name of file owner
field 4 : group name for group permissions
field 5 : character count
field 6 : date the file was last written to disk
field 7 : name of file or directory

Move/Rename Files

Examples

  1. Rename the file chapter.1 to chap.1
  2. Move a file chap.1 to subdirectory book
  3. Use the mv command with pattern-matching
  4. This moves all files in the directory book into the current directory

Remove Files

Examples

  1. Delete a file
  2. Delete files one by one
  3. Enter y to delete the file, or press Enter to keep it.

  4. Delete a directory tree
  5. This recursively removes the contents of all subdirectories of book, then removes book itself


File Permissions

Objectives

Output Format

(1)          (2) (3)   (4)     (5)          (6)        (7)
drwx------    2   cc   staff   104   Oct 20   09:00    bin
-rwxrwxr--    1   cc   staff   542   Oct 18   10:45    prog.sas
field 1 : file or directory, permission bits
field 2 : link count
field 3 : user name of file owner
field 4 : group name for group permissions
field 5 : character count
field 6 : date the file was last written to disk
field 7 : name of file or directory

File Protection/Permission

  • r = read; w = write; x = execute
  • rwx rwx rwx
  • user group others

  • Ordinary file
  • Directory
  • For directory, x permission is required to access any of the files and/or subdirectories within it
  • For directory, x permission is necessary for w permission to be effective
  • If you cannot get into the directory(x) then you cannot create or remove files within it(w)
  • Change Mode - Permissions

    Symbolic Format

    Examples

    1. Add write permission for group and other
    2. Give read, write execute to owner but denies group and other the permission to access

    Specify the final permissions, not relative changes, as with the Symbolic Format

    Octal Format - specify permissions as 3 digits

    Example : Allow the owner read, write and execute, read and execute to group and no permission to other

    Function/Permissions Required

    Command

    Source Directory

    Source File

    Target Directory

    cd

    x

    n/a

    n/a

    ls

    r

    n/a

    n/a

    mkdir

    x,w

    n/a

    n/a

    rmdir

    x,w

    n/a

    n/a

    cat,pg,more

    x

    r

    n/a

    mv

    x,w

    none

    x,w

    cp

    x

    r

    x,w

    touch

    x(w)

    n/a

    none

    rm

    x,w

    none

    n/a


    The ‘vi’ Editor

    Objectives

    vi

    Cursor Movement

    From command mode

    h

    left, one space

    j

    down, one line

    k

    up, one line

    l

    right, one space

    ^

    beginning of line

    $

    end of line

    1G

    first line

    G

    last line

    < ctrl > f

    forward one screen

    < ctrl > b

    backward one screen

    < ctrl> d

    down half screen

    < ctrl > u

    up half screen

    From command or text mode

    <esc> get to command mode

    Create a File

    Insert and Delete

    a    - append or add text
             - change the editor to text input mode
             - ‘a’ will not be displayed
    d$  - delete from cursor to end of line
    dd  - delete the entire line
    ndd - delete n lines
    dw  - delete to the end of the word 
    i    - insert text before cursor
    o   - open a new line after cursor
    O  - open a new line before cursor 
    p   - put item last deleted before current line
    P  - put item last deleted after current line
    rx - replace current character with x
    x  - delete 1 character at the cursor position
    

    Save / Exit a File

    < esc >  - change the editor to command
                     mode
    :q!        - exit without saving
    :w         - write the editing buffer to the file
    :wq      - write and exit vi  
    ZZ        - save & quit
    

    String Search

    /the/  - put the cursor on the first 
                   occurrence of the string ‘the’
    ?the?- similar to /the/  but in opposite
                   direction
    n      - search next occurrence of the string
    N      - similar to ‘n’ but in opposite direction
    

    Global Search and Replace

    Example :Replace every ‘ if ‘ with ‘ if and only if ‘

    Undo

    :u  -  allow command to 
               appear on screen
    u   -  command will not
               appear anywhere on
               the screen
     .    - repeat last command
    

    Environment Customization

    .exrc File

    Crash Recovery


    Processes

    Objectives

    What is a Process ?

    Process Characteristics

    Self-Contained

    Monitor Processes

    Examples

    1. List the processes that you have started : ps
    2. Give full information about the processes you are running : ps -f
    3. Display all process information : ps -efl
    4. List processes owned by specific users :

    Control Processes


    Introduction to the Shell

    Objectives

    What is a Shell ?

    The shell-User Interface to UNIX command interpreter programming language

    Redirection - Standard Input

    Example : mail nhychi < letter.1

    Redirection - Standard Output

    Example : ls -lR > list.file

    Example : ls -lR >> list.file

    Redirection - Standard Error

    Example : cat file1 file2 2 > error.file

    Line Continuation

    the ‘/’ is used to continue a command on a separate line

    Command Grouping

    Commands can be entered on one line, separated by ‘;’

    Pipes

    Example : ls | wc -w is similar to

    Pipes and filters

    Example : ps -e | grep cc | wc -l