unix commands in a nutshell 2

Upload: pavan-kumar

Post on 08-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 Unix Commands in a Nutshell 2

    1/54

    Training Material

  • 8/6/2019 Unix Commands in a Nutshell 2

    2/54

    July 17, 2009

    Unix Commands in a Nutshell

    Practical Usage of Commands

    (Please open this PPT in openOffice.org)

    DC/ SC/ Geo/Practice: Hi-Tech

    Name of the project/support fn: Numonyx

    Name of the author: Rohit ChopraDate Created:16-Jul-2009

  • 8/6/2019 Unix Commands in a Nutshell 2

    3/54

    July 17, 2009

    Objectives

    Introduction

    Unix Shell

    Unix commands

    Vi Editor

    String Manipulation

    Unix Utilities Monitoring Tools

    Scheduling Jobs

  • 8/6/2019 Unix Commands in a Nutshell 2

    4/54

    July 17, 2009

    Flavors of UNIX

    Mac OSXDarwin

    Red HatLinux

    SUNSolaris

    HPHP-UX

    DECDEC Ultrix

    Zilog ComputersZeus

    IBMIBM AIX

    Microsofts UNIX operating system

    to run on microcomputers

    Xenix

    Company

    Product Name

  • 8/6/2019 Unix Commands in a Nutshell 2

    5/54

    July 17, 2009

    UNIXFeatures

    Easier to modify the UNIX system code for installation on a new

    computer rather than write from scratch a new operating system

    Portability

    Several users can use the same computer simultaneously

    i.e. , more than one keyboard and terminal can be connectedto one computer

    Highly cost-effective

    Multi-user

    Performing tasks simultaneously rather than sequentially

    e.g., While printing a document , start editing another document

    Each task is considered to be a process

    Multitasking

    DescriptionFeature

  • 8/6/2019 Unix Commands in a Nutshell 2

    6/54

    July 17, 2009

    File ManagementAnd SecurityNetworking

    Services

    Date and TimeServices

    Input / Output

    Services

    SignalHandling

    ProcessScheduling

    SystemAdministrationand Accounting

    MemoryManagement

    UNIXUNIX

    SYSTEMSYSTEM

    KERNELKERNEL

    UNIX Kernel

  • 8/6/2019 Unix Commands in a Nutshell 2

    7/54

    July 17, 2009

    Bourne Shell (sh)

    C Shell (csh)

    TC Shell (tcsh)

    Korn Shell (ksh)

    Bourne Again Shell (bash)

    Different types of unix Shell

  • 8/6/2019 Unix Commands in a Nutshell 2

    8/54

    July 17, 2009

    Bourne Shell (sh)

    This is the original UNIX shell

    This shell doesnt have any interactive features.

    C Shell (csh)

    This shell provides C-like language for shell scripts.

    All features of modern shell like command line history, alias are included in this shell

    TC Shell (tcsh)

    Similar to C shell but have emacs style editing

    All features of modern shell are available.

    Unix Shell

  • 8/6/2019 Unix Commands in a Nutshell 2

    9/54

    July 17, 2009

    Korn Shell (ksh)

    This shell is now standard shell on UNIX systems

    This shell provides all the features of C shell along with scripting similar to original Bourne shell (sh)

    All features of modern shell are available.

    Unix Shells

    Bourne Again Shell (bash)

    This is the first public domain shell

    This shell is full implementation of IEEE Posix shell and tool specifications.

    All features of modern shell like command line history, alias, command line editing etc are included in this shell.

    Unix Shell

  • 8/6/2019 Unix Commands in a Nutshell 2

    10/54

    July 17, 2009

    - Ordinary Files

    - Directory Files

    - Special Files

    - Standard Files

    Classification of files in Unix

  • 8/6/2019 Unix Commands in a Nutshell 2

    11/54

    July 17, 2009

    A file can be addressed as container of data.

    Directory is a container of files.

    Every file is assigned to a Directory

    $ls -ld /usr/bin

    drwxr-xr-x 4 bin bin 17408 May 25 03:51 /usr/bin

    $ls -ld /etc/passwd-rw-r--r-- 1 root security 6374 May 09 07:56 /etc/passwd$

    Files and Directories

  • 8/6/2019 Unix Commands in a Nutshell 2

    12/54

    July 17, 2009

    * Used to reference Physical devices such as terminals and disks

    * Read and written like ordinary files

    Device Drivers associated with each file

    $ls -ld /dev/nullcrw-rw-rw- 1 root system 2, 2 Aug 14 18:30 /dev/null$ls -ld /dev/cd0br--r--r-- 1 root system 14, 0 Oct 28 2005 /dev/cd0$ls -ld /dev/ttycrw-rw-rw- 1 root system 1, 0 Aug 14 18:29 /dev/tty$

    Concept of Special Files

  • 8/6/2019 Unix Commands in a Nutshell 2

    13/54

    July 17, 2009

    Helps display information on the screen* Special Names for Communication channels

    * Keyboard input channel is called Standard input

    (stdin) - file id is 0

    * Terminal Screen output is called Standard output

    (stdout) - file id is 1

    Diagnostic error messages (generated by a program) are sent to

    Standard error

    (stderr) - file id is 2 ( shown on terminal screen )

    * All three files are open by default at the time oflogin

    Concept of Standard Files

  • 8/6/2019 Unix Commands in a Nutshell 2

    14/54

    July 17, 2009

    * Hierarchical

    * Ownership on each file

    - User / Owner- Group- Others

    * Separate security for permissions

    - read- write and- execute

    * Removable

    * File Independence

    * Time stamp on each file

    - Modification time

    - Access time

    Features

  • 8/6/2019 Unix Commands in a Nutshell 2

    15/54

    July 17, 2009

    * Three levels of access

    User/Owner, group, others

    * Three types of access on Files and Directories

    Read, Write, Execute

    AccessM

    ode Ordinary Directory

    Read Examination of Listing ofFile Contents files within

    Directory

    Write Allows changing Creating new

    of filecontents files withinDirectory

    Execute Executing file as Searching thea command Directory allowed

    allowed

    Security and Access Methods

  • 8/6/2019 Unix Commands in a Nutshell 2

    16/54

    July 17, 2009

    Useful Commands

    mkdir creates a new directory

    rm removes a file

    rmdir removes a directory

    du displays disk usage

    df displays number of free block

    touch updates the time of last modification

    find locates files that match certain area

    file displays the type of file

    pwd displays f ull pathname ofcurrent directory

  • 8/6/2019 Unix Commands in a Nutshell 2

    17/54

    July 17, 2009

    cat Concatenate & Print on screen or printer

    $cat [Options] [Arguments]

    Options - take input from stdin

    -n no. of output lines

    -s squeeze adj. blank lines

    -v enable display of non-printing

    characters-b used with -n to avoid numbering

    blank lines

    e.g.,

    $cat try.c Display the contents of try.c on the screen

    $cat Takes input from stdin i.e. keyboard and

    displays on screen

    File Contents

  • 8/6/2019 Unix Commands in a Nutshell 2

    18/54

    July 17, 2009

    Usages of cat command

    $ cat f1 > f2 Takes input from file f1 & puts iton file f2

    $ cat f2 > f3 f3 contains the contents off1

    $ cat f4 >> f3 Appends the contents off4 to file f3

    $ cat try[0-3] > final The file final contains contentsof try0, try1, try2 try3

    $ cat test* > report The file report contains all filesbeginning with test

  • 8/6/2019 Unix Commands in a Nutshell 2

    19/54

    July 17, 2009

    UNIX Commands

    $ Is -IList the files along with the protection bits and the user

    $ Is -1 symtab.csymtab.otreegentest

    $ Is -I-rw-rr 1 smj proj1 30766 Mar 3 15:08 symtab.c-rw-rr 1 smj proj1 8759 Mar 3 15:12 symtab.o

    -rwxr-xr-x 4 smj proj1 40743 Mar 3 15:23 treegendrwxrwxr-x 1 smj proj1 53 Mar 1 09:15 test

    $ Is -a List the files starting with .and ..also / hidden files....profile.cshrcsymtab.c

    ...

    $ Is -iI 10936-rw-rrI smj proj1 3076 Mar 3 15:08 test.c10936 - inode number of file test.c

  • 8/6/2019 Unix Commands in a Nutshell 2

    20/54

    July 17, 2009

    Unix Command Continues...

    wc WordCount$wc [Options] filename

    Options - Display no. of lines, words, characters-I Display no. of lines-w Display no. of words

    -c Display no. ofcharacterse.g., $ wc test.c

    20 200 5678

    20 - lines

    200 - words

    5678- characters

  • 8/6/2019 Unix Commands in a Nutshell 2

    21/54

    July 17, 2009

    Unix Command Continues...

    cp copy a file

    -i - user interactive mode

    e.g.,$cp test.c test.c.baktest.c and test.c.bak contain the same contents

    Extra disk storage

    In Create link

    e.g.,

    $ ln first.c second.cThe file is referenced by two different namesNo Extra disk storage

  • 8/6/2019 Unix Commands in a Nutshell 2

    22/54

    July 17, 2009

    Unix Command Continues...

    mv Moves or renames files and directories

    -i interactive modee.g.,

    $ mv old.c new.cRenames the file old.c as new.c

    rm Deletes the indicated file(s) files

    rm removes files and directories

    -i remove interactively-f forcible remove

    -r remove recursively Dangerous

    used in conjunction with -i

    touch Updates access, modification or change times of a file

    -a update access time-m update modification time-c prevents creating the file

    e.g., $ touch f1* The current system date & time stamp is put on the file f1* Iff1 does not exist then it is created with 0 bytes

  • 8/6/2019 Unix Commands in a Nutshell 2

    23/54

    July 17, 2009

    cmp Compare two files

    If files are same no output is sent to the terminal, or else The line numberand the byte at which the first difference occurs is reported

    -s Outputs nothing Registers return code

    Return code

    0 if files are identical1 if files are different2 on error

    e.g.,

    $ cmp test1 test2test1 and test2 differ in char 36 line 3$ cmp -s test1 test2$ echo $?

    outputs 1 indicating that the files are different

    File Comparison

  • 8/6/2019 Unix Commands in a Nutshell 2

    24/54

    July 17, 2009

    File Comparison continues..

    diff - Reports more than one differences

    $diff [Options] file1 file2

    -b Ignores trailing blanks

    -e Gives a list ofed commands so as to convert

    file1 into file2.

    e.g., $ diff test1 test2

    Outputs: n1 a n3,n4

    n1,n2 d n3

    n1,n1 c n3,n4

    where * n1 ,n2, n3 ,n4 are line numbers* a ,d, c means append, delete ,change respectively

  • 8/6/2019 Unix Commands in a Nutshell 2

    25/54

    July 17, 2009

    File Comparison continues..

    commD

    isplay common lines

    $comm -[123] f1 f2Prints a three column output:

    - lines that occur only in f1

    - lines that occur only in f2

    - lines that occur in both

    comm -12 - prints lines common to the two filescomm -23 - prints only lines in the first file but not in the

    second

    comm -123 - prints nothing

    e.g.,

    $ comm test1 test2 Reports the common lines between

    files test1, test2 and reports thelines differing

    $ comm -12 test1 test2 Prints line common to both

  • 8/6/2019 Unix Commands in a Nutshell 2

    26/54

    July 17, 2009

    pack Compress the file

    $ pack

    e.g., $ pack try- Creates a file try.z which is packed

    - Normally the hu

    ge logs are packed- The size is reduced by 25 - 40 %

    unpack Uncompress packed fileor

    pcat

    e.g., $ unpack try.zunpacks the file try.zor

    $ pcat try.zread the contents of try.z

    File Compression

  • 8/6/2019 Unix Commands in a Nutshell 2

    27/54

    July 17, 2009

    passwd To change the password or yourshell

    chown To change the ownership of the file

    $ chown owner filename

    e.g., $ chown trg2 test.c* Initially the owner is trg1

    * Only the owner or the superusercanchange the ownership of the file

    File Security

  • 8/6/2019 Unix Commands in a Nutshell 2

    28/54

    July 17, 2009

    File Security Continues...

    umask Set file creation mode mask$ umask nnn(nnn set file creation mode)umask can also be set as a shell variable

    e.g.,

    umask 022- Files normally created with 666 mode is assigned

    644 permission

    The value of each digit is subtracted from the corresponding "digit" specified bythe system for the creation of a file.

  • 8/6/2019 Unix Commands in a Nutshell 2

    29/54

    July 17, 2009

    tail Displays the last lines of fileoptions : -n (n= no. of lines)e.g., $ tail -30 test.cDisplays the last 30 lines of file test. c

    head Displays the top lines of filee.g., $ head -10 test.c Displays the first 10 lines oftest.c

    split S plits the file into different files as specified by the number of linese.g., $ split -20 test.c

    Splits the file test.cin blocks of 20 lines and creates filesxaa,xaband so on, such that

    xaa has first 20 lines oftest.cxab has the next 20 lines oftest.c

    ...

    The file test.c is unaffected

    $ split-20 test.c try Generates files as tryaa , tryab , tryac

    paste Joins the two or more files horizontallye.g., $ paste xaa xabFile xaa and xab are joined horizontally and output to the terminal

    File Operation

  • 8/6/2019 Unix Commands in a Nutshell 2

    30/54

    July 17, 2009

    The editor* Powerful full screen editor

    * vi v/s ed, ex

    * Mostly single key stroke commands

    * Interface with ex

    * Macro facility

    * Ability to process text

    Invoking $ vi filename

    e.g., $ vi pgm.c

    Modes :

    * Command mode* Insert mode

    * From Insert mode Pressing

    remitts Command mode

    The vi - Visual Editor

  • 8/6/2019 Unix Commands in a Nutshell 2

    31/54

    July 17, 2009

    Types of commands

    * vi-commands (invisible)

    Command mode

    Commands can be categorised as :

    * Cursor movement

    * Text manipulations

    - insert, delete, copy, change

    * Marking/Selecting, Positioning* Search

    Objects of interest recognized by vi:* characters

    * words

    * lines

    * block

  • 8/6/2019 Unix Commands in a Nutshell 2

    32/54

    July 17, 2009

    Format of commands

    [count] command [operand]

    Use . to repeat last commandUse u to undo last command

    CursorMovementLine Oriented :

    ^ or (zero) beginning of line$ end of line

    Character orientedh move left

    l move right

    j move downk move up

  • 8/6/2019 Unix Commands in a Nutshell 2

    33/54

    July 17, 2009

    Format of commands (contd.)

    Word Oriented :

    e move to end of next word

    w move to beginning of word

    b move to beginning of previous word

    E move to end of next word

    ignoring punctuation

    W move to beginning of word ignoring

    punctuation

    B move to previous word ignoring

    punctuation

  • 8/6/2019 Unix Commands in a Nutshell 2

    34/54

    July 17, 2009

    Text Manipulation

    Searching for a string :

    /string for searching forward

    ?string for searching backward

    Use n to continue search in the same direction

    Use N to continue search in the opposite direction

    s/oldstr/newstr commands can be used in vi

  • 8/6/2019 Unix Commands in a Nutshell 2

    35/54

    July 17, 2009

    Common Set commands

    set ai Causes automatic indentation

    set noai Nullifies the effect of auto-indent

    set nu Causes line numbers to be displayed

    set wrapmargin = n Sets n column right margin

    set bf The beautify option Removes allunimportant control characters

    set Causes a displays ofcurrent set options

  • 8/6/2019 Unix Commands in a Nutshell 2

    36/54

    July 17, 2009

    Shell Utilities and Commands

  • 8/6/2019 Unix Commands in a Nutshell 2

    37/54

    July 17, 2009

    Shell Startup Dot files

    Helps in customizing UNIX System

    * Executes .profile forbsh, ksh and bash when user logs on

    * Executes .login and .cshrc for C Shell when user logs on

    * Executes .logout for C Shell when user logs out from C-Shell

    * Keeps track of the history mechanism In C Shell through the .history

  • 8/6/2019 Unix Commands in a Nutshell 2

    38/54

    July 17, 2009

    Shell Variables

    User definedassignment : name = value

    e.g., (on Bourne shell)

    $ x=Hello how are you$ echo $x

    Hello how are you

    $ PATH=$PATH:/usr/games$ export PATH

    $ echo $PATH

    :.:/usr/bin:/bin:/usr/games:

  • 8/6/2019 Unix Commands in a Nutshell 2

    39/54

    July 17, 2009

    Predefined Special Parameters

    $# number of positional parameters

    $? exit value of last command

    $$ process number of a process

    $* all the parameters

    $! process id of last command started with

    asynchronous execution i.e., &

  • 8/6/2019 Unix Commands in a Nutshell 2

    40/54

    July 17, 2009

    Variables Used by Shell in .profile

    HOME login dir

    PATH search path forcommands

    PS1 primary system prompt

    PS2 secondary system prompt (eg. With whilestatements)

    MAIL files containing mail messages

    TERM terminal type

  • 8/6/2019 Unix Commands in a Nutshell 2

    41/54

    July 17, 2009

    To algebraically compare variable values

    test $num1 -eq $num2

    Other algebraic operators -ne, -gt -ge -lt, -le

    File related

    $ test -f file True if file exists and is an ordinary file

    $ test -r file True if file exists and is readable file

    $ test -w file True if file exists and is writable file

    $ test -s file True if file exists and has size greater than zero

  • 8/6/2019 Unix Commands in a Nutshell 2

    42/54

    July 17, 2009

    grep get regular expressions only

    fgrep fast, several simple strings at one time

    egrep extended grep, can handle more powerfulexpressions like | - or operators

    FILTERS :

  • 8/6/2019 Unix Commands in a Nutshell 2

    43/54

    July 17, 2009

    Searching Files with grep

    e.g.,

    $ grep -ni func1 *.c Prints all the lines and linenumbers in files *.c that match pattern func1

    ignoring the case

    $ grep * * Search for the pattern * in all the files

    $ ls -l | grep ^d Searches for all subdirectories

  • 8/6/2019 Unix Commands in a Nutshell 2

    44/54

    July 17, 2009

    Cut

    * Deletes columns from a file producing a new file with

    shorter lines

    * Cu

    ts ou

    t selected fields of ea

    ch line in a file.

    * Cuts columns from a table or fields from a file which can

    be of type

    - Fixed length fields or

    - Delimited by some character

    Processing Tabular Data

  • 8/6/2019 Unix Commands in a Nutshell 2

    45/54

    July 17, 2009

    Cut (contd.)

    cut -c list { file1 file2 ...}e.g.,

    cut -c 5-70 file1

    cut would pass 5-70 characters from file1

    cut -flist {-d char } {file1 file2....}

    e.g.,

    who | cut -d -f1

    gives a list ofuser login names

    cut -d: -f 1,5 /etc/passwd

    gives a list ofuser ID and names

  • 8/6/2019 Unix Commands in a Nutshell 2

    46/54

    July 17, 2009

    PS, kill

    $ somecommand &

    5511 - pid

    $ps

    pid tty time command

    3432 2 0 : 24 -sh5765 2 0 : 03 ps5511 2 0 : 51 somecommand

    $ kill 5511$ ps

    pid tty time command

    3432 2 0 : 24 -sh5985 2 0 : 03 ps

  • 8/6/2019 Unix Commands in a Nutshell 2

    47/54

    July 17, 2009

    $ stubborn-cmd &

    pid tty time command3432 2 0 : 24 -sh6004 2 0 : 03 ps5995 2 0 : 44 stubborn-cmd$ kill 5995

    $ ps pid tty time command3432 2 0 : 24 -sh6004 2 0 : 03 ps5995 2 0 : 44 stubborn-cmd$ kill -9 5995

    $ ps pid tty time command3432 2 0 : 24 -sh6103 2 0 : 03 ps

  • 8/6/2019 Unix Commands in a Nutshell 2

    48/54

    July 17, 2009

    1. Usercommunication commandsmailwritetalkpostnewsmesgwall

    2. Networking commandsuucpuutorlogintelnet

    ftprcpuuxsftpscp

    Communication Features

  • 8/6/2019 Unix Commands in a Nutshell 2

    49/54

    July 17, 2009

    Practical Use of the Command (cat)

    $cat /etc/passwd > temp

    $sort < temp

    $ rm temp

    These statements first create a temporary file, Sort the file

    and then delete the temporary file.This same result can be

    achieved through a single unix statement.

    $ cat /etc/passwd | sort

    This requires No need ofcreation/deletion of a file and then sorts

    the filepasswd as per the first entry in passwd.

  • 8/6/2019 Unix Commands in a Nutshell 2

    50/54

    July 17, 2009

    Practical Use of the Command (find)

    find . -name text.xmlThis command will search a file text.xml from the

    current path of the directory.

    find / -atime -30 -print

    This command will list all the files accessed less

    30 days ago.

    find / -atime +30 -print

    This command will list all the files accessed after30 days

  • 8/6/2019 Unix Commands in a Nutshell 2

    51/54

    July 17, 2009

    Practical Use of the Command (find)

    find . -type f -print | xargs grep -il StringName

    This is a very useful command which is a

    combination of find and grep commands. This

    command will find a file which contains a matching

    StringName in it.

    find /directory -mtime +30 -print -exec rm {} \;

    This command will remove all the files in /directory that have

    not been modified in 30 days.

  • 8/6/2019 Unix Commands in a Nutshell 2

    52/54

    July 17, 2009

    Practical Use of the Command (find)

    find $SEARCH_PATH -size +5000000c -print

    To search for files greater than 5MB we can use the

    above command where the + (plus sign) specifies greater than,

    and the c denotes bytes

    find $SEARCH_PATH -mtime 1 -print

    This command will find all files that have been modified, or

    created, in the last 24 hours

    TS=$(date +%m%d%y%H%M%S)

    This date command gives two digits for month, day, year,

    hour, minute, and second:

  • 8/6/2019 Unix Commands in a Nutshell 2

    53/54

    July 17, 2009

    Practical Use of the Command (SED)

    chsh username new_default_shell

    The chsh command, short forchange shell, allows you to modifythe system environment for your login

    SED is used to edit a large number ofconfiguration files .

    for each in `ls *.cfg`; domv $each $each.bak #Safety First!sed '30,35s/^/\#/g' $each.bak > $eachdone

    This will flow through all of the config files in a certain directory

    and add a # sign at the beginning of lines 30 through 35,

    commenting those lines out and if all goes

    well, delete all of the .bak files created as backups by the script.

  • 8/6/2019 Unix Commands in a Nutshell 2

    54/54

    July 17 2009

    Practical Use of the Command (AWK)

    awk ' '{print $1}' file

    This command is used to extract the first column from a file.AWK command allows the user to manipulate files that are

    structured as columns of data and strings.

    ls files_list| awk '{print "mv "$1" "$1".new"}' | shThis command will help in renaming the files (append .new

    to "files_list"):