01 - Basic Unix

Embed Size (px)

Citation preview

  • 7/27/2019 01 - Basic Unix

    1/43

    CSc 352: Basic Unix

  • 7/27/2019 01 - Basic Unix

    2/43

    Reading

    Chapter 1: Upto Background

    Jobs (page 17)

    2

  • 7/27/2019 01 - Basic Unix

    3/43

    What is Unix?

    Unix is an operating syste sits bet!een the hard!are and the

    user"app#ications

    pro$ides high%#e$e# abstractions (e&g&' i#es) and

    ser$ices (e&g&' u#tiprograing)

    inux: a Unix%#ike operating syste: user%#e$e#

    interace $ery sii#ar to Unix

    code base is dierent ro origina# Unix code

    *

  • 7/27/2019 01 - Basic Unix

    4/43

    Layers of a Unix system

    +

    hard!are

    she##

    Unix operating syste kerne#

    users app#ications

    she##coan

    dssyste

    ca##s

  • 7/27/2019 01 - Basic Unix

    5/43

    The file system

    , fileis basica##y ase-uence o bytes

    Co##ections o i#esare grouped intodirectories(o#ders)

    , directory is itse# ai#e

    i#e syste has ahierarchica# structure(i&e&' #ike a tree)o the root is reerred to

    as ".

    /

    ddccbb

    "

    ee

  • 7/27/2019 01 - Basic Unix

    6/43

    !"erything is a file#

    0n Unix' e$erything #ooks #ike a i#e: docuents stored on disk

    directories

    inter%process counication

    net!ork connections de$ices (printers' graphics cards' interacti$e terina#s'

    /)

    hey are accessed in a unior !ay:

    consistent ,0 (e&g&' read' !rite' open' c#ose'/)

    consistent naing schee (e&g&'"hoe"debray' "de$"cdro)

    3

  • 7/27/2019 01 - Basic Unix

    7/43

    Referring to files: $%sol&te'aths

    ,n absolute pathspeciies ho! to get toa i#e starting at thei#e syste root

    #ist the directories onthe path ro the root(")' separated by "

    7

    ddccbb

    "

    ee

    gg

  • 7/27/2019 01 - Basic Unix

    8/43

    Referring to files: $%sol&te'aths

    ,n absolute pathspeciies ho! to get toa i#e starting at thei#e syste root

    #ist the directories onthe path ro the root(")' separated by "

    4

    ddccbb

    "

    ee

    gg

    abso#ute path:

    (dd(ee(gg

  • 7/27/2019 01 - Basic Unix

    9/43

    Referring to )iles: Relati"e'aths

    ypica##y !e ha$e anotion o a currentdirectory

    , relative pathspeciiesho! to get to a i#estarting ro thecurrent directory 5**6 eans o$e up one

    #e$e#

    5*6 eans currentdirectory

    #ist the directories on thepath separated by "

    ddccbb

    "

    ee

    gg

  • 7/27/2019 01 - Basic Unix

    10/43

    Referring to files: Relati"e'aths

    ypica##y !e ha$e anotion o a currentdirectory

    , relative pathspeciiesho! to get to a i#estarting ro thecurrent directory 5**6 eans o$e up one

    #e$e#

    5*6 eans currentdirectory

    #ist the directories on thepath separated by "

    18

    ddccbb

    "

    ee

    gg9xap#e:ffre#ati$e to eeis: **(ff

  • 7/27/2019 01 - Basic Unix

    11/43

    Referring to files: Relati"e'aths

    ypica##y !e ha$e anotion o a currentdirectory

    , relative pathspeciiesho! to get to a i#estarting ro thecurrent directory 5**6 eans o$e up one

    #e$e#

    5*6 eans currentdirectory

    #ist the directories on thepath separated by "

    11

    ddccbb

    "

    ee

    gg9xap#e:ccre#ati$e to eeis: **(**(cc

  • 7/27/2019 01 - Basic Unix

    12/43

    +ome directories

    9ach user has a hoe directory speciied !hen the account is created

    gi$en in the i#e (etc(,ass-d

    hen you #og in' your current directory isyour hoe directory can then start a shelland issue coands

    ;otationa# shorthand: one6s o!n hoe directory: .

    soe other user/oe6s hoe directory: ./oe

    12

    , she## is

  • 7/27/2019 01 - Basic Unix

    13/43

    0n,&t and o&t,&t

    =ata are read ro and !ritten to i"ostreams

    here are three predeined streas:stdin : standard input usua##y' keyboard input

    stdo&t: standard output usua##y' the screen

    stderr: standard error or error essages (usua##y'the screen)

    >ther streas can be created using syste ca##s(e&g&' to read or !rite a speciic i#e)

    1*

  • 7/27/2019 01 - Basic Unix

    14/43

    'rocesses

    rogras are executed $iaprocesses a process is the unit o execution

    consists o: the code that is executed

    the data this code anipu#ates

    =ierent processes execute concurrent#y each process has its o!n address space' stdin'

    stdout' etc&

    their execution is anaged by the operatingsyste

    Coon tasks are carried out using a set

    o syste%pro$ided progras ca##edcommands1+

  • 7/27/2019 01 - Basic Unix

    15/43

    Unix Commands

    9ach coand perors ?$ariations o@ asing#e task options can be used to odiy !hat a coand does

    dierent coands can be g#ued together to peror

    ore cop#ex tasks Ayntax:

    command options arguments

    Examples:

    1.

    Command 1,tions $rg&ments

    p!dcd "hoe"debray

    #s %a %#

    #s %a# "usr"#oca#

    Options can(usually) becombinedtogether:these areequivalent

  • 7/27/2019 01 - Basic Unix

    16/43

    Unix Commands

    9ach coand perors ?$ariations o@ asing#e task options can be used to odiy !hat a coand does

    dierent coands can be g#ued together to peror

    ore cop#ex tasks Ayntax:

    command options arguments

    Examples:

    13

    Command 1,tions $rg&ments

    p!dcd "hoe"debray

    #s %a %#

    #s %a# "usr"#oca#

    Not always required:

    may have defaultvalues

    deau#tstocurrentdirector

    y

    typica# deau#ts:input: stdinoutput: stdoutdirectory: current

  • 7/27/2019 01 - Basic Unix

    17/43

    !xam,les of Unix commands 0

    iguring out one6s current directory: ,-d

    o$ing to another directory: cd targetdir

    Examples:

    17

    cd ( o$e to the root o thei#e syste

    cd .(a#so:

  • 7/27/2019 01 - Basic Unix

    18/43

    !xam,les of Unix commands 00

    Coand: ls lists the contents of adirectory

    9xap#es:

    14

    ls #ist the i#es in the current directorywont show files whose names start

    with .

    ls (&sr(%in #ist the i#es in the directory "usr"bin

    ls l gi$e a #ong orat #isting (pro$idesadditiona# ino about i#es)

    ls a #ist a## i#es in the current directory'

    inc#uding those that start !ith 5&6ls al (&sr(local gi$e a #ong orat #isting o a## the

    i#es (inc#& those starting !ith 5&6) in"usr"#oca#

  • 7/27/2019 01 - Basic Unix

    19/43

    Com%ining commands

    he output o one coand can be ed toanother coand as input&

    Ayntax: command1 D command

    2

    9xap#e:

    1

    pipe

    ls #ists the i#es in a directory

    more foo sho!s the i#e fooone screenu# at a tie

    ls 4 more #ists the i#es in a directory one screenu#at a tie

    +o- this -ors:ls!rites its output to its stdo&tmore6s input strea deau#ts to itsstdinthe pipe connects ls6s stdout to

    more6s stdinthe piped coands run in

  • 7/27/2019 01 - Basic Unix

    20/43

    )inding o&t a%o&t commands 0

    iguring out !hich coand to use a,ro,os !eyword

    man 6 !eywordsearches a set o database i#es containing short

    descriptions o syste coands or key!ords Ee#pu#' but not a panacea:

    depends on appropriate choice o key!ords ay re-uire tria# and error

    ay return a #ot o resu#ts to sit through pipe through more

    28

  • 7/27/2019 01 - Basic Unix

    21/43

    )inding o&t a%o&t commands 00

    iguring out ho! to use a coand man commanddisp#ays the on%#ine anua# pages

    ro$ides inoration about coandoptions' arguents' return $a#ues' bugs'etc&

    21

  • 7/27/2019 01 - Basic Unix

    22/43

    !xam,le: man ls#

    22

    ites !ithin s-uarebrackets are optiona#

  • 7/27/2019 01 - Basic Unix

    23/43

    !xam,le: man man#

    2*

  • 7/27/2019 01 - Basic Unix

    24/43

    !xam,le: man man#

    2+

    we can specifywhat !ind ofinformation wewant

  • 7/27/2019 01 - Basic Unix

    25/43

    Some other &sef&l commands

    -c ?file@ word count: counts characters' !ords' and #ines in theinput

    gre, pattern ?file@ se#ect #ines in the input that atchpattern

    head"n ?file@ sho! the irst n#ines o the input

    tail "n ?file@ sho! the #ast n#ines o the input

    c, file1file2 copy file

    1to file

    2

    m" file1file

    2

    o$e file1to file

    2

    2.

  • 7/27/2019 01 - Basic Unix

    26/43

    !xam,le: 7eleting a file

    iguring out !hich coand to use: a,ro,os delete produces any screenu#s o output that go by too

    -uick#y

    a,ro,os delete 4 more any screenu#s o output' but sho!n one screenu#

    at a tie

    ost o the coands sho!n aren6t re#e$ant

    23

  • 7/27/2019 01 - Basic Unix

    27/43

    !xam,le: 7eleting a file/(1)

    0dea 1: i#ter out irre#e$antstu

    man 6 delete 4gre, file

    27

    a #ot e!erresu#tsFnothingre#e$ant

  • 7/27/2019 01 - Basic Unix

    28/43

    !xam,le: 7eleting a file8(2)

    0dea 2: try a dierentkey!ord

    man 6 remo"e 4gre, file

    24

  • 7/27/2019 01 - Basic Unix

    29/43

    !xam,le: 7eleting a file8(*)

    0dea 2: try a dierentkey!ord

    man 6 remo"e 4gre, file

    2

    these are the on#y

    coands thatreer to reo$ingi#es

    l l i fil

  • 7/27/2019 01 - Basic Unix

    30/43

    !xam,le: 7eleting a file8(+)

    0dea 2: try a dierentkey!ord

    man 6 remo"e 4gre, file

    *8

    this is the on#y

    user coandthat reers toreo$ing i#es

    l l i fil

  • 7/27/2019 01 - Basic Unix

    31/43

    !xam,le: 7eleting a file8(.)Conir that this is the appropriate coand:

    man rm#

    *1

    strong#ysuggestaking thisyour deau#t

    Setting defa&lts for yo&r

  • 7/27/2019 01 - Basic Unix

    32/43

    Setting defa&lts for yo&rcommands

    Create an a#ias or your coand syntax dierent or dierent she##s

    bash: aliasaliasNameGcmdName#

    e$g$: alias rm%&rm "i#

    see man alias or detai#s

    o ha$e this a#ias in orce !hene$er you#og in' add this #ine to the i#e

    H"&bashrc "" assuing your #ogin she##is bash

    o ind out your #ogin she##' run thecoand

    echo I8 *2

  • 7/27/2019 01 - Basic Unix

    33/43

    'attern matching: gre,

    **

    'attern matching: gre,

  • 7/27/2019 01 - Basic Unix

    34/43

    'attern matching: gre,8(1)

    *+

    print the currentdirectory

    sho! the contents o thisi#e

    print out the #ines thatatch nation

    'attern matching: gre,

  • 7/27/2019 01 - Basic Unix

    35/43

    'attern matching: gre,8(2)

    *.

    print all lines in the input

    that match the string er

    'attern matching: gre,

  • 7/27/2019 01 - Basic Unix

    36/43

    'attern matching: gre,8(*)

    *3

    print all lines in the input that

    match the string er or re

    print all lines in the input thatbegin with the string er or re

  • 7/27/2019 01 - Basic Unix

    37/43

    'attern matching in the shell

    e can a#so use patterns in she##coands' e&g&:

    Example:

    *7

    9 atches any string

    /;

    atches any one o the characters !ithinbraces

    ls %9c #ist i#es that begin !ith %and end!ith c

    ls

    axy

  • 7/27/2019 01 - Basic Unix

    38/43

    0(1 Redirection

    =eau#t input"output beha$ior orcoands: stdin: keyboardF stdo&t: screenF stderr:

    screen

    e can change this using 0"> redirection:

    *4

    cmd file redirect cmd6s stdin to read rofile

    cmd Kfile

    redirect cmd6s stdout to file

    cmd KKfile

    append cmd6s stdout to file

    cmd LKfile

    redirect cmd6s stdout and stderr tofile

    cmd1D

    cmd2

    redirect cmd16s stdout to cmd26s

    stdin

    =etting more information

  • 7/27/2019 01 - Basic Unix

    39/43

    =etting more informationa%o&t files

    #s M# : pro$ides additiona# ino about i#es

    *

    = tti i f ti

  • 7/27/2019 01 - Basic Unix

    40/43

    =etting more informationa%o&t files8 (1)

    +8

    file namelast-modified timesizegroupowner

    no. of hard links

    access permissions

    file type

    nora# i#e

    d directory

    l(ell)

    sybo#ic #ink

  • 7/27/2019 01 - Basic Unix

    41/43

    )ile access ,ermissions

    +1

    access permissions for owner (u)

    access permissions for group (g)

    access permissions for others (o) r read

    - !rite

    x execute(executab#e i#e)

    enter (directory)

    no perission

    Changing file access

  • 7/27/2019 01 - Basic Unix

    42/43

    Changing file access,ermissions

    Coand:chmod whowhat file

    1file

    2 file

    n

    Example:

    +2

    N Or' !' xP

    N Oa' u' g' oP

    chmod &-foo

    reo$e !rite perission or user on i#e oo

    chmod g>rx

    %ar

    gi$e read and execute perission to group

    or bar

    chmod or-x9*doc

    reo$e a## access perissions or otherusers (i&e&' not o!ner or group ebers)or Q&doc i#es

    chmod a>r-

    ,9

    gi$e read and !rite perission to e$eryone

    or a## i#es starting !ith p

    )oregro&nd and Bacgro&nd

  • 7/27/2019 01 - Basic Unix

    43/43

    )oregro&nd and Bacgro&nd'rocesses

    u#tip#e processes can run concurrent#y at any point' there is exact#y one process that

    you can interact !ith through the keyboard(oreground process)

    reaining processes execute in thebackground

    , process can be started in thebackground:

    process;ae

    he execution o the current oregroundprocess can be paused $ia ctr#%R