UNIX - Linux Shell Cheat Sheet

Embed Size (px)

DESCRIPTION

UNIX - Linux Shell Cheat Sheet

Citation preview

  • 5/20/2018 UNIX - Linux Shell Cheat Sheet

    1/2

    A full PDF and online tutorial is available at http://steve-parker.org/sh/sh.shtml

    v1.1 7 Aug 2007

    UNI / !inu" #hell $heat #heet

    File %anipulation &est 'perators (ariable #ubstitution

    > file create (overwrite) file

    >> file append to file # do something

    >file 2>1 !oth output and errors to file "$%&err' "$ or err* if unset

    + file read from file

    a , ! pipe output from -a- as input to -!- Numeri) &ests $onditional *"e)ution

    less than c1 ,, c2 run c1 if it fails run c2

    $ommon $onstru)ts greater than c1 c2 run c1 if it wor/s run c2while read f eual to

    do read tet not eual $ommon utilities and s+it)hes

    echo ine is "f* file line greater or eual list files !iggest last

    done + file !3 line le less or eual list files newest last

    File &ests director3 si4es (slow)find newer than sort 5n sort numericall3 (not alpha)

    matching d is a director3 list m3 commands

    lines f is a file download 6

    r reada!le

    get field w writea!le touch file create file

    with delimiter eecuta!le read read * from /e3!oard

    get output #tring &ests

    of command 8 eual to

    4 4ero length

    n not 4ero length Net+orking

    a) case is a good list all networ/ interfaceswa3 to avoid !ogi)al &ests show routers

    iterating logical A9: log in to host as user u*

    !) through man3 ,, logical ;

    < logical 9;= user u*

    constructs.

    ) Argument (ariables

    "0 program name less file displa3 file page !3 page

    "1 alias l8-ls 5l- create l* as alias for ls 5l*

    "2

    ... ... list?of?files

    function "@ displa3 a calendar (ar 7B)

    declaration " all arguments show dis/ mounts

    ' and calling "# 9o. of arguments truss 5p CD:

    s3nta

    Files: $ontents / Attributes

    A for loop find . 5si4e 10/ 5print files over 10E!

    for i in iterates through find tet files

    do its input (which ls all directories

    echo File is "i* is su!Gect to

    done simple maths

    !etter maths

    Useful (ariables !ar* in file"DFH Dnternal File Heparator

    "& return code from last program of each line

    "HIJ what shell is running this script&

    A9K anguage L is 6H Jnglish file with !ar*

    http%MMsteve5par/er.orgMshMsh.shtml

    if N "* 5lt "3* O then "$%5def' "$ or def* if unset

    "$%8def' "$ (set to def* if unset)

    fi

    lt

    gt

    e

    ne

    ge ls 5lHr

    ls 5ltr

    ls 5lh human5reada!le filesi4es

    " grep foo m3file du 5s/ afoo nt

    foo ps 5ef

    foo!ar wget 6

    time cmd stopwatch on PcmdP

    " cut 5d% 5fQ MetcMpasswd

    :il!ert

    cmd , R cmd output to stdout and

    foo8PlsP tee file.tt also to file.tt

    nice cmd run cmd with low priorit3

    case "foo in

    ifconfig 5a echo foo is A* netstat 5r

    sshuShost

    scp file.tt R cop3 file.tt to host as

    echo foo is T* ifMelifMelifMelif uShost%

    ,eneral Admin

    echo foo is not A or T*

    1stargument

    esac 2ndargument tar cf t.tar R create a tar archive t.tar

    from the listed dirsMfiles

    dou!leit() @thargument cal B 1@7B

    epr "1 R 2 df 5h

    show s3scalls of CD:

    dou!leit B # returns U

    find . 5name .tt* 5print

    find Mfoo 5t3pe d 5ls

    under Mfoo

    glo!!ing) three8Pepr 1 V 2P

    echo Wscale 8 Q Q121 M 102X* , !c

    egrep (foo,!ar)* file find foo* or

    aw/ - print "Q '- file print the Qthword

    sed sMfooM!arMg file replace foo* in

    mailto:u@hostmailto:u@hostmailto:u@hostmailto:u@hostmailto:u@host
  • 5/20/2018 UNIX - Linux Shell Cheat Sheet

    2/2