fundamentals of unix - for freshers

Upload: krishna-keshav

Post on 04-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 Fundamentals of Unix - For Freshers

    1/299

    LogicaCMG 2006. All rights reserved1

    Introduction to Unix

    Gopan Nair

    Systems Manager Financial Services

  • 7/29/2019 Fundamentals of Unix - For Freshers

    2/299

    2

    Schedule

    Overview of Unix

    Login Process

    Learning the basic commands

    Getting Help

    Customizing the shell environment Understanding file permissions

    Job Control

  • 7/29/2019 Fundamentals of Unix - For Freshers

    3/299

    3

    Schedule

    Using the vi editor

    Regular expressions and pattern matching

    Shell programming (Bourne shell)

  • 7/29/2019 Fundamentals of Unix - For Freshers

    4/299

    4

    What Is an Operating System?

    Operating

    System

    Users

    Programs

    Consumers

    Disks

    Memory

    CPU

    Network

    Printers

    Resources

  • 7/29/2019 Fundamentals of Unix - For Freshers

    5/299

    5

    Historyof the UNIX Operating System

    UNIX is originally developed at AT&T Bell Laboratories in 1969 by Ken Thompson

    In 1973, Unix was re-written in C language. This led to its popularity

    In 1983, Bell Lab announced their support for a new version, known as System V.

    By this time, University of California at Berkeley had also developed a new variant of Unix,

    known as 4.3 BSD.

    The 4.3 BSD provided new features such as reliable signals and TCP/IP.

    Neither AT&T nor BSD is now active in Unix developmentso most new developments are

    now defined by a specific vendor or a body such as IEEE (POSIX standard), FreeBSD

    organization or the Linux community.

  • 7/29/2019 Fundamentals of Unix - For Freshers

    6/299

    6

    Different Flavours of Unix?

    Product From Processor

    AIX IBM PowerPC

    HP UX HP PA-RISC/ITANIUM

    Solaris Sun SPARC

    Tru64Unix Compaq Alpha

    IRIX SGI MIPS

    LINUX Freeware Intel

    SCO Unix SCO Intel

    NCR MP-RAS NCR Motorola/Intel

  • 7/29/2019 Fundamentals of Unix - For Freshers

    7/2997

    Features of UNIX

    Hierarchical file system

    Multi-tasking

    Multi-user

    Provides more than one type of shells (command interpreters)

    Provides more than one mechanism for inter-process communication (IPC)

    Excellent networking support through TCP/IP

    Portability

  • 7/29/2019 Fundamentals of Unix - For Freshers

    8/2998

    Unix Architecture

  • 7/29/2019 Fundamentals of Unix - For Freshers

    9/2999

    What Defines a User Account?

  • 7/29/2019 Fundamentals of Unix - For Freshers

    10/29910

    Creating a User Account

    #useradd

  • 7/29/2019 Fundamentals of Unix - For Freshers

    11/29911

    passwd Assigns a login password

    $ passwdChanging password for user3Old password:

    New password:Re-enter new password:

    minimum of six characters

    at least two alpha characters

    at least one non-alpha character

    Setting Your Password

  • 7/29/2019 Fundamentals of Unix - For Freshers

    12/29912

    Your Login Process

    When you log in successfully, the first program started is the shell, orcommand line interpreter. Now that you have logged in you shouldsee a $ prompt awaiting commands. This program gives you amethod of communicating with the operating system to run andmanage other programs.

    For the login shell process, Unix makes yourhome directory as thecurrent directory.

    Therefore, after login, by default, you will be working with your home

    directory.

  • 7/29/2019 Fundamentals of Unix - For Freshers

    13/299

    13

    Your Login Process

    At login time, each shell reads one or more initialization files from your homedirectory. These files give you the opportunity to set environment variables

    The name of this script depends upon the login shell. For Bourne shell, this file is.profile file. For C-shell, this file is .cshrc file.

    To customize your login environment, you can modify these startup scripts.

  • 7/29/2019 Fundamentals of Unix - For Freshers

    14/299

    14

    The Shell Command Interpreter

    Functions of a Shell:

    After login you are presented with a shell prompt which waits

    for you to type commands Validates the command entered by

    the user.

    Once the return key is pressed, the shell processes the command

    When the command is finished, the shell re-displays the prompt.

    This process continues until the user exits the shell, by typing exit

    or by pessing ctrl-d, at which time the user is logged out of theUNIX host

  • 7/29/2019 Fundamentals of Unix - For Freshers

    15/299

    15

    Basic Unix Commands

  • 7/29/2019 Fundamentals of Unix - For Freshers

    16/299

    16

    Using Unix Commands

    Most Unix commands are individual programs that perform various tasks. Unixhas a large set of commands that are described related reference (man) pages.

    When you enter a command, the shell interprets the command, and then gives anappropriate response -- that is, the system either runs the program or displays an

    error message.

    If the command is valid, the shell directs the operating system to do what isrequested.

    Note: Unix is case sensitive

  • 7/29/2019 Fundamentals of Unix - For Freshers

    17/299

    17

    Getting Help

    The UNIX reference manual is divided into eight numbered

    sections:

    1. General User Commands

    2. System Calls

    3. User-level Library Functions

    4. Device Drivers, Protocols

    5. File Formats

    6. Games (rarely available)

    7. Document Preparation

    8. System AdministrationYou can see the command summary for each section by typing:

    man #intro where # is one of the eight section numbers

  • 7/29/2019 Fundamentals of Unix - For Freshers

    18/299

    18

    Syntax:

    man [-k| X ] keyword | command

    in which Xis the number of one of the manual sections

    Examples:

    $ man date$ man -k copy $ apropos copy $ man passwd $ man 4 passw

    Use to view next page.

    Use to view next line.

    Use to quit the man command.q

    Space

    Return

    The Online Manual

  • 7/29/2019 Fundamentals of Unix - For Freshers

    19/299

    19

    General commands

    stty set terminal options

    date display or set the date

    ps display information about processes

    env display or change current environment

    id Display you user and group identificationswho dentify other users logged on to the system

    echo Display simple messages to your screen.

    clear Clears terminal screen.

  • 7/29/2019 Fundamentals of Unix - For Freshers

    20/299

    20

    The stty Command

    stty reports or sets terminal control options

    $stty -aspeed 9600 baud; 45 rows; 107 columns;

    eucw 1:1:0:0, scrw 1:1:0:0:intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = ^@

    eol2 = ^@; start = ^Q; stop = ^S; susp = ^Z; dsusp = ^Y; reprint = ^R

    $ stty erase ^H to change the erase character from ^? (the delete key) to ^H$ stty echo To stop the display of the characters typed$ stty echo

  • 7/29/2019 Fundamentals of Unix - For Freshers

    21/299

    21

    The date Command

    Date [options] [+format] Displays or sets the date or time

    Common Options

    -u use Universal Time (or Greenwich Mean Time)+format specify the output format

    %a weekday abbreviation, Sun to Sat

    %j day of year, 001 to 366%t %y last 2 digits of year, 00 to 99%D MM/DD/YY date

    $ date mmddHHMM[yy] sets the system date$date +%a%t%DMon 06/10/96

    $date '+%y:%j'96:162

  • 7/29/2019 Fundamentals of Unix - For Freshers

    22/299

    22

    id Displays user and group identification for session

    $iduid =303 (user3) gid=300 (class)

    The id Command

  • 7/29/2019 Fundamentals of Unix - For Freshers

    23/299

    23

    who [am i] Reports information about users who arewhoami currently logged on to a system

    $ whoroot tty1p5 Jul 01 08:01user1 tty1p4 Jul 01 09:59user2 tty0p3 Jul 01 10:01

    $ who am i

    user2 tty0p3 Jul 01 10:01

    $ whoamiuser2

    $ tty Prints the name of the tty line

    The who Command

  • 7/29/2019 Fundamentals of Unix - For Freshers

    24/299

    24

    The echo Command

    echo [ String ... ]Writes character strings to standard output

    $ echo Hello Class or echo "Hello Class

    Hello Class

    $ echo \n Hello Class

    $ echo $PATH

  • 7/29/2019 Fundamentals of Unix - For Freshers

    25/299

    25

    clear Clears terminal screen

    cls

    The clear Command

  • 7/29/2019 Fundamentals of Unix - For Freshers

    26/299

    26

  • 7/29/2019 Fundamentals of Unix - For Freshers

    27/299

    27

    sbin usr dev etc var stand

    profilegroup

    user1 user2 user3

    = directory

    = file

    passwd

    opt hometmp

    bin contrib local sbinlib vueshare

    mail news tmp vue

    vi

    man

    cp ls man sh tar

    vmunix

    The File System Hierarchy

  • 7/29/2019 Fundamentals of Unix - For Freshers

    28/299

    28

    Monitoring Disk Usage

    Monitor available file system space with df:

    # df

    Filesystem

    /dev/vg00/lvol5

    /dev/vg00/lvol4

    /dev/vg01/myfs1

    /dev/vg01/myfs2

    Determine space used by directory subtrees with du:

    # du -sk /myfs2/*

    844 /myfs2/data1

    1327 /myfs2/data2

    1073 /myfs2/data3

    10757 /myfs2/data4

    4 /myfs2/lost+found

    kbytes

    294912

    24576

    16384

    15893

    used

    261523

    19333

    1174

    14006

    avail

    31352

    4978

    14331

    297

    %used

    89%

    80%

    8%

    98%

    Mounted on

    /opt

    /home

    /myfs1

    /myfs2

  • 7/29/2019 Fundamentals of Unix - For Freshers

    29/299

    29

    Name of current working directory

    The directory in which you are working at any given time is yourcurrent, orworking directory.

    If you are uncertain about the directory in which you are working, enter the (print working directory) command.

    You can use the cd command to move to your home directory from any location.

  • 7/29/2019 Fundamentals of Unix - For Freshers

    30/299

    30

    Understanding pathnames

    A pathname specifies the location of a directory or a file within the filesystem.

    A pathname consists of a series of directory names separated by

    slashes ( / ) that ends with a directory name or a file name.

    A pathname that starts with a slash ( / ) is called a full pathname or an

    absolute pathname.

    A pathname that starts without a slash ( / ) is called a relative

    pathname

  • 7/29/2019 Fundamentals of Unix - For Freshers

    31/299

    31

    Understanding Absolute pathnames

    You can also think of a full pathname as the complete name of a file or adirectory in relation to the root (/) directory.

    Regardless of where you are working in the file system, you can alwaysfind a file or a directory by specifying its full pathname.

  • 7/29/2019 Fundamentals of Unix - For Freshers

    32/299

    32

    Understanding Relative Pathnames

    The file system also lets you use relative pathnames. Relativepathnames do not begin with the / that represents the root directorybecause they are relative to the current directory.

    You can specify a relative pathname in one of several ways:

    As the name of a file in the current directory or with the name of a directoryone level below your current directory.

    As a pathname that begins with .. (dot dot, the relative pathname for theparent directory).

    As a pathname that begins with . (dot, which refers to the current directory).This relative pathname notation is useful when you want to run your ownversion of an operating system command in the current directory (for

    example ./ls).

  • 7/29/2019 Fundamentals of Unix - For Freshers

    33/299

    33

    Understanding directories

    In Unix, a directory is a file that has a number of entries. Each entrycontains:

    A filename

    The I-node number of the file

    Every directory contains at least two entries:

    Filename of .. (dot dot)

    Filename of. (dot)

  • 7/29/2019 Fundamentals of Unix - For Freshers

    34/299

    34

    Understanding directories

    In the C shell and the Korn or POSIX shell, you may also use a tilde ( ~) at thebeginning of relative pathnames.

    The tilde character used alone specifies your home directory. The tilde characterfollowed by a user name specifies the home directory of that user. For example,

    $ cd ~/mydir

    $ cd ~micky/mouse

  • 7/29/2019 Fundamentals of Unix - For Freshers

    35/299

    35

    pwd

    ls

    cd

    find

    mkdir

    rmdir

    Displays the directory name of your current location in the hierarchy.

    Sees what files and directories are under the current directory.

    Changes your location in the hierarchy to another directory.

    Finds files.

    Creates a directory.

    Removes a directory.

    Basic File System Commands

  • 7/29/2019 Fundamentals of Unix - For Freshers

    36/299

    36

    ls [-adlFR] [pathname(s)]

    $ ls

    f1 f2 memo$ ls -Ff1 f2* memo/$ ls -aFprofile f1 f2* memo/$ ls memo

    f1 f2$ ls -F /homeuser1/ user2/ user3/$ ls -F ../user2f1

    ls List Contents of a Directory

  • 7/29/2019 Fundamentals of Unix - For Freshers

    37/299

    37

    cd [ ]dir_pathname

    $ pwd/home/user3$ cd memo; pwd/home/user3/memo$ cd ../..; pwd/home$ cd /tmp; pwd/tmp$ cd; pwd/home/user3$ cd -

    cd Change Directory

  • 7/29/2019 Fundamentals of Unix - For Freshers

    38/299

    38

    findpath_list expression

    $ find . -name .profile./.profile$

    Performs an ordered search through the file system.

    path_list is a list of directories to search.expression specifies search criteria and actions.

    The find Command

  • 7/29/2019 Fundamentals of Unix - For Freshers

    39/299

    39

    The find Command More options

    Option Description-amin n file was last accessed nminutes ago

    -atime n file was last accessed 24*nhours ago

    -empty file is empty and is either a ordinary file or a directory

    -exec execute commands

    -mtime n file was last modified n*24 hours ago

    -size n[ck] size of file is n512 byte blocks (c bytes, k kilobytes)

    -user unametrue if file belongs to uname

  • 7/29/2019 Fundamentals of Unix - For Freshers

    40/299

    40

    mkdir [-p] dir_pathname(s)

    rmdir dir_pathname(s)

    $ pwd/home/user3

    $ mkdir fruit

    $ mkdir fruit/apple

    $ cd fruit

    $ mkdir grape orange

    $ rmdir orange

    $ cd ..$ rmdir fruit

    rmdir: fruit not empty

    $ rmdir fruit/apple fruit/grape fruit

    mkdir and rmdir Create and Remove Directories

  • 7/29/2019 Fundamentals of Unix - For Freshers

    41/299

    41

    Disk usage summary: du

    du - summarizes disk space usage$du sk *

    538873 WL8.1

    50150 jre130

    2 logs1 registry.xml

    346190 tuxedo8.0

    282596 wl61

    $ du

    sk .

  • 7/29/2019 Fundamentals of Unix - For Freshers

    42/299

    42

    File

    Directory

    Tree

    Path name

    HOME

    pwdcdlsfind

    mkdirrmdir

    A container for data

    A container for files and other directories

    Hierarchical structure of a UNIX system

    Identifies a file's or directory's location in the hierarchy

    Represents the path name of your login directory

    Displays your current location in the hierarchyChanges your location in the hierarchy to another directory

    Lists the contents of a directory

    Finds files specified by options

    Creates directories

    Removes directories

    The File SystemSummary

  • 7/29/2019 Fundamentals of Unix - For Freshers

    43/299

    43

  • 7/29/2019 Fundamentals of Unix - For Freshers

    44/299

    44

    A container for data or a link to a device.

    Every file has a name and may hold data that resides on a disk.

    There are several different types of files:

    Regular files

    text, data, drawings

    executable programs Directories

    Device files

    What Is a File?

  • 7/29/2019 Fundamentals of Unix - For Freshers

    45/299

    45

    Manipulating files and directories

    In Unix, each file is a series of bytes without any structure (format). The directory isthe only file that has a structure imposed on it.

    Each file has one or more names. Each file is also stored in a directory. Thedirectories are stored in the file system.

    A file system is the useful arrangement of files into directories.

  • 7/29/2019 Fundamentals of Unix - For Freshers

    46/299

    46

    Listing files and directories

    In Unix, each file has a number of attributes associated with it. Some ofthese are:

    File type File permissions

    File owner user ID and group ID

    File size

    Date/time of creation, last update and last access

    Number of hard links

  • 7/29/2019 Fundamentals of Unix - For Freshers

    47/299

    47

    File names

    A file name can contain any character except the following because these havespecial meaning to the shell:

    Slash ( / )

    Backslash ( \ )

    Ampersand ( & ) Left- and right-angle brackets (< and >)

    Question mark ( ? )

    Dollar sign ( $ )

    Left bracket ( [ )

    Asterisk ( * )

    Tilde ( ~ )

    Vertical bar or pipe symbol ( | )

  • 7/29/2019 Fundamentals of Unix - For Freshers

    48/299

    48

    File names

    You may use a period or dot ( . ) in the middle of a file name. If you use a dot atthe beginning of the file name it will be hidden when doing a simple listing of filesthru the ls command.

    The maximum length of a file name depends upon the file system used. Most newfile system allow a maximum length of 255 characters (the default). Older filesystems allow a maximum file name length of only 14 characters.

  • 7/29/2019 Fundamentals of Unix - For Freshers

    49/299

    49

    Listing files and directories

    Following are some of the file types:

    - (hyphen) for ordinary files

    b for block-special files

    c for character-special files

    d for directories

    l for symbolic links

    p for pipe-special files (first in, first out)

    s for local sockets

  • 7/29/2019 Fundamentals of Unix - For Freshers

    50/299

    50

    How do I create files?

    vi touch

    I/O redirection

    Examples:$ ls -Rl / > allfiles$ cat test.c > copytest.c

    echo

    Examples:$ echo testing > file

    ln

  • 7/29/2019 Fundamentals of Unix - For Freshers

    51/299

    51

    touch Create a file

    Syntaxtouch[options] [date_time] file

    touch[options] [-t time] file

    Common Options

    -a change the access time of the file (SVR4 only)

    -c dont create the file if it doesnt already exist

    -f force the touch, regardless of read/write permissions

    -m change the modification time of the file (SVR4 only)

    -t time use the time specified, not the current time (SVR4 only)

    When setting the "-t time" option it should be in the form:

    [[CC]YY]MMDDhhmm[.SS]

    Examples

    To create a file:

    $ touch filename

  • 7/29/2019 Fundamentals of Unix - For Freshers

    52/299

    52

    lscat

    morelesshead

    tailwcuniqlpcp

    mv

    mvlnrmfilestrings

    Look at the characteristics of a file

    Look at the contents of a file

    Look at the contents of a file, one screenful at a time

    more versatile paginate than more

    display the first few lines of a file

    displays the last part of a filedisplay a count of lines, words and characters in a file

    remove duplicatelinesPrint a file

    Make a copy of a file

    Change the name of a file or directory

    Move a file to another directory

    Create another name for a file

    Remove a file

    determine the type of file

    to search a binary file for printable, ASCII characters

    What Can We Do with Files?

  • 7/29/2019 Fundamentals of Unix - For Freshers

    53/299

    53

    Listing files and directories

    1. Number of 512-byte blocks used by files in this directory.

    2. Number hard of links to each file.

    3. User name of the file's owner.

    4. Group to which the file belongs.

    5. Number of bytes in the file.

    6. Date and time the file was created or last modified.

    7. Name of the file or directory.

    Examples:$$ls -ltotal 4 [1] [2] [3] [4] [5] [6] [7]

    -rw-r--r-- 1 larry system 101 Jun 5 10:03 file1

    -rw-r--r-- 1 larry system 75 Jun 5 10:03 file2

    -rw-r--r-- 1 larry system 75 Jun 5 10:03 file2

    -rw-r--r-- 1 larry system 65 Jun 5 10:06 file3drwxr-xr-x 2 larry system 32 Jun 5 10:07 project

  • 7/29/2019 Fundamentals of Unix - For Freshers

    54/299

    54

    cat [file...] Concatenate and display the contents of file(s)

    $ cat remind

    Your mother's birthday is November 29.$ cat note remindTO: Mike SmithThe meeting is scheduled for July 29.Your mother's birthday is November 29.$ catabc

    1234

    abc1234 CTRL + D

    cat Display the Contents of a File

  • 7/29/2019 Fundamentals of Unix - For Freshers

    55/299

    55

    more [filename]... Display files one screen at a time

    $ more funfile...

    --funfile (20%)--Q or q

    Return

    Space

    more Display the Contents of a File

  • 7/29/2019 Fundamentals of Unix - For Freshers

    56/299

    56

    less - opposite of more

    less [filename]... less is a more functional clone of more provided bythe GNU project .One added feature of less is that it allows you to scroll backand forth in a file one line at a time with the arrow keys. less reads filesincrementally, rather than loading each file into memory like more, so lessuses less memory.

    $ less funfile...

    :Q or q

  • 7/29/2019 Fundamentals of Unix - For Freshers

    57/299

    57

    head [-n] [filename]... display first few lines of file(s)

    $ head /var/log/messages

    $ head -12 /var/log/messages

    head Display the End of a File

  • 7/29/2019 Fundamentals of Unix - For Freshers

    58/299

    58

    tail [-n] [filename]... Display the end of file(s)

    $ tail -1 notesoon as it is available.$ tail f [filename]... tracks the file, printing out every new line as it is added.

    tail Display the End of a File

  • 7/29/2019 Fundamentals of Unix - For Freshers

    59/299

    59

    wc [OPTION]. . . [FILE]. . .

    -c count bytes-m count characters (SVR4)-l count lines-w count words

    If no options are specified it defaults to "-lwc".

    $wc l /etc/passwd

    wc Print the number of bytes, words, and lines in files

  • 7/29/2019 Fundamentals of Unix - For Freshers

    60/299

    60

    uniq - remove duplicate lines

    uniq[options] [+|-n] file [file.new]

    d one copy of only the repeated lines

    -u select only the lines not repeated+n ignore the first n characters-s n same as above (SVR4 only)-n skip the first n fields, including any blanks (space &tab)-f fields same as above (SVR4 only)

    $uniq3 /etc/passwd

    The -n option of the uniqcommand is used to skip the first 3 fields in file, and filter outlines which are duplicates from the 4th field onward.

  • 7/29/2019 Fundamentals of Unix - For Freshers

    61/299

    61

    cp [-i] file1 new_file Copy a filecp [-i] file [file...] dest_dir Copy files to a directorycp -r [-i] dir [dir...] dest_dirCopy directories

    $ ls -Ff1 f2* memo/ note remind$ cp f1 f1.copy$ ls -F

    f1 f1.copy f2* memo/ note remind$ cp note remind memo$ ls -F memonote remind

    cp Copy Files

  • 7/29/2019 Fundamentals of Unix - For Freshers

    62/299

    62

    mv [-i] file new_file Rename a filemv [-i] file [file...] dest_dir Move files to a directorymv [-i] dir [dir...] dest_dir Rename or move directories

    $ ls -F $ mv note remind memof1 f2* memo/ note remind $ ls -F$ mv f1 file1 file1 memo/$ ls -F $ls -F memo

    file1 f2* memo/ note remind file2* note remind$ mv f2 memo/file2 $ mv memo letters$ ls -F $ ls -Ffile1 memo/ note remind file1 letters/$ ls -F memofile2*

    mv Move or Rename Files

  • 7/29/2019 Fundamentals of Unix - For Freshers

    63/299

    63

    cmp compare file contents

    Syntax

    cmp[options] file1 file2 [skip1] [skip2]

    Common Options

    -l report on each difference

    -s report exit status only, not byte differences

    Examples

    Given the files file1 and file2

    apple orange

    grape mango

    red red

    blue blue

    green yellow

    The comparison of the two files yields:

    $ cmp file1 file2

    The default it to report only the first difference found.

  • 7/29/2019 Fundamentals of Unix - For Freshers

    64/299

    64

    diff reports all differences between files

    Syntax

    diff [options] file1 file2

    Common Options

    -b ignore trailing blanks

    -i ignore the case of letters

    -w ignore and characters

    -e produce an output formatted for use with the editor, ed

    -r apply diff recursively through common sub-directories

    Examples

    For the mon.logins and tues.logins files above, the difference between them is given by:

    $ diff file1 file2

    The output lists the differences as well as in which file the difference exists. Lines in thefirst file are preceded by "< ", and those in the second file are preceded by "> ".

  • 7/29/2019 Fundamentals of Unix - For Freshers

    65/299

    65

    rm [-if] filename [filename...] Remove filesrm -r[if] dirname [filename...] Remove directories

    $ ls -Ff1 f2 fruit/ memo/$ rm f1$ ls -Ff2 fruit/ memo/

    $ rm -i f2f2?

  • 7/29/2019 Fundamentals of Unix - For Freshers

    66/299

    66

    $ ls -l f1

    -rw-rw-r-- 1 user3 class 37 Jul 24 11:06 f1

    $ ln f1 /home/user2/f1.link

    $ ls -l f1

    -rw-rw-r-- 2 user3 class 37 Jul 24 11:06 f1

    $ ls -l /home/user2

    -rw-rw-r-- 2 user3 class 37 Jul 24 11:06 f1.link

    $ ls -i f1 /home/user2/f1.link

    1789 f1 1789 /home/user2/f1.link

    ln file new_file Link to a fileln file [file ...] dest_dir Link files to a directory

    /

    home

    user3

    f1

    user2

    f1.link

    You are here.

    This is a samplefile to be copied.

    ln Link Files

  • 7/29/2019 Fundamentals of Unix - For Freshers

    67/299

    67

    Hard Links

    Directory/mydir

    f1 101

    file1 101

    ln /mydir/f1 /mydir/file1

    101 -rwxr-xr-x

    Inode Table Data Blocks

  • 7/29/2019 Fundamentals of Unix - For Freshers

    68/299

    68

    Symbolic Links

    Directory/mydir

    f2 102

    file2 103

    Inode Table

    102 rwxr-xr-x

    103 link to /mydir/f2

    Data Blocks

    ln -s /mydir/f2 /mydir/file2

  • 7/29/2019 Fundamentals of Unix - For Freshers

    69/299

    69

    Hard Links and Soft Links

    There are two kinds of links available for your use:

    hard links

    soft, or symbolic, links

    When you create a hard link, you are providing another name for the same file.Hard links let you link only files in the same file system. All the link names are onequal footing. It is incorrect to think of one file name as the real name, andanother as only a link.

  • 7/29/2019 Fundamentals of Unix - For Freshers

    70/299

    70

    Hard Links and Soft Links

    Soft links or symbolic links let you link both files and directories.

    In addition, you may link both files and directories across different file systems.

    A symbolic link is actually a distinct file that contains a pointer to another file ordirectory. This pointer is the pathname to the destination file or directory. Only theoriginal file name is the real name of the file or directory. Unlike a hard link, a softlink is actually only a link.

  • 7/29/2019 Fundamentals of Unix - For Freshers

    71/299

    71

    Creating Links

    To link a hard link, use the following command format:

    ln

    If you want to link files and directories across file systems, you can createsymbolic links.

    To create a symbolic link, add an flag to the above command and specify fullpathnames of both files.

  • 7/29/2019 Fundamentals of Unix - For Freshers

    72/299

    72

    Understanding Links

    Each file has a unique identification, called an I-node number. The I-nodenumber refers to files data stored at a particular location - rather than to the filename.

    A directory entry is a link between an I-node number and a file name. This linkenables you to link multiple file names to the same I-node number.

    To display I-node number of files, use with the flag.

  • 7/29/2019 Fundamentals of Unix - For Freshers

    73/299

    73

    Understanding Links

    Hard links to a file carry the same I-node number. Since an I-node numberrepresents a file within a particular file system, hard links cannot exist between filesystems.

    However, the soft link is a new file with its own, new I-node number.

    Since the soft link refers to the original file by name, rather than by I-nodenumber, they work across file systems.

  • 7/29/2019 Fundamentals of Unix - For Freshers

    74/299

    74

    Deleting Links

    The rm (remove file) command does not always remove a file.

    Assume that a file has several hard links (and therefore more than one file name).In this case, the command removes the link between the file I-node number

    and that file name, but leaves the physical file intact.

    The command physically removes a file only after it has removed the last linkbetween that file and its names.

  • 7/29/2019 Fundamentals of Unix - For Freshers

    75/299

    75

    file determine file type

    Syntax

    file[options] [-m magic_file] [-f file_list] file

    Common Options

    -c check the magic file for errors in format

    -f file_list file_list contains a list of files to examine

    -h dont follow symbolic links (SVR4 only)

    -L follow symbolic links (BSD only)

    -m magic_file use magic_file as the magic file instead of /etc/magic

    Examples

    Below we list the output from the command "file filename" for some representative files.

    /etc/passwd: ascii text

    /usr/bin/ls: ELF 32-bit MSB executable SPARC Version 1

    test.tar: USTAR tar archive

    test.tar.Z: compressed data block compressed 16 bits

  • 7/29/2019 Fundamentals of Unix - For Freshers

    76/299

    76

    ls -lcat

    moretail

    cpmvlnrm

    Display file characteristics

    Concatenate and display contents of files to screen

    Format and display contents of files to screen

    Display the end of files to screen

    Copy files or directoriesMove or rename files or directories

    Link file names together

    Remove files or directories

    File/Directory Manipulation Commands Summary

  • 7/29/2019 Fundamentals of Unix - For Freshers

    77/299

    77

  • 7/29/2019 Fundamentals of Unix - For Freshers

    78/299

    78

    Access to files is dependent on a user's identification and the permissions associated with a file. This module will show

    how to

    Permissions

    ls (11, ls -l)chmodumaskchownchgrpsu

    newgrp

    Understand the read, write, and execute access to a file

    Determine what access is granted on a file

    Change the file access

    Change default file access

    Change the owner of a file

    Change the group of a file

    Switch your user identifier

    Switch your group identifier

    File Permissions and Access

  • 7/29/2019 Fundamentals of Unix - For Freshers

    79/299

    79

    The UNIX system incorporates a three-tier structure to define who has access

    to each file and directory:

    user The owner of the filegroup A group that may have access to the file

    other Everyone else

    The ls -l command displays the owner and group who has access to the file.

    $ ls -l-rw-r--r-- 1 user3 class 37 Jul 24 11:06 f1

    -rwxr-xr-x 1 user3 class 37 Jul 24 11:08 f2drwxr-xr-x 2 user3 class 1024 Jul 24 12:03 memo

    | |owner group

    Who Has Access to a File?

  • 7/29/2019 Fundamentals of Unix - For Freshers

    80/299

    80

    contents can be examined.

    contents can be examined.

    contents can be changed.

    contents can be changed.

    file can be used as a command.can become current working directory.

    There are three types of access for each file and directory:

    Read

    files:

    directories:

    Write

    files:

    directories:

    Execute

    files:directories:

    Types of Access

  • 7/29/2019 Fundamentals of Unix - For Freshers

    81/299

    81

    Permissions are displayed with ls -l:

    $ ls -l

    - rw- r-- r-- 1 user3 class 37 Jul 24 11:06 f1- rwx r-x r-x 1 user3 class 37 Jul 24 11:08 f2d rwx r-x r-x 2 user3 class 1024 Jul 24 12:03 memo

    user (owner)access

    group access

    other access

    file owner

    file group

    Permissions

  • 7/29/2019 Fundamentals of Unix - For Freshers

    82/299

    82

    Permissions

    symbolic r w - r - - r - -

    \ / \ / \ /

    binary 110 100 100

    \ / \ / \ /

    Octal 6 4 4

  • 7/29/2019 Fundamentals of Unix - For Freshers

    83/299

    83

    Default File Permissions

    When you create a file or directory, the system automatically supplies adefault permission. The following is a typical file permission :

    -rw-r--r--

    This permission specifies that the owner has read and write permissions whilethe group and all others have only read permission.

  • 7/29/2019 Fundamentals of Unix - For Freshers

    84/299

    84

    Default File Permissions

    When you create a file or directory, the system automatically supplies adefault permission. The following is typical directory permission :

    drwxr-xr-x

    This permission specifies that the owner has read and write permissions whilethe group and all others have read and execute (search) permission.

  • 7/29/2019 Fundamentals of Unix - For Freshers

    85/299

    85

    Default File Permissions

    The default permission codes that your system provides relieve you from thetask of specifying them explicitly every time you create a file or directory.

    If you want to create your own default permission codes, you must change

    your user mask with the umaskcommand.

  • 7/29/2019 Fundamentals of Unix - For Freshers

    86/299

    86

    Changing File Permissions

    Use the chmod(change mode) command to set or change the permissions foryour files and directories. Please remember that whatever restrictions youimpose, the super-user can always override them.

    There are two ways to specify the permissions forchmod: You can specify permissions with letters and symbols.

    You can specify permissions with octal numbers.

  • 7/29/2019 Fundamentals of Unix - For Freshers

    87/299

    87

    chmodmode_list file... Change permissions of file(s)

    mode_list [who[operator]permission] [ ,... ]

    who user, group, other or all

    operator + (add), - (subtract), = (set equal to)permission read, write, execute

    Original permissions: mode user group otherrw-r--r-- rw- r-- r--

    $ chmod u+x,g+x,o+x file or $ chmod +x fileFinal permissions: mode user group other

    rwxr-xr-x rwx r-x r-x

    chmod Change Permissions of a File

  • 7/29/2019 Fundamentals of Unix - For Freshers

    88/299

    88

    Changing Permissions Using Letters

    The following is the format of the chmod command when using letters andsymbols:

    chmod userclass-operation-permission filename

    The userclass-operation-permission entry represents three codes thatspecify the user class code, operation, and permission code that you want toactivate. The filename entry is the name of the file or files whosepermissions you want to change.

  • 7/29/2019 Fundamentals of Unix - For Freshers

    89/299

    89

    Changing Permissions Using Letters

    Use one or more of the following to define a user class:

    u User (owner)

    g Group

    o All others (besides owner and group)

    a All (user, group, and all others)

  • 7/29/2019 Fundamentals of Unix - For Freshers

    90/299

    90

    Changing Permissions Using Letters

    Use one or more of the following to define an operation:

    + Add permission

    - Remove permission

    = Assign permission regardless of previous setting

  • 7/29/2019 Fundamentals of Unix - For Freshers

    91/299

    91

    Changing Permissions Using Letters

    Use one or more of the following to define a permission:

    r Read

    s Set user or group ID

    w Writex Execute

  • 7/29/2019 Fundamentals of Unix - For Freshers

    92/299

    92

    Changing Permissions Using Letters

    Assume a file file1 that has -rw-r--r set of permissions.

    Now, enter the chmodcommand with the flags go+w.

    This command expands the permissions for both the group (g) and for others(o) by giving them write access (+w) to file1 in addition to the read accessthey already have.

  • 7/29/2019 Fundamentals of Unix - For Freshers

    93/299

    93

    Setting Default Permissions

    If you want to further restrict the permissions established by a program whenit creates a file or directory, you must specify a user mask with the umaskcommand.

    The user mask is a value that determines the access permissions when a fileor directory is created.

  • 7/29/2019 Fundamentals of Unix - For Freshers

    94/299

    94

    Setting Default Permissions

    The umask command has following format:

    umask octal-number

    The octal-number is a 3-digit octal number that specifies the permissions tobe subtracted from default permissions (777 or 666).

    Please note that through umask you actually specify permissions that arenotto be granted.

  • 7/29/2019 Fundamentals of Unix - For Freshers

    95/299

    95

    umask [-S] [mode] User file-creation mode mask

    user group otherdefault permissions: rw- rw- rw-

    set default permissions: rw- r-- ---

    $ umask g=r,o=

    umask Permission Mask

  • 7/29/2019 Fundamentals of Unix - For Freshers

    96/299

    96

    SUID and SGID Files

    It is often the case in Unix that an ordinary user must perform a task whichonly the root has the power to. Since it is not practical to give every user theroot password, setuid and setgid bits are used. A setuid program (or script)assumes the UID of the program owner when it is run (a setgid programassumes the GID of the program group when run). So a setuid root program(or script) has a special bit set such that it can be run by any user to performrootly tasks.

    chmod u+s

    -rwsr--r-- 1 smith user 1178 Feb 29 15:17 file

    chmod g+sfile

    -rwxr-sr-- 1 smith user 1178 Feb 29 15:17 file

  • 7/29/2019 Fundamentals of Unix - For Freshers

    97/299

    97

    What's the sticky bit?

    In the old days when machines did not have very much memory, the sticky bitwas used to force a program or file to remain in memory. Today, the sticky bitis largely used on publicly writable directories to ensure that users do notoverwrite each other's files.

    When the sticky bit (t) is turned on for a directory users can have read and/or

    write permissions for that directory, but they can only remove or rename filesthat they own.

    Only root is permitted to turn the sticky bit on or off.

    chmod +t/test

    drwxrwxrwt 5 sys sys 543 May 29 09:41 tmp

  • 7/29/2019 Fundamentals of Unix - For Freshers

    98/299

    98

    touch [-amc] file... update access and/or modification times of file

    $ ll-rw-r--r-- 1 karenk users 25936 Aug 24 09:53 firstfile

    -rw-r--r-- 1 karenk users 10245 Aug 24 09:53 secondfile$ touch newfile$ ll-rw-r--r-- 1 karenk users 25936 Aug 24 09:53 firstfile-rw-r--r-- 1 karenk users 0 Aug 25 10:02 newfile-rw-r--r-- 1 karenk users 10245 Aug 24 09:53 secondfile$ touch secondfile$ ll

    -rw-r--r-- 1 karenk users 25936 Aug 24 09:53 firstfile-rw-r--r-- 1 karenk users 0 Aug 25 10:02 newfile-rw-r--r-- 1 karenk users 10245 Aug 25 10:05 secondfile$

    touch Update Timestamp on File

  • 7/29/2019 Fundamentals of Unix - For Freshers

    99/299

    99

    chown owner [:group] filename ...

    $ iduid=303 (user3), gid=300 (class)$ cp f1 /tmp/user2/f1$ ls -l /tmp/user2/f1-rw-r----- 1 user3 class 3967 Jan 24 13:13 f1$ chown user2 /tmp/user2/f1$ ls -l /tmp/user2/f1

    -rw-r----- 1 user2 class 3967 Jan 24 13:13 f1

    Only the owner of a file (or root) can change the ownership of the file.

    Changes owner of a file(s) and,

    optionally, the group ID

    chown Change File Ownership

  • 7/29/2019 Fundamentals of Unix - For Freshers

    100/299

    10

    chgrp newgroup filename ...

    $ iduid=303 (user3), gid=300 (class)$ ls -l f3-rw-r----- 1 user3 class 3967 Jan 24 13:13 f3$ chgrp class2 f3$ ls -l f3-rw-r----- 1 user3 class2 3967 Jan 24 13:13 f3

    $ chown user2 f3$ ls -l f3-rw-r----- 1 user2 class2 3967 Jan 24 13:13 f3$

    Changes group access to a file

    Only the owner of a file (or root) can change the group

    of the file.

    The chgrp Command

  • 7/29/2019 Fundamentals of Unix - For Freshers

    101/299

    10

    su [user_name]

    $ ls -l /usr/local/bin/class_setup-rwxr-x--- 1 class_admin teacher 3967 Jan 24 13:13 class_setup$ id

    uid=303 (user3), gid=300 (class)$ su class_adminPassword:$ iduid=400 (class_admin), gid=300 (class)$ /usr/local/bin/class_setup$

    log out of su session$

    CTRL + D

    Change your user ID and group ID designation

    su Switch User Id

  • 7/29/2019 Fundamentals of Unix - For Freshers

    102/299

    10

    newgrp [group_name] Changes the group ID

    $ ls -l /usr/local/bin/class_setup-rwxr-x--- 1 class_admin teacher 3967 Jan 24 13:13 class_setup$ iduid=303 (user3) gid=300 (class)$ newgrp teacher$ iduid=303 (user3) gid=33 (teacher)$ /usr/local/bin/class_setup$ newgrpreturn to login group status$ newgrp otherSorry$

    The newgrp Command

  • 7/29/2019 Fundamentals of Unix - For Freshers

    103/299

    10

    Access Control Lists

    lsacl filename list the ACL for a filechacl ACL filename change the ACL for a file

    $ lsacl funfile(user3.%,rw-)(%.class,r--)(%.%,r--) funfile$ chacl "user2.class=rw,%.%-r" funfile$ lsacl funfile(user2.class,rw-)(user3.%,rw-)(%.class,r--)(%.%,---) funfile$ chacl -d "user2.class" funfile$ ll funfile

    rw-r----- 1 user3 class 3081 May 28 16:12 funfile$ lsacl funfile(user3.%,rw-)(%.class,r--)(%.%,---) funfile

    S

  • 7/29/2019 Fundamentals of Unix - For Freshers

    104/299

    10

    Permissions

    chmodumask

    chownchgrpsunewgrp

    Define who has what access to a file

    user, group, others

    read, write, execute

    Change the permissions of a file

    Define the default permissions for new files

    Change the owner of a file

    Change the group of a file

    Switch user ID

    Switch group ID

    File Permissions and Access Summary

  • 7/29/2019 Fundamentals of Unix - For Freshers

    105/299

    10

    vi

    Cursor Control Commands

  • 7/29/2019 Fundamentals of Unix - For Freshers

    106/299

    10

    Cursor Control Commands

    move up one line

    line move one character to the left

    line move one character to the right

    move down one line

    beginning of previous word

    beginning of next word

    end of current/next word

    beginning of line

    end of line

    beginning of current/previous sentence

    beginning of next sentence

    beginning of current/previous paragraph

    end of current paragraph

    top line on screen

    middle line on screen

    bottom line on screen

    Input modes

  • 7/29/2019 Fundamentals of Unix - For Freshers

    107/299

    10

    Input modes

    Command Mode Name Insertion Point

    ---------------------------------------------------------------------------------------

    a append just after the current character

    A append end of the current line

    i insert just before the current character

    I Insert beginning of the current line

    o open new line below the current line

    O open new line above the current line

    Deletion commands

  • 7/29/2019 Fundamentals of Unix - For Freshers

    108/299

    10

    Deletion commands

    Command Function

    x Delete only the current character

    D Delete to the end of the line

    db Delete from the current character to the beginning of the

    current wordde Delete from the current character to the end of the current

    word

    dd Delete the current line

    dw Delete from the current character to the beginning of the next

    word

    u undo the last change

    Using the vi editor

  • 7/29/2019 Fundamentals of Unix - For Freshers

    109/299

    10

    Using the vi editor

    Start the vi program by entering following command:

    This is a new file, so the system responds by putting your cursor at the top of ascreen.

    Examples:

    $ vi file1 [Return]

    Using the vi editor

  • 7/29/2019 Fundamentals of Unix - For Freshers

    110/299

    11

    Using the vi editor

    Type the lowercase letter i to specify that you want to insert text to thenew file. The system does not display the i that you enter.

    Enter following text, pressing Return key after each line:

    You start the vi program by entering [Return]

    the vi command optionally followed by the name [Return]

    of a new or existing file.

    [Escape]

    Using the vi editor

  • 7/29/2019 Fundamentals of Unix - For Freshers

    111/299

    11

    Using the vi editor

    Press the Escape key to indicate that you have finished your currentwork.

    Type a colon (:) to enter the Last Line mode. The cursor will beplaced on the last line of the screen, where you can enter thecommands recognized by the vi editor.

    Enter lowercase letter next. This indicates to the system that youwant to write, or save, a copy of the new file in your current directory.

    Using vi editor

  • 7/29/2019 Fundamentals of Unix - For Freshers

    112/299

    11

    Using vi editor

    Use dd command to delete current line.

    Use dd command to delete lines beginning from the current line.

    Use dw command to delete current word.

    Use dw command to delete words beginning from the current word.

    Using vi editor

  • 7/29/2019 Fundamentals of Unix - For Freshers

    113/299

    11

    Using vi editor

    Use ^ command to move to beginning of current line.

    Use $ command to move to END of current line.

    Use w command to move forward by word.

    Use b command to move back by word.

    Use e command to move to end of word.

    Using vi editor

  • 7/29/2019 Fundamentals of Unix - For Freshers

    114/299

    11

    Using vi editor

    Use Ctrl-F command to move to next screen.

    Use Ctrl-D command to move forward by half a screen.

    Use Ctrl-B command to move to previous screen.

    Position cursor to any opening bracket and press % key to see the matchingclosing bracket.

    Press A command to add data to end of current line.

    Using vi editor

  • 7/29/2019 Fundamentals of Unix - For Freshers

    115/299

    11

    Using vi editor

    Use yy command to copy current line.

    Use yy command to copy lines from the current line.

    Use p to paste the lines cut using the yy or the dd command. The linesare pasted AFTER the current line.

    Use P to paste the lines cut using the yy or the dd command. The linesare pasted BEFORE the current line.

    Using vi editor

  • 7/29/2019 Fundamentals of Unix - For Freshers

    116/299

    11

    Using vi editor

    Use . command to repeat the last add, update, delete or paste command.

    Use x command to delete current character.

    Use x command to delete characters beginning from the currentcharacter.

    Use r command to replace current character with character.

    Using vi editor

  • 7/29/2019 Fundamentals of Unix - For Freshers

    117/299

    11

    Using vi editor

    Use cw command to change current word with new word(s). Enter newwords and press ESC when done.

    Use cw command to change words beginning with current wordwith new word(s). Enter new words and press ESC when done.

    Use C command to replace remainder of the line. Enter new text andpress ESC when done.

    Use G to move to end of file.

    Using vi editor

  • 7/29/2019 Fundamentals of Unix - For Freshers

    118/299

    11

    Using vi editor

    Use :/string command to search the string.

    Use n command to repeat the previous search.

    Use :s/old_string/new_string command to substitute old_string withnew_string in the current line.

    Use :,s/old_string/new_string command to substitute old_stringwith new_string in line m thru n.For first line set m to 1. For last line,

    set n to $.

    Using vi editor

  • 7/29/2019 Fundamentals of Unix - For Freshers

    119/299

    11

    Using vi editor

    Use :set nu command to see line numbers.

    Use :set nonu command to remove line numbers.

    Use :set ai to enable auto-indent feature.

    Use :set noai to remove auto-indent feature.

    Use :set sm (show match) to enable bracket-match feature.

    Quitting the vi editor

  • 7/29/2019 Fundamentals of Unix - For Freshers

    120/299

    12

    Quitting the vi editor

    To save your work and quit:

    Press the Escape key to indicate that you have finished your current work.

    Type a colon (:) to enter the Last Line mode. Enter command .

    This indicates to the system that you want to write, or save, a copy of the newfile in your current directory and quit the vi editor.

    Quitting the vi editor

  • 7/29/2019 Fundamentals of Unix - For Freshers

    121/299

    12

    Quitting the vi editor

    To quit without saving your work :

    Press the Escape key to indicate that you have finished your current work.

    Type a colon (:) to enter the Last Line mode. Enter command .

    This indicates to the system that you want quit the vi editor WITHOUT saving thechanges you made to the file SINCE THE LAST w command.

    Searching for Text: /, n, N

  • 7/29/2019 Fundamentals of Unix - For Freshers

    122/299

    12

    There is one here

    and one more here

    and yet one more

    but not this

    nor this One

    next

    previous

    n

    N

    g

    Configuring the vi session

  • 7/29/2019 Fundamentals of Unix - For Freshers

    123/299

    12

    Configuring the vi session

    To configure the vienvironment certain options can be set with the line editor command

    :set during a viediting session.:set ignorecase ignore the case of a character in a search

    :set list display tabs and carriage returns

    :set nolist turn off list option

    :set nonumber turn off line numbers

    :set showmode display indication that insert mode is on

    Alternatively, frequently used options can be set automatically when viis invoked, byuse of the .exrc file :set all display all option settings

    The following is a sample .exrc file:

    set number

    set list

    set ignorecase

    vi Quick Reference

  • 7/29/2019 Fundamentals of Unix - For Freshers

    124/299

    12

    Cursor Movement Commands:

    (n) indicates a number, and is optional

    (n)h left (n) space(s)

    (n)j down (n) space(s)

    (n)k up (n) space(s)

    (n)l right (n) space(s)

    (The arrow keys usually work also)

    ^F forward one screen

    ^B back one screen

    ^D down half screen

    ^U up half screen

    H beginning of top line of screen

    M beginning of middle line of screen

    L beginning of last line of screen

    G beginning of last line of file

    0 (zero) beginning of line

    $ end of line

    (n)w forward (n) word(s)

    (n)b back (n) word(s)

    Inserting Text:

    i insert text before the cursor

    a append text after the cursor (does not

    overwrite other text)

    I insert text at the beginning of the line

    A append text to the end of the line

    r replace the character under the cursor

    with the next character typedR Overwrite characters until the end of the

    line (or until escape is pressed to changecommand)

    o (alpha o) open new line after the current

    line to type text

    O (alpha O) open new line before the current lineto type text

    vi Quick Reference

  • 7/29/2019 Fundamentals of Unix - For Freshers

    125/299

    12

    Change Commands:

    (n)cc changes (n) characters on line(s) until end of the line (or until escape is pressed)

    cw changes characters of word until end of the word (or until escape is pressed)

    (n)cw changes characters of the next (n) words

    c$ changes text to the end of the line

    C changes remaining text on the current line (until stopped by escape key)

    ~ changes the case of the current character

    Jjoins the current line and the next line

    u undo the last command just done on this line

    . repeats last change

    s substitutes text for current character

    S substitutes text for current line

    :s substitutes new word(s) for old

    : s/old/new/g

    & repeats last substitution (:s) command.

    (n)yy yanks (n) lines to buffer

    y(n)w yanks (n) words to buffer

    p puts yanked or deleted text after cursor

    P puts yanked or deleted text before cursor

    vi Quick Reference

  • 7/29/2019 Fundamentals of Unix - For Freshers

    126/299

    12

    File Manipulation:

    :w (file) writes changes to file (default is current file)

    :wq writes changes to current file and quits edit session

    :w! (file) overwrites file (default is current file)

    :q quits edit session w/no changes made

    :q! quits edit session and discards changes

    :n edits next file in argument list

    :f (name) changes name of current file to (name)

    :r (file) reads contents of file into current edit at the current cursor position (insert a file)

    :!(command) shell escape

    :r!(command) inserts result of shell command

    vi editor - global replace

  • 7/29/2019 Fundamentals of Unix - For Freshers

    127/299

    12

    g p

    Use :s/old_string/new_string command to substitute old_string withnew_string in the current line.

    Use :,s/old_string/new_string command to substitute old_stringwith new_string in line m thru n.For first line set m to 1. For last line,set n to $.

    Use :g/ old_string/s//new_string command to replace old_string withnew_string in the file.

    The grep Command

  • 7/29/2019 Fundamentals of Unix - For Freshers

    128/299

    12

    The name of the grep command is an acronym forglobal regularexpression printer.

    Basic form : grep

    Grep can take input from file, standard input or a pipe ( output of anothercommand )

    Options :-c : Prints the COUNT of number of matching lines

    -v : Prints line that DO NOT match

    -l : prints the names of files with matching pattern

    -n : Prints line number with each matching output

    e.g., grep -i error logfile

    Matching filenames

  • 7/29/2019 Fundamentals of Unix - For Freshers

    129/299

    12

    If the file names have a common pattern, the shell can match that pattern,generate a list of those names, and automatically pass them to thecommand as arguments.

    The asterisk (*), sometimes referred to as a wildcard , matches any string

    of characters.

    Examples:

    $ ls file *

    Matching filenames

  • 7/29/2019 Fundamentals of Unix - For Freshers

    130/299

    13

    If the file names have a common pattern, the shell can match that pattern,generate a list of those names, and automatically pass them to thecommand as arguments.

    The asterisk (*), sometimes referred to as a wildcard , matches any string

    of characters. For example, file* will match any filename starting withword file.

    Matching filenames

  • 7/29/2019 Fundamentals of Unix - For Freshers

    131/299

    13

    There is one exception to the general rules for pattern matching. When thefirst character of a file name is a period, you must match the periodexplicitly.

    For example, displays the names of all files in the current directory

    except those that begin with a period. The command displays all filenames, including those that begin with a period.

    Matching filenames

  • 7/29/2019 Fundamentals of Unix - For Freshers

    132/299

    13

    This restriction prevents the shell from automatically matching the relativedirectory names. These are . (for the current directory) and .. (for the parentdirectory).

    In addition to the asterisk (*), shells provide other ways to match character

    patterns. These are summarized in the next slide.

    Matching filenames

  • 7/29/2019 Fundamentals of Unix - For Freshers

    133/299

    13

    The filename pattern-matching characters are:

    * - matches any string, including NULL

    ? - matches any single character

    [] - matches any of the set characters[.-.] - matches any character within the

    specified range

    [!] - matches any character except those

    in the set

    New Topic

  • 7/29/2019 Fundamentals of Unix - For Freshers

    134/299

    13

    Using Regular Expressions

    Regular Expressions

  • 7/29/2019 Fundamentals of Unix - For Freshers

    135/299

    13

    A regular expression specifies a set of strings to be matched. It containsordinary text characters and operator characters. Ordinary characters matchthe corresponding characters in the strings being compared. Operatorcharacters specify repetitions, choices, and other features.

    Regular expressions fall into two groups:

    Basic regular expressions

    Extended regular expressions

    Basic Regular Expressions

  • 7/29/2019 Fundamentals of Unix - For Freshers

    136/299

    13

    Basic expressions are built by concatenating simpler basic regularexpressions.

    Ordinary characters and operator characters together make up the set ofsimple basic regular expressions. You can concatenate any number or

    combination of simple expressions to create a compound expression that willmatch any sequence of characters that corresponds to the concatenatedsimple expressions.

    Basic Regular Expressions - Rules

  • 7/29/2019 Fundamentals of Unix - For Freshers

    137/299

    13

    An ordinary character matches itself.

    A period (dot) matches any single characterexcept the newline character.

    An * (asterisk) matches any number of occurrences of the preceding simpleexpression, including none.

    Basic Regular Expressions - Rules

  • 7/29/2019 Fundamentals of Unix - For Freshers

    138/299

    13

    The Bracket expression) matches a single instance of any one ofthe characters within the brackets. Ranges of characters can be abbreviatedby using a hyphen. For example, [0-9a-z] matches any single digit orlowercase letter.

    Within brackets, all characters are ordinary characters except the hyphen(when used in a range abbreviation) and the circumflex (when used as thefirst character inside the brackets).

  • 7/29/2019 Fundamentals of Unix - For Freshers

    139/299

    Basic Regular Expressions - Rules

  • 7/29/2019 Fundamentals of Unix - For Freshers

    140/299

    14

    The \char (Backslash) - except within a bracket expression, escapes the nextcharacter to permit matching on explicit instances of characters that areusually basic regular expression operators.

    The exprexpr... (concatenation) - matches any string that matches all of the

    concatenated expressions in sequence.

  • 7/29/2019 Fundamentals of Unix - For Freshers

    141/299

    14

  • 7/29/2019 Fundamentals of Unix - For Freshers

    142/299

    14

    Commonly Used Shells

  • 7/29/2019 Fundamentals of Unix - For Freshers

    143/299

    14

    /usr/bin/sh/usr/bin/ksh/usr/old/bin/sh/usr/bin/csh/usr/bin/keysh/usr/bin/rksh/usr/bin/rsh/bin/bash

    POSIX shell

    Korn shell

    Bourne shell

    C Shell

    A context-sensitive softkey shell

    Restricted Korn shell

    Restricted Bourne shell

    Bash shell (GNU Bourne-Again Shell)

    POSIX Shell Features

  • 7/29/2019 Fundamentals of Unix - For Freshers

    144/299

    14

    A shell user interface with some advanced features: Command aliasing

    File name completion

    Command history mechanism

    Command line recall and editing

    Job control

    Enhanced cd capabilities

    Advanced programming capabilities

    Aliasing

  • 7/29/2019 Fundamentals of Unix - For Freshers

    145/299

    14

    alias [name[=string]]

    $ alias dir=ls$ alias mroe=more$ alias mstat=/home/tricia/projects/micron/status$ alias laser="lp -dlaser"$ laser fileXrequest id is laser-234 (1 file)$ alias $ alias mroe mroe=more

    File Name Completion

  • 7/29/2019 Fundamentals of Unix - For Freshers

    146/299

    14

    $ more fra$ more frankenstein

    .

    .

    .

    $ more abc$ more abcdef =

    1)abcdefXlmnop2)abcdefYlmnop

    $ more abcdefThen type X or Y, then .

    Associated file name will be completed

    Return

    ESCESC

    ESCESC

    ESCESC

    ESC

    Recalling Commands

  • 7/29/2019 Fundamentals of Unix - For Freshers

    147/299

    14

    Uses the history mechanism. Must have the EDITORenvironment variable set.

    EDITOR=viexport EDITOR

    At $, press and use normal vi commands to scroll through previous commands. k scrolls backward through the command history.

    j scrolls forward through the command history. nGtakes you to command number n.

    Press to execute the command.

    Return

    Esc

    Command Line Editing

  • 7/29/2019 Fundamentals of Unix - For Freshers

    148/299

    14

    Provides the ability to modify text entered on current or previous command lines.

    Press to enter command mode.

    Recall desired command by either

    Pressing until it appears

    Typing the command number, then G Use vi commands to edit the line

    Do not use the arrow keys

    Enter to execute the modified command.

    K

    Return

    Esc

    The User Environment

  • 7/29/2019 Fundamentals of Unix - For Freshers

    149/299

    14

    env

    $ envHOME=/home/gerryPWD=/home/gerry/develop/basicsEDITOR=viTERM=70092...PATH=/usr/bin:/usr/contrib/bin:/usr/local/bin:\/home/gerry/bin

    Your environment describes your session to the programs you run.

    Setting Shell Variables

  • 7/29/2019 Fundamentals of Unix - For Freshers

    150/299

    15

    A shell variable is a name that represents a value. The value associated with the name can be modified.

    Some shell variables are defined during the login process.

    A user can define new shell variables.

    name=value

    $ PATH=/usr/bin/X11:/usr/bin$ PS1=

    Two Important Variables

  • 7/29/2019 Fundamentals of Unix - For Freshers

    151/299

    15

    The PATH variable A list of directories where the shell will search for the commands you type

    The TERMvariable

    Describes your terminal type and screen size to the programs you run

    $ env

    ...PATH=/usr/bin:/usr/contrib/bin:/usr/local/bin$ TERM=70092$$ tsetErase is BackspaceKill is Ctrl-U$

    echo Displaying the shell Variable values

  • 7/29/2019 Fundamentals of Unix - For Freshers

    152/299

    15

    There are a number of variables automatically set by the shell when it starts.

    echo

    $SHELL:Returns the name of the shell

    $? : the exit status of the last command executed

    $$ : the process number (PID) of this shell - useful for including in

    filenames, to make them unique$! : the process id of the last command run in the background.

    $- : the current options supplied to this invocation of the shell.

    $* : a string containing all the arguments to the shell, starting at $1.

    What Happens When a User Logs In?

  • 7/29/2019 Fundamentals of Unix - For Freshers

    153/299

    15

    csh login scripts

    /etc/csh.login

    $HOME/.profile

    $HOME/.kshrc

    login: test12

    password:

    system login

    script

    local login

    script

    local

    $

    additional

    login script

    $HOME/.login

    $HOME/.cshrc

    POSIX login scripts

    /etc/profile

    The Shell Startup Files

  • 7/29/2019 Fundamentals of Unix - For Freshers

    154/299

    15

    .profile

    .kshrc

    .profile

    .profile

    .kshrc

    .profile

    .login

    .cshrc

    Korn (/usr/bin/ksh)

    Bourne (/usr/old/bin/sh)

    POSIX (/usr/bin/sh)

    Restricted (/usr/bin/rsh,/usr/bin/rksh)

    C (/usr/bin/csh)

    Shell Intrinsics versus UNIX Commands

  • 7/29/2019 Fundamentals of Unix - For Freshers

    155/299

    15

    Shell intrinsics are built into the shell.

    setwhileiffor

    cdls

    UNIX commands live in /usr/bin.

    more

    file

    The system locates UNIX commands by using the PATHvariable.

    Looking for Commands whereis

  • 7/29/2019 Fundamentals of Unix - For Freshers

    156/299

    15

    $ whereis [-b|-m|-s] command Searches a list ofdirectories for a command

    $ whereis ifif :$$ whereis lsls : /sbin/ls /usr/bin/ls /usr/share/man/man1.Z/ls.1$$ whereis cd

    cd : /usr/bin/cd /usr/share/man/man1.Z/cd.1$$ whereis holdyourhorsesholdyourhorses :$

    Commonly Used Shells

  • 7/29/2019 Fundamentals of Unix - For Freshers

    157/299

    15

    /usr/bin/sh/usr/bin/ksh/usr/old/bin/sh/usr/bin/csh/usr/bin/keysh/usr/bin/rksh

    /usr/bin/rsh

    POSIX shellKorn shell

    Bourne shell

    C Shell

    A context-sensitive softkey shell

    Restricted Korn shell

    Restricted Bourne shell

  • 7/29/2019 Fundamentals of Unix - For Freshers

    158/299

    Shell Substitution Capabilities

  • 7/29/2019 Fundamentals of Unix - For Freshers

    159/299

    15

    There are three types of substitution in the shell: Variable substitution

    Command substitution

    Tilde substitution

    Variable Substitution

  • 7/29/2019 Fundamentals of Unix - For Freshers

    160/299

    16

    $name Directs the shell to perform variable substitution

    $ echo $PATH/usr/bin:/usr/contrib/bin:/usr/local/bin

    $ PATH=$PATH:$HOME:.$ echo $PATH/usr/bin:/usr/contrib/bin:/usr/local/bin:/home/user3:.$ echo $HOME/home/user3$ file_name=$HOME/file1$ more $file_name

    Command Substitution

  • 7/29/2019 Fundamentals of Unix - For Freshers

    161/299

    16

    $(command)

    $ pwd/home/user2

    $ curdir=banner $(ls)$ echo $curdir/home/user2$ cd /tmp$ pwd/tmp

    $ cd $curdir$ pwd/home/user2

  • 7/29/2019 Fundamentals of Unix - For Freshers

    162/299

  • 7/29/2019 Fundamentals of Unix - For Freshers

    163/299

  • 7/29/2019 Fundamentals of Unix - For Freshers

    164/299

    Passing Variables to an Application

  • 7/29/2019 Fundamentals of Unix - For Freshers

    165/299

    16

    env var

    TERM=98550

    STEP 1:

    env var

    TERM=98550

    local variables

    color=lavender

    local variables

    color=lavender

    fork: program and

    data spaces

    are duplicated

    local variables

    parent sleeps $

    env var

    TERM=98550

    env var

    TERM=98550

    local variables

    color=lavender

    STEP 2:are replaced with program and data

    of requested program (/usr/bin/vi) and

    program is executed. When program

    (/usr/bin/sh) and the prompt is displayed.completes, control returns to the parent

    program and local data spaceexec:

    Monitoring Processes

  • 7/29/2019 Fundamentals of Unix - For Freshers

    166/299

    16

    $ ps -fUID PID PPID C STIME TTY TIME COMMAND

    user3 4702 1 1 08:46:40 ttyp4 0:00 -shuser3 4895 4702 18 09:55:10 ttyp4 0:00 ps -f

    $ ksh

    $ ps -fUID PID PPID C STIME TTY TIME COMMANDuser3 4702 1 0 08:46:40 ttyp4 0:00 -shuser3 4896 4702 1 09:57:20 ttyp4 0:00 kshuser3 4898 4896 18 09:57:26 ttyp4 0:00 ps -f

    $ exec ps -fUID PID PPID C STIME TTY TIME COMMAND

    user3 4702 1 0 08:46:40 ttyp4 0:00 -sh

    user3 4896 4702 18 09:57:26 ttyp4 0:00 ps -f

    $

    Child Processes and the Environment

  • 7/29/2019 Fundamentals of Unix - For Freshers

    167/299

    16

    $ export color=lavender$ ksh

    $ ps -fUID PID PPID C STIME TTY TIME COMMAND

    user3 4702 1 0 08:46:40 ttyp4 0:00 -shuser3 4896 4702 1 09:57:20 ttyp4 0:00 kshuser3 4898 4896 18 09:57:26 ttyp4 0:00 ps -f

    $ echo $colorlavender$ color=red$ echo $colorred$ exit $ ps -f

    UID PID PPID C STIME TTY TIME COMMAND

    user3 4702 1 0 08:46:40 ttyp4 0:00 -shuser3 4895 4702 1 09:58:20 ttyp4 0:00 ps -f$ echo $colorlavender

  • 7/29/2019 Fundamentals of Unix - For Freshers

    168/299

    16

    Introduction to Quoting

  • 7/29/2019 Fundamentals of Unix - For Freshers

    169/299

    16

    Many characters have "special" meaning to the shell:white space

    carriage return$#*< >Quoting removes (escapes) the special meaning of the special

    characters.

    Quoting Characters

  • 7/29/2019 Fundamentals of Unix - For Freshers

    170/299

    17

    \'"

    BackslashSingle Quotes

    Double Quotes

    Quoting \

  • 7/29/2019 Fundamentals of Unix - For Freshers

    171/299

    17

    \ Removes the special meaning of the next character

    $ echo the \\ escapes the next characterthe \ escapes the next character$ color=red\ white\ and\ blue$ echo the value of \$color is $colorthe value of $color is red white and blue$ echo one two \

    > three fourone two three four

    Quoting '

  • 7/29/2019 Fundamentals of Unix - For Freshers

    172/299

    17

    ' Removes the special meaning of all characters surrounded bythe single quotes

    $ color='red white and blue'$ echo 'the value of \$color is $color'the value of \$color is $color$ echo 'the value of $color is' $colorthe value of $color is red white and blue$ echo 'this doesn't work'

    >$ echo '**************'************

    Ctrl + d

    Quoting "

  • 7/29/2019 Fundamentals of Unix - For Freshers

    173/299

    17

    " Removes the special meaning of all characters surrounded by thedouble quotes except \ , $, {variable name}, $(command),and "

    $ color="red white and blue"$ echo "the value of \$color is $color"the value of $color is red white and blue$ cur_dir="$LOGNAME - your current directory is $(pwd)"$ echo $cur_dir

    user3 - your current directory is /home/user3/tree$ echo "they're all here, \\, ', \" "they're all here, \, ', "

    Quoting Summary

  • 7/29/2019 Fundamentals of Unix - For Freshers

    174/299

    17

    Backslash

    Single Quotes

    Double Quotes

    Escapes next character

    Escapes all characters inside ' 'Escapes all characters inside " ", except \, $, {variable name},and $(command)

  • 7/29/2019 Fundamentals of Unix - For Freshers

    175/299

    17

    Input and Output Redirection Introduction

  • 7/29/2019 Fundamentals of Unix - For Freshers

    176/299

    17

    Shell

    * ? [ ]

    cmd >file

    cmd 2>f.errcmd< file

    Command execution

    File name generation

    Interpretive

    programming language

    Pipelines

    I/O redirection

    Environment settings

    Variableassignment

    Variablesubstitution

    Commandsubstitution

    export varname

    env

    variable=value

    set

    $varname

    $(cmd)

    stdin, stdout, and stderr

  • 7/29/2019 Fundamentals of Unix - For Freshers

    177/299

    17

    Reset

    BreakStop

    f1 f2 f3 f4 f5 f6 f7 f8

    Back

    Space

    ReturnCTRL

    User

    System

    . / + -

    7 8 9

    4 5 6 ,

    1 2 3

    0 .

    Menu

    TabNext Select

    Prev

    Insert

    line

    Delete

    line

    Insert Delete

    char char

    |

    \

    }

    ]

    {

    [

    =

    +

    _

    -

    "

    '

    :

    ;

    Shift

    Extend

    char

    ?

    /

    >

    .

  • 7/29/2019 Fundamentals of Unix - For Freshers

    178/299

    17

    Any command that reads its input from stdin can have its input redirected to come from another file.

    $ cat remind

    Your mother's birthday is November 29$ mail user3 < remind$ mailFrom user3 Mon July 15 11:30 EDT 1993

    Your mother's birthday is November 29

    ?d$

    Output Redirection and >>

  • 7/29/2019 Fundamentals of Unix - For Freshers

    179/299

    17

    Any command that produces output to stdout can have its output redirected to another file.

    $ date > date.out $ ls >> ls.out$ date > who.log $ who >> who.log$ cat > cat.out $ ls >> who.loginput text here

    Ctrl + d

    Error Redirection 2> and 2>>

  • 7/29/2019 Fundamentals of Unix - For Freshers

    180/299

    18

    Any command that produces error messages to stderr can have

    those messages redirected to another file.

    $ cp 2> cp.err $ cp 2>> cp.err

    $

    $ more cp.errUsage: cp [-f|-i] [-p] source_file target_file

    cp [-f|-i] [-p] source_file ...target_directorycp [-f|-i] [-p] -R|-rsource_directory...target_directory

    Usage: cp [-f|-i] [-p] source_file target_file

    cp [-f|-i] [-p] source_file ... target_directorycp [-f|-i] [-p] -R|-r source_directory...target_directory

    What Is a Filter?

  • 7/29/2019 Fundamentals of Unix - For Freshers

    181/299

    18

    Reads standard input andproduces standard output.

    Filters the contents of the input stream or a file.

    Sends results to screen, never modifies the input stream or file.

    Processes the output of other commands when they are used in conjunction with output redirection.

    Examples: cat, grep, sort,wc

    wc Word Count

  • 7/29/2019 Fundamentals of Unix - For Freshers

    182/299

    18

    Syntax:

    wc [-lwc] [file...] Counts lines, words, and characters ina file

    Examples:

    $ wc funfile

    116 529 3134 funfile$$ wc -l funfile116 funfile$$ ls > ls.out$$ wc -w ls.out

    72 ls.out

    sort Alphabetical or Numerical Sort

  • 7/29/2019 Fundamentals of Unix - For Freshers

    183/299

    18

    sort [-ndutX] [-k field_no] [file...] Sorts lines

    $ sort funfile

    $ tail -1 /etc/passwduser3:xyzbkd:303:30:studentuser3:/home/user3:/usr/bin/sh1 2 3 4 5 6 7$ sort -nt: -k 3 < /etc/passwd

    $ who > whoson

    $ sort whoson $ sort -u -k 1,1 whoson

    grep Pattern Matching

  • 7/29/2019 Fundamentals of Unix - For Freshers

    184/299

    18

    grep [-cinv] [-e]pattern [-e pattern] [file...]grep [-cinv] -f patterns_list_file [file...]

    $ grep user /etc/passwd

    $ grep -v user /etc/passwd$ grep -in -e like -e love funfile

    $ who > whoson$ vi whoson:1,$s/ .*//gZZ$ grep -f whoson /etc/passwd

    Input and Output Redirection Summary

  • 7/29/2019 Fundamentals of Unix - For Freshers

    185/299

    18

    cmd < filecmd > filecmd >> file

    cmd 2 > file.errA filter

    wcsortgrep

    Redirects input to cmdfrom fileRedirects standard output from cmdto fileRedirects standard output from cmdand append to fileRedirects errors from cmdto file.err

    A command that accepts stdin and generates stdout

    Line, word, and character countSorts lines alphabetically or numerically

    Searches for lines that contain a pattern

  • 7/29/2019 Fundamentals of Unix - For Freshers

    186/299

    18

    Pipelines Introduction

  • 7/29/2019 Fundamentals of Unix - For Freshers

    187/299

    18

    Shell

    * ? [ ]

    Command execution

    File name generation

    Interpretive

    programming language

    cmd >file

    cmd 2>f.err

    cmd< file

    I/O redirection

    Environment settings

    Variableassignment

    Variablesubstitution

    Commandsubstitution

    export varname

    env

    variable=value

    set

    $varname

    $(cmd)

    PipelinescmdA | cmdB

    Why Use Pipelines

  • 7/29/2019 Fundamentals of Unix - For Freshers

    188/299

    18

    $ who > temp_file

    $ wc -l < temp_file

    $ rm temp_file

    who wc -l

    The | Symbol

  • 7/29/2019 Fundamentals of Unix - For Freshers

    189/299

    18

    cmd_A cmd_B*

    $ ps -ef | more

    cmd_C

    generatesstderr

    generates

    stdout

    generatesstdout

    generatesstdout

    generatesstderr

    generatesstderr

    acceptstdin

    acceptstdin

    $ ls | more$ ls | sort -r | more

    *cmd_Bmust be a filter.

    Pipelines versus Input and Output Redirection

  • 7/29/2019 Fundamentals of Unix - For Freshers

    190/299

    19

    cmd_out >file cmd_out | cmd_inor

    cmd_in who.out who | sortsort < who.out

    Redirection in a Pipeline

  • 7/29/2019 Fundamentals of Unix - For Freshers

    191/299

    19

    3 streams for each command:

    -stdin-stdout-stderr

    You can redirect streams that are not dedicated to the pipeline:

    stdout stdin

    cmd_A cmd_BAvailable forredirection:

    stdinstderr

    stdoutstderr

    stdout stdin stdout stdin

    cmd_A cmd_B cmd_Cstdinstderr

    stderr stdoutstderr

    Available forredirection:

    $ grep user /etc/passwd | sort > sort.out

    Some Filters

  • 7/29/2019 Fundamentals of Unix - For Freshers

    192/299

    19

    Cuts out specified columns or fields and display to stdout

    Translates characters

    Passes output to a file and to stdout

    Prints and format output to stdout

    cut

    tr

    Tee

    pr

    The cut Command

  • 7/29/2019 Fundamentals of Unix - For Freshers

    193/299

    19

    cut -clist [file...] Cuts columns or fieldscut -flist [-dchar][-s][file...] from files or stdin

    $ date | cut -c1-3$ tail -1 /etc/passwduser3:mdhbmkdj:303:30:student user3:/home/user3:/usr/bin/sh1 2 3 4 5 6 7

    $ cut -f1,6 -d: /etc/passwd

    $ cut -f1,6 -d: /etc/passwd | sort -r$ ps -ef | cut -c49- | sort -d

    The tr Command

  • 7/29/2019 Fundamentals of Unix - For Freshers

    194/299

    19

    tr [-s] [string1 [string2]] Translates characters

    $ who | tr -s " "$$ date | cut -c1-3 | tr "[:lower:]" "[:upper:]"

    The tee Command

  • 7/29/2019 Fundamentals of Unix - For Freshers

    195/299

    19

    tee [-a] file [file. . .] Tap the pipeline

    $ who | sort$ who | tee unsorted | sort

    $ who | tee unsorted | sort | tee sorted$ who | wc -l$ who | tee whoson | wc -l

    stdin stdout

    file

    The pr Command

  • 7/29/2019 Fundamentals of Unix - For Freshers

    196/299

    19

    pr [-option] [file...] Formats stdin and produces stdout

    $ pr -n3 funfile$ pr -n3 funfile | more$ ls | pr -3$ grep home /etc/passwd | pr -h "User Accounts"

    Printing from a Pipeline

  • 7/29/2019 Fundamentals of Unix - For Freshers

    197/299

    19

    ... | lp Located at end of pipe; sends output to printer

    $ pr -l58 funfile | lp

    Request id is laser-226 (standard input).$$ ls -F $HOME | pr -3 | tee homedir | lpRequest id is laser-227 (standard input).$$ grep home /etc/passwd | pr -h "user accounts" | lpRequest id is laser-228 (standard input).

    Pipelines Summary

  • 7/29/2019 Fundamentals of Unix - For Freshers

    198/299

    19

    cmd_out | cmd_incmd_out | cmd_in_out | cmd_in

    Cuts out columns or fields to standard output

    Sends input to standard output and a specified file

    Prints formatter to the screen, commonly used with lp

    Translates characters

    Pipeline

    cutteepr

    tr

  • 7/29/2019 Fundamentals of Unix - For Freshers

    199/299

    19

    The ps Command

  • 7/29/2019 Fundamentals of Unix - For Freshers

    200/299

    20

    ps [-efl] Reports process status

    $ psPID TTY TIME COMMAND

    1324 ttyp2 0:00 sh1387 ttyp2 0:00 ps

    $ ps -efUID PID PPID C STIME TTY TIME COMMANDroot 0 0 0 Jan 1 ? 0:20 swapperroot 1 0 0 Jun 23 ? 0:00 initroot 2 0 0 Jun 23 ? 0:16 vhand

    root 3 0 0 Jun 23 ? 12:14 statdaemonuser3 1324 1 3 18:03:21 ttyp2 0:00 -shuser3 1390 1324 22 18:30:23 ttyp2 0:00 ps -ef

    Background Processing

  • 7/29/2019 Fundamentals of Unix - For Freshers

    201/299

    20

    command line > cmd.out &

    $ grep user * > grep.out &

    [1] 194

    $ psPID TTY TIME COMMAND164 ttyp2 0:00 sh194 ttyp2 0:00 grep

    195 ttyp2 0:00 ps

    Putting Jobs in Background/Foreground

  • 7/29/2019 Fundamentals of Unix - For Freshers

    202/299

    20

    jobs

    fg [%number]fg [%string]bg [%number]bg [%string]

    Displays jobs currently runningSuspends a job running in the foreground

    stty susp ^ZBrings job number to the foreground or

    any job whose command line begins with string.Transfers job number to the background or

    any job whose command line begins with string.

    Ctrl + z

    The nohup Command

  • 7/29/2019 Fundamentals of Unix - For Freshers

    203/299

    20

    nohup command line & Makes a command immune to hangup(logout) no hangup

    $ nohup cat * > bigfile &[1] 972

    $

    login: user3Password:

    $ ps -ef | grep catUID PID PPID COMMANDuser3 972 1 .... cat * > bigfile &

    Ctrl + d Return

    The nice Command

  • 7/29/2019 Fundamentals of Unix - For Freshers

    204/299

    20

    nice [-N] command_line Runs a process at a lower priorityNis a number between 1 and 19.

    $ nice -10 cc myprog.c -o myprog$ nice -5 sort * > sort.out &$

    The kill Command

  • 7/29/2019 Fundamentals of Unix - For Freshers

    205/299

    20

    kill [-s signal_name] PID [PID...]

    $ cat /usr/share/man/cat1/* > bigfile1 &[1] 995

    $ cat /usr/share/man/cat2/* > bigfile2 &[2] 996

    $ kill 995[1] - Terminated cat /usr/share/man/cat1/* > bigfile1 &$ kill -s INT %2[2] + Interrupt cat /usr/share/man/cat2/* > bigfile2 &$ kill -s KILL 0

    Sends a signal to specified

    processes.

  • 7/29/2019 Fundamentals of Unix - For Freshers

    206/299

    20

    Shell Programming Overview

  • 7/29/2019 Fundamentals of Unix - For Freshers

    207/299

    20

    A shell program is a regular file containing UNIX system commands.

    The file's permissions must be at least "read" and "execute."

    To execute, type the name of the file at the shell prompt.

    Data can be passed into a shell program through

    environment variables

    command line arguments

    user input

    Example Shell Program

  • 7/29/2019 Fundamentals of Unix - For Freshers

    208/299

    20

    /usr/bin/sh

    /usr/bin/date /usr/bin/ls

    $

    $ myprog

    PID = 1324

    PID = 1350

    PID = 1361 PID = 1362

    /usr/bin/sh

    myprog

    #

    date

    ls -F

    Execution ofmyprog:

    $ cat myprog#this is the program myprogdatels -F

    Passing Data to a Shell Program

  • 7/29/2019 Fundamentals of Unix - For Freshers

    209/299

    20

    $ color=lavender

    $ cat color1echo You are now running program: color1echo the value of the variable color is: $color

    $ chmod +x color1

    $ color1You are now running program: color1the value of the variable color is:

    $ export color

    $ color1You are now running program: color1the value of the variable color is: lavender

    Arguments to Shell Programs

  • 7/29/2019 Fundamentals of Unix - For Freshers

    210/299

    21

    $ sh_program arg1 arg2 . . . argX$0 $1 $2 . . . $X

    $ cat color3

    echo You are now running program: $0echo The value of command line argument \#1 is: $1echo The value of command line argument \#2 is: $2

    $ chmod +x color3

    $ color3 red green

    You are now running program: color3The value of command line argument #1 is: redThe value of command line argument #2 is: green

    Arguments to Shell Programs (Continued)

  • 7/29/2019 Fundamentals of Unix - For Freshers

    211/299

    21

    This shell program will install a program, specified as a command-line

    argument to your bin directory:

    $ cat my_installecho $0 will install $1 to your bin directorychmod +x $1mv $1 $HOME/binecho Installation of $1 is complete

    $ chmod +x my_install

    $ my_install color3

    my_install will install color3 to your bin directoryInstallation of color3 is complete$

    Some Special Shell Variables # and *

  • 7/29/2019 Fundamentals of Unix - For Freshers

    212/299

    21

    # The number of command line arguments* The entire argument string

    $ cat color4

    echo There are $# command line argumentsecho They are $*echo The first command line argument is $1

    $ chmod +x color4

    $ color4 red green yellow blue

    There are 4 command line argumentsThey are red green yellow blueThe first command line argument is red$

    Some Special Shell Variables # and * (Continued)

  • 7/29/2019 Fundamentals of Unix - For Freshers

    213/299

    21

    This enhanced example of the install program accepts multiple

    command-line arguments:

    $ cat > my_install2echo $0 will install $# files to your bin directoryecho The files to be installed are: $*chmod +x $*mv $* $HOME/binecho Installation is complete

    $ chmod +x my_install2

    $ my_install2 color1 color2my_install2 will install 2 files to your bin directoryThe files to be installed are: color1 color2Installation is complete

    read variable [ variable ]

    The read Command

  • 7/29/2019 Fundamentals of Unix - For Freshers

    214/299

    21

    readvariable [ variable ... ]

    $ cat color6echo This program prompts for user inputecho "Please enter your favorite two colors ->