Unix Command Unit 1

  • Upload
    -

  • View
    242

  • Download
    3

Embed Size (px)

Citation preview

  • 7/31/2019 Unix Command Unit 1

    1/40

    Unix and shell programming

    UNIT-1

    CHAPTER 1

    THE UNIX OPERATING SYSTEM

    UNIX OPERATING SYSTEM:

    Like DOS and windows, there is another operating system called UNIX

    UNIX operating system arrived earlier than the previous two (i.e., DOS and

    Windows) operating system.

    UNIX is a giant operating system.

    UNIX runs on every hard ware and provides inspiration to open source

    movement.

    UNIX operating system is difficult to use, even when the user is an

    experienced professional.

    UNIX uses numerous symbols.

    UNIX operating system doesnt tell whether you are right or wrong and it

    doesnt give any warnings.

    Interaction with UNIX system is through a command interpreter called the

    shell.

    Commands in UNIX are just like English languages.

    A BRIEF INTRODUCTION TO UNIX SYSTEMS:

    Logging in with username and password:

    UNIX is security conscious. It can be used only by those persons who

    maintain an account with the computer system. The list of account is maintained

    separately in the computer.

    System Administrator is a person who grants you the authority to use the

    system.

    To login in UNIX system there is a login prompt which indicates that the

    terminal is available for someone to login. This login prompt also indicates that the

    previous user has logged out.

    For example, consider that you have a user account by name kumar. Enter

    Rashmi Shenoy K

    Dept Of CS&EPage 1

  • 7/31/2019 Unix Command Unit 1

    2/40

    Unix and shell programming

    the string at the login prompt then press enter key after the string.

    Login: kumar[Enter]

    Password:

    The system now requests you to enter the password. The password entry is

    displayed as asterisk (*) which is another security feature built in the system.

    Login: kumar[Enter]

    Password: ******* [Enter]

    The string that you entered at the first prompt (ie, login) is known as login

    name or user-id or username and the secret code entered is called as Password.

    If you enter either username or password incorrectly, then the system gives

    the following message.

    Login incorrect

    login:

    After logging in you will see a $ prompt, with the cursor constantly blinking

    beside it. This $ symbol is a typical UNIX prompt. For some UNIX system %

    prompt will be there instead of $ prompt and the system administrator will behaving # prompt.

    THE COMMANDS:

    1] date: Displaying both date and time.

    UNIX system has date command that shows or displays the date and time of

    the system you are logged in.

    $ date [Enter]

    Fri Aug 19 13:28:33 IST 2009 [Enter]

    $ _

    date command displays the current day, month, date, time and year. IST here is

    Rashmi Shenoy K

    Dept Of CS&EPage 2

  • 7/31/2019 Unix Command Unit 1

    3/40

    Unix and shell programming

    Indian Standard Time.

    2] tput clear: Clearing the screen.

    All UNIX systems offer the tputcommand to clear the screen. When you

    use tputas it is, (i.e., without any additional words) it will display like:

    $ tput

    usage: tput [-T [term]] capname [parm argument]

    OR: tput S

  • 7/31/2019 Unix Command Unit 1

    4/40

    Unix and shell programming

    With cal, we can produce the calendar for any month or year between the

    years 1 and 9999.

    4] who: Who are the users?

    UNIX is a system that can be concurrently used by multiple users. Forknowing the people who are using the systems, you have to use who command.

    $ who

    kumar console May 9 09:31 (:0)

    vipul pts/4 May 9 09:32 (:0.0)

    There are currently two users- kumar and vipul. These are the user-ids or

    usernames which the user uses to login. The output includes the username (ie,kumar). The second column shows the name of the terminal the user is working

    on (ie, console). The date and time of the login are also shown in third, fourth and

    fifth column.

    The output of the who command doesnt include any headers to indicate

    what the various columns mean.

    5] ps: Viewing Processes.

    Every command that you run gives rise to aprocess. To view all processs

    that you are responsible for creating, run the ps command.

    $ ps

    PID TTY TIME CMD

    344 console 01:00 ksh

    ps command generates a header followed by line containing the details of

    the ksh process. This process has a unique number (ie, 344 here in the example

    above) called theprocess-id, and when you log out, this process is killed.

    6] ls: Listing Files.

    Rashmi Shenoy K

    Dept Of CS&EPage 4

  • 7/31/2019 Unix Command Unit 1

    5/40

    Unix and shell programming

    UNIX system has a large number of files that control its functioning, and

    users also create files on their own. These files are organised in separate folders

    called directories. ls command can be used to list the names of the files available

    in the directory.

    $ ls

    README

    chap01

    chap02

    chap03

    helpdir

    progs

    The files are arranged alphabetically with uppercase letters having

    precedence over lowercase letters called as ASCII collating sequence.

    To get the filenames having similar names, we can give ls command like:

    $ ls chap*

    chap01

    cahp02

    chap03

    If you want to know more about the files, you can use ls command with

    optionl, between the command and the file name.

    $ ls l chap*

    Rashmi Shenoy K

    Dept Of CS&EPage 5

  • 7/31/2019 Unix Command Unit 1

    6/40

    Unix and shell programming

    -rw-r- -r- - 1 kumar users 5609 Apr 23 09:30 chap01

    -rw-r- -r- - 1 kumar users 23456 May 14 17:55 chap02

    -rw-r- -r- - 1 kumar users 234678 June 14 04:09 chap03

    The argument beginning with a hyphen is known as an option. An option

    changes the default behaviour of the command, so ifls displays a columnar list of

    files, thel option makes to display some of the attributes also.

    7] wc: Counting Number of Lines in a file

    The wc command gives the number of lines in a particular file.

    $ wc list

    6 7 43 list

    Here listis the filename given as argument to the wc command. The first

    column displays Number of lines in the file list, second column displays Number

    of words in the file list, and third column displays Number of the characters in the

    file list.

    8] exit: Signing Off

    To suspend a session you have to use exitcommand. This command givesthe login prompt which indicate that the user has logged out successfully and

    another user can be logged in.

    $ exit

    login:

    CHAPTER 2

    Rashmi Shenoy K

    Dept Of CS&EPage 6

  • 7/31/2019 Unix Command Unit 1

    7/40

    Unix and shell programming

    THE UNIX ARCHITECTURE AND COMMAND USAGE

    THE UNIX ARCHITECTURE:

    The entire UNIX system is supported by a handful of essentially simple, though

    somewhat abstract concepts. The software architecture of UNIX has

    1. Division of Labor : Kernel and Shell

    2. The File and Process

    3. The System Calls

    1] Division of Labor: Kernel and Shell

    UNIX architecture comprises of two major components-The shell and the

    kernel. The Kernel interacts with the machines hardware and the shell interactswith the user.

    The Kernel is the core of the operating system. It is a collection of routines

    mostly written in C. It is loaded ino the memory when the system is booted and

    communicates directly with the hardware. User programs that need to access the

    hardware use the services of the kernel and the kernel performs the job on behalf

    of the user. These programs access the kernel through a set of functions called

    system calls. Kernel manages the systems memory, schedules processes, decides

    their priorities and performs various other tasks.

    The shell performs the role of command interpreter. Even though there is

    only one kernel running on the system, there could be several shells in action, one

    for each user whos logged in.

    When you enter a command through keyboard, the shell thoroughly

    examines the keyboard input for special characters. If it finds any, it rebuilds a

    simplified command line, and finally communicates with the kernel to see that the

    command is executed.

    The kernel is represented by the file depending on the system. The shell is

    represented by sh(Bourne shell), csh(C shell), ksh(Korn shell) orbsh(Bash shell).

    One of these shells will be running when the user logs in.

    To know which shell is running right now, you have to type $ echo

    Rashmi Shenoy K

    Dept Of CS&EPage 7

  • 7/31/2019 Unix Command Unit 1

    8/40

  • 7/31/2019 Unix Command Unit 1

    9/40

    Unix and shell programming

    3] The System Calls

    The UNIX systems consist of kernel, shell and applications. Though there

    are over a thousand commands in the system, they all use a handful of functions

    called System Calls, to communicate with the kernel.If an operating system uses different system calls, then it wont be UNIX

    system.

    For example: Write system cal is used to write a file. Open system call is

    used to open a file.

    These system calls are built into the kernel and interaction through them

    represents an efficient means of communication with the system.

    FEATURES OF UNIX:

    UNIX is an operating system, so it has all the features an operating system is

    supposed to have.

    1. UNIX:A Multiuser System

    2. UNIX:A multitasking System

    3. The Building Block Approach

    4. The UNIX Toolkit

    5. Pattern Matching

    6. Programming Facility

    7. Documentation

    1] UNIX: A Multiuser System

    UNIX is a multiprogramming system. It permits multiple programs to run in two

    ways:

    Multiple users can run separate jobs. A single user can also run multiple jobs.

    In UNIX, the resources are actually shared between all users. UNIX is also a

    multiuser system.

    Rashmi Shenoy K

    Dept Of CS&EPage 9

  • 7/31/2019 Unix Command Unit 1

    10/40

    Unix and shell programming

    2] UNIX: A Multitasking System

    A single user can also run multiple tasks simultaneously. UNIX is a

    multitasking system.

    It is possible for a user to edit a file, print another file on the printer, send e-mail and browse the internet at a time.

    In a multitasking environment, a user sees one job running in foreground and

    the rest running in the background.

    3] The Building-Block Approach

    The Designers of UNIX operating system developed a few hundred

    commands each performing one simple job only.

    For example: two commands ls and wc can be used with the | (pipe) to

    count the number of files in your directory. No separate command was designed to

    perform the job. The commands which are connected in this way are called filters

    because they filter or manipulate data in different ways.

    UNIX tools are designed with the requirement that the output of one tool can

    be used as input to another.

    4] The UNIX Toolkit

    UNIX system uses general-purpose tools, text manipulation utilities

    (called filters), compilers and interpreters, networked applications and system

    administration tools.

    New tools are being added and the older ones are being removed or

    modified.

    5] Pattern Matching

    UNIX features very sophisticated pattern matching features. Instead of

    giving all filenames as input for ls command, you can give an unusual argument

    like (chap*).

    Eg: $ ls (chap*)

    Rashmi Shenoy K

    Dept Of CS&EPage 10

  • 7/31/2019 Unix Command Unit 1

    11/40

    Unix and shell programming

    chap01

    chap02

    chap03

    The * is a special character used by the system to indicate that it can

    match number of filenames. Matching is not only for filenames. The *(known

    as metacharacter) is not the only special character used by UNIX system, there

    are several others. Some of the most advanced and useful tools also use a special

    expression called a regular expression.

    6] Programming Facility

    The UNIX shell is also a programming language. It has aii the necessary

    ingredients, like control structures, loops and variables. These features are used

    to design shell scripts. Many of the systems function can be controlled and

    automated by using shell scripts.

    7] Documentation

    The online facility available for documentation in UNIX system is the man

    command, which remains the most important reference for commands and their

    configuration files.

    LOCATING COMMANDS:

    The UNIX system is commandbased i.e., things happen because of the

    commands that you key in. UNIX commands are not more than five characters

    long. All UNIX commands are single words like ls, cat, who etc.

    These names are all in lowercase. For example, if you type uppercase

    instead of lowercase, it will display like:

    $ LS

    bash: LS: command not found

    This message is from the shell (here, the Bash shell). There is no command

    named LS on the UNIX system.

    Rashmi Shenoy K

    Dept Of CS&EPage 11

  • 7/31/2019 Unix Command Unit 1

    12/40

    Unix and shell programming

    These commands are essentiallyfiles, containing programs, mainly written

    in C. Files are stored in directories. For example, ls command is a file found in the

    directory /bin.

    You can find the location of an executable program using type command.$ type ls

    Ls is /bin/ls

    This means that when you execute ls command, the shell locates this file in /

    bin directory and makes arrangements to execute it.

    THE PATH:

    The sequence of directories that the shell searches to look for a commandis specified in its own PATH variable. These directories are separated by colons.

    echo command is used to evaluate this variable and you can see list of directories

    separated by colons.

    $ echo $PATH

    /bin:/usr/bin:/usr/local/bin:/usr/ccs/bin:/usr/local/java/bin:

    There are six directories in the above colon separated list. First / indicates

    the topmost directory called root. To consider the second one, /usr/bin represents

    a hierarchy of three directory names. Here usr is below the root directory and bin

    is below the usr. When you issue a command, the shell searches this list in the

    sequence specified to locate and execute it.

    INTERNAL AND EXTENAL COMMANDS:

    Some commands are implemented as part of the shell itself rather than

    separate executable files. Such commands are called internal commands. If a

    command exists both as an internal command of the shell and external command,

    then the shell will accord top priority to its own internal command with the same

    name. Some built-in commands are echo, pwdetc

    Since ls command is a program or file having an independent existence in

    the /bin directory, it is branded as an external command. Most commands are

    Rashmi Shenoy K

    Dept Of CS&EPage 12

  • 7/31/2019 Unix Command Unit 1

    13/40

    Unix and shell programming

    external in nature, but there are some which are not found or executed even if they

    are in one of the directories specified by PATH.

    For example:

    $ type echo

    echo is a shell builtin

    echo is not an external command, ie when you type echo, the shell wont

    take in its PATH to locate it.

    echo command execute from its own set of built-in commands that are not

    stored as separate files. These built-in commands, of which echo is a member are

    known as internal commands.

    The shell does all the work. The shell program starts running when the

    user logs in and dies when user logs out. The shell is an external command with a

    difference; it possesses its own set of internal command.

    COMMAND STRUCTURE:

    UNIX commands take the following syntax:

    Command [option] [argument]

    Where command is the command name, options means set of option which the

    command can take, arguments means the arguments used.

    Commands and arguments have to be separated by spaces or tabs to enable

    tha system to interpret them as words. A contiguous string of spaces and tabs

    together are called whitespace. The shell compresses multiple occurrences of

    whitespace into a single whitespace.

    UNIX arguments range from the simple to the complex. They consist ofoptions, expressions, instructions, filenames etc...

    1] OPTIONS

    Options are special type of arguments which is used with a sign in the

    beginning.

    Rashmi Shenoy K

    Dept Of CS&EPage 13

  • 7/31/2019 Unix Command Unit 1

    14/40

    Unix and shell programming

    For example:

    $ ls -l

    -l here is a special argument to ls known as an option. An option is normally

    preceded by a minus sign [-] to distinguish it from filenames. There must not beany whitespace between - and l.

    If you give wrong option or not a valid option, then the shell locates the

    command. But it finds the option wrong.

    For example:

    $ ls -z

    ls: illegal option z

    If you type your command with no space between command and option,

    then it will display as command not found.

    For example:

    $ ls-l

    bash: ls-l: command not found

    Options are given a special name because they are predetermined. Options

    can be normally combined with only one sign.

    For example:

    Instead of using

    $ ls l a t

    We can use one minus sign like

    $ ls lat

    Rashmi Shenoy K

    Dept Of CS&EPage 14

  • 7/31/2019 Unix Command Unit 1

    15/40

    Unix and shell programming

    2] FILENAME ARGUMENTS

    Many UNIX commands use filename as argument so the command can

    take input from the file. If a command uses filename as argument, it will be its last

    argument and after all options.For example:

    $ ls latchap01 chap02 chap03

    $ cp chap01 chap02 progs

    $ rm chap01 chap02

    The command with its arguments and options is known as the command

    line. This command line can be considered complete only after the userhas pressed [Enter], so that the entire line is fed to the shell as its input for

    interpretation and execution.

    3] EXCEPTIONS

    Some commands like pwddo not take any options and arguments and who

    command may or may not be specified with arguments.

    The ls command can run without arguments

    For example: $ ls

    It can be used only with options

    For example: $ ls l

    It can be used only with filenames

    For example: $ ls filename

    And it can be used with the combination of both option and filename

    For example: $ ls l filename

    Some commands like cutcompulsorily take options. Some commands like grep,

    sedcan take an expression as an argument, or a set of instructions as argument.

    Rashmi Shenoy K

    Dept Of CS&EPage 15

  • 7/31/2019 Unix Command Unit 1

    16/40

    Unix and shell programming

    FLEXIBILTY OF COMMAND USAGE:

    The UNIX system provides certain degree of flexibility in the usage of

    commands. A command can be entered in more than one way.

    1] COMBINING COMMANDS

    UNIX allows the user to specify more than one command in the command

    line. Each command has to be separated from the other by a ; (semicolon).

    For example:

    $ wc note ; ls lnote

    You can even group several commands together so that their combined

    output is redirected to a file.

    For example:

    $ (wc note; ls lnote)>newlist

    The combined outputs of the two commands are now sent to file newlist.

    This is called redirecting the output. Here whitespace doesnt bother. When a

    command line contains a semicolon, the shell understands that the command

    on each side of it needs to be processed separately. The ;here is known as

    metacharacter.

    2] A COMMAND LINE CAN OVERFLOW OR BE SPLIT INTO MULTIPLE

    LINES

    The terminal width of command is restricted to 80 characters, but that

    doesnt prevent the user from entering a command. In one line even though the

    total width may exceed 80 characters, the command simply overflows to the next

    line though it is still in a single logical line.

    When you split a long command into multiple lines, the shell issues a

    secondary prompt, usually >, to indicate that the command line is not complete.

    For example:

    $ echo This is

    Rashmi Shenoy K

    Dept Of CS&EPage 16

  • 7/31/2019 Unix Command Unit 1

    17/40

    Unix and shell programming

    >a three-line

    >text massage

    The output of the above command will be

    This is

    a three-line

    text message

    3] ENTERING A COMMAND BEFORE PREVIOUS COMMAND HAS

    FINISHED

    UNIX provides a full-duplex terminal where the user can type a commandat any time. When the user runs a long program, the prompt wont appear until

    program execution is complete. Subsequent commands can be entered at the

    keyboard without waiting for the prompt. Prompt may or may not appear on the

    screen.

    The command that the user enters is not seen by the shell as its output

    when it is busy running another program. The input remains stored in a buffer (a

    temporary memory storage) that is maintained by the kernel for all keyboard input.

    The command is passed to the shell after completing the execution of the previousinput or command.

    man: BROWSING THE MANUAL PAGE ON-LINE:

    UNIX offers an omline help facility in the man command. man displays the

    documentation called the man documentation.

    For example: To seek help on the wc command, the user simply has to run

    man command with wc as argument.

    $ man wc

    The entire information about wc command is displayed on the screen. The

    man command represents the first page and then pauses. It is done by sending its

    output to a pager program, which displays output at one page at a time. The pager

    Rashmi Shenoy K

    Dept Of CS&EPage 17

  • 7/31/2019 Unix Command Unit 1

    18/40

    Unix and shell programming

    is actually a UNIX command, and man command is always preconfigured to be

    used with a specific pager. UNIX system currently uses two pager programs:

    more, Brekeleys pager, which is now available universally as a superior

    alternative to the original pg command. less, the standard pager used on Linux systems, but also available for all

    UNIX platforms. Less is modeled on the vi editor and is more powerful than

    more because it provides vi-like navigational and search facilities.

    On a man page that uses more as the pager, the user will see a prompt at the

    bottom-left of the screen which looks like:

    ---More(26%)

    Less shows : prompt

    At this prompt the user can press a key to perform navigation, or search for

    string. The key which the user presses is interpreted as one of the mans internal

    commands. Many UNIX utilities like vi and mail also have their own internal

    commands. To quit thepagerand man, press q.

    NAVIGATION AND SEARCH

    There are numerous navigation commands. For example:

    f or spacebar, displays by one screen text at a time.

    b, moves back one screen.

    The man documentation is sometimes quite extensive, and the search facility

    lets the user to locate a page containing a keyword quite easily. For example: To

    call up the page containing the word clobberby using the string with the / (front

    slash):

    / clobber[Enter]

    You are taken to the page containing clobber. If that is not the page you are

    looking for, then you can repeat the search by pressing n.

    Rashmi Shenoy K

    Dept Of CS&EPage 18

  • 7/31/2019 Unix Command Unit 1

    19/40

    Unix and shell programming

    UNDERSTANDING THE man DOCUMENTATION:

    The man documentation is organized in 8 sections. Later enhancements have

    added subsections (like 1C, 1M, 3N etc). Occasional references to other sections

    can also be reflected in the SEE ALSO section of a man page.When you use man command, it starts searching the manuals starting from

    section 1. If it locates a keyword in one section, it wont continue the search

    even if the keyword occurs in another section. We can provide section numbers

    additionally as arguments forman command.

    For example:passwdappears in section 1 and section 4. If you want to get

    documentation ofpasswdin section 4, you have to type

    $ man4 passwd

    8 SECTIONS OF THE MAN DOCUMENTATION:

    Sections Subject

    1 User programs

    2 Kernels system calls

    3 Library functions

    4 Administrative file formats

    5 Miscellaneous

    6 Games7 Special files

    8 Administration commands

    UNDERSTANDING A man PAGE:

    [see page number 36 Fig 2.2]

    A man page is divided into a number of compulsory and optional sections.

    Every command doesnt have all sections, but the first three sections (ie, NAME,SYNOPSIS and DESCRIPTION) are generally seen in all man pages.

    NAME presents a one-line introduction to the command.

    For example: $ man wc

    Rashmi Shenoy K

    Dept Of CS&EPage 19

  • 7/31/2019 Unix Command Unit 1

    20/40

    Unix and shell programming

    NAME

    wc - displays a count of lines, words and characters in a file.

    SYNOPSIS shows the syntax used by the command

    For example:

    SYNOPSIS

    wc [-C / -m / -c] [-lw] [file]

    SYNOPSIS section should be examined closely.

    In the example above you can see the syntax with the options and arguments

    used with the command.The SYNOPSIS follows certain conventions and rules which every user must

    understand:

    If a command argument is enclosed in rectangular brackets, then it is

    optional. Otherwise, the argument is required. In the example above the wc

    man page shows all of its arguments enclosed in three such groups. This

    means that wc can be used without arguments.

    The ellipsis (a set of three dots) implies that there can be more instances of

    the preceding word. The expression [file] signifies that wc can be used

    with more than one filename as argument.

    The / character means that only one of the options shown on either side of

    the pipe can be used. In the example above the option c, -m and c can be

    used.

    DESCRIPTION provides a detailed description.

    For example:

    DESCRIPTION

    The wc utility reads one or more input files and, by default, writes the

    number of newline characters, words and bytes contained in each input file

    to the standard

    Rashmi Shenoy K

    Dept Of CS&EPage 20

  • 7/31/2019 Unix Command Unit 1

    21/40

    Unix and shell programming

    All options used by the command are listed in the OPTIONS section.

    Theres separate section named EXIT STATUS which lists possible error

    conditions and their numeric representation.

    USING man TO UNDERSTAND man:

    Since man is also a UNIX command like ls orcat, you can use this

    command to view its own documentation.

    For example:

    $ man man

    The variable, PAGER, controls the pager man uses, and if it is set to less,

    then man will use less as its pager.

    This is how you set PAGER at the command before invoking man:

    PAGER=less; export PAGER

    And then run man command

    To evaluate the value of PAGER the command echo $PAGERis used. This

    setting is valid only for the current session.

    FURTHER HELP WITH man k AND whatis:

    The POSIX specification requires man to support only one option (-k). Most

    UNIX systems also offer the apropos command that emulates man k.

    man k: Searches a summary database and prints a one-line description of the

    command.

    Example:

    $ man k awk

    awk awk(1) -pattern scanning and processing language

    nawk nawk(1) - pattern scanning and processing language

    apropos: lists the commands and files associated with a keyword.

    Rashmi Shenoy K

    Dept Of CS&EPage 21

  • 7/31/2019 Unix Command Unit 1

    22/40

    Unix and shell programming

    $ apropos FTP

    ftp ftp(1) -file transfer program

    ftpd in.ftpd(lm) -file transfer protocol server

    ftpusers ftpusers(4) -file listing users to be disallowed ftp login

    privileges

    whatis: man command uses the f option to emulate whaiis behavior. The

    command lists one-liners for a command

    $whatis cp

    cp cp(1) -copy files

    WHEN THINGS GO WRONG:

    Terminals and keyboards have no uniform behavioral pattern. Terminal

    settings directly impact the keyboard operation. If you observe a different behavior

    from that expected, when you press certain keystrokes, it means that the terminal

    settings are different. In such cases, you should know which keys to press to get

    the required behavior.

    Backspacing doesnt work: Backspacing doesnt work on UNIX system.

    Consider that you misspelled passwd as password, and when you press the

    backspace key to erase the last three characters, you saw like:

    $ password^H^H^H

    Backspace doesnt work here. So it is seen like ^H when you press

    backspace. To erase a character you should use [Ctrl-h] or[Delete] key instead of

    backspace.

    Killing a line: If a command line contains many mistakes, you can kill the linealtogether without executing it. In such case, use [Ctrl-u].

    The line kill character erases everything in the line and returns the cursor to

    the beginning of the line.

    Interrupting a command: Sometimes, program goes on running for an hour and

    Rashmi Shenoy K

    Dept Of CS&EPage 22

  • 7/31/2019 Unix Command Unit 1

    23/40

    Unix and shell programming

    doesnt sem to complete. You can interrupt the program and bring back the prompt

    by using either[Ctrl-c] or[Delete]

    Terminating a commands input: The input of certain commands can be

    terminated using [Ctrl-d]$ cat

    [Ctrl-d]

    $_

    The keyboard is locked: The keyboard will get locked when you accidently press

    [Ctrl-s]. It can be released using [Ctrl-q]

    You can halt the output temporarily by pressing [Ctrl-s]. to resume scrolling, press[Ctrl-q]

    When [Enter] key doesnt work use either[Ctrl-j] or[Ctrl-m]

    The following table lists keyboard commands to try when things go wrong.

    Keystroke or commands Function

    [Ctrl-h] Erases text

    [Ctrl-c] or [Delete] Interrupts a command

    [Ctrl-s] Stops scrolling of screen output andunlocks keyboard

    [Ctrl-q] Resumes scrolling of screen output andunlocks keyboard

    [Ctrl-u] Kills command line without executing it

    [Ctrl-\] Kills running program but creates a core filecontaining the memory image of the program

    [Ctrl-z] Suspends process and returns shell prompt; usefg to resume lob

    [Ctrl-j] Alternative to [Enter]

    [Ctrl-m] Alternative to [Enter]

    CHAPTER 3

    Rashmi Shenoy K

    Dept Of CS&EPage 23

  • 7/31/2019 Unix Command Unit 1

    24/40

    Unix and shell programming

    THE FILE SYSTEM

    THE TYPES OF FILE:

    THE FILE:

    The file is a container for storing information. For example, if you name

    a file foo and write three characters a, b, c into it, thenfoo will contain only the

    string abc. A files size is not stored in the file, not even its name. All file attributes

    are kept in a separate area of the hard disk, not directly accessible to humans, but

    only to kernel.

    UNIX treats directories and devices as file. A directory is simply a folder

    to store filenames and other directories. All physical devices like the hard disk,

    memory, CD-ROM, printer and modem are treated as files. The shell is also a file,and also kernel.

    Files are divided into three categories:

    Ordinary file

    Directory file

    Device file

    1] Ordinary file:

    An Ordinary file is also called as regular file. This is the most common file

    type. All programs you type belong to this type. An ordinary file can be divided

    into two types:

    Text file

    Binary file

    Text file:

    A text file contains only printable characters. All C and Java program

    sources, shell and perl scripts are text files. A text file contains lines of characters

    where every line is terminated with the newline character, also known as linefeed

    (LF). When you press [Enter] while inserting text, the LF character is appended to

    every line.

    Rashmi Shenoy K

    Dept Of CS&EPage 24

  • 7/31/2019 Unix Command Unit 1

    25/40

    Unix and shell programming

    Binary file:

    A binary file contains both printable and unprintable characters that cover

    the entire ASCII range (0 to 255). Most UNIX commands are binary files, and the

    object code and executables that produce by compiling C programs are also binaryfiles. Pictures, sound and video files as well.

    2] Directory file:

    A directory file contains no data, but keeps some details of the files and

    subdirectories that it contains. The UNIX file system is organized with a number of

    directories and subdirectories and it can be created when needed.

    A directory file contains an entry file and subdirectories that it hosts. If you

    have 20 files in a directory, there will be 20 entries in the directory. Each entry hastwo components:

    The filename

    A unique identification number for the file or directory called inode number.

    When you create or remove a file, the kernel automatically updates its

    corresponding directory by adding or removing the entry (filename or inode

    number) associated with the file.

    3] Device file:

    All the operations on the devices are performed by reading or writing the file

    representing the device. It is advantageous to treat devices as files as some of the

    commands used to access an ordinary file can be used with device files as well.

    Device filenames are generally found inside a single directory structure, /

    dev. A device file is not really a stream of characters. It is the attributes of the file

    that entirely govern the operation of the device. The kernel identifies a device from

    its attributes and uses them to operate the device.

    FILENAMES IN UNIX:

    In UNIX system, a filename can consist of up to 255 characters. Files may

    Rashmi Shenoy K

    Dept Of CS&EPage 25

  • 7/31/2019 Unix Command Unit 1

    26/40

    Unix and shell programming

    or may not have extensions and can consist of practically any ASCII character

    except / and NULL character. You are permitted to use control characters or other

    nonprintable characters in a filename. However, you should avoid using these

    characters while naming a file. It is recommended to use only following characters

    as filenames:

    Alphabetic characters and numerals

    The period (.), hyphen (-) and underscore (_)

    UNIX imposes no rules for framing filename extensions. In all cases, it is

    the application that imposes restriction.

    Example: a C compiler expects C program filenames to end with .c, Oracle

    requires SQL scripts to have .sql extension.

    A file can have as many dots embedded in its name. a filename can also

    begin with or end with a dot.

    UNIX is case sensitive. For example: chap01, Chap01 and CHAP01 are

    three different filenames that can coexist in the same directory.

    THE PARENT-CHILD RELATIONSHIP:

    All files in UNIX are related to one another. The file system in UNIX is a

    collection of all of these related files organized in a hierarchical structure.

    The implicit feature of every UNIX file system is that there is a top, which

    serves as reference point for all files. This top is called rootand is represented by a

    / (front slash). Root is actually a directory.

    The root directory (/) has a number of subdirectories under it. These

    subdirectories in turn have more subdirectories and other files under them. For

    example, bin and usrare two directories directly under /, while a second bin and

    kumarare subdirectories under usr.

    Every file, apart from root, must have a parent. Thus, home directory is the

    parent ofkumar, while / is the parent ofhome, and grand parent ofkumar. If you

    create a file login.sqlunder the kumardirectory, then kumarwill be the parent of

    this file.

    Rashmi Shenoy K

    Dept Of CS&EPage 26

  • 7/31/2019 Unix Command Unit 1

    27/40

    Unix and shell programming

    /(root)

    bin dev etc home lib stand tmp usr

    date who dsk rdsk kumar sharma unix bin include sbin

    f0q18dt login.sql progs safe

    In parent-child relationship, the parent is always a directory. home andkumarare both directories as they are parent of atleast one file or directory.

    login.sqlis simply an ordinary file, it cant have any directory under it.

    THE HOME VARIABLE - THE HOME DIRECTORY:

    When the user logs on to the system, UNIX automatically places the user

    in a directory called the home directory. It is created by the system when a user

    account is opened. For example: If you login with user name kumar, you will have

    the pathname/home/kumar. The user can change his home directory when he

    wants.

    The shell variable HOME knows the home directory. to know the home

    directory you have to type $ echo $HOME

    You will get the output like/home/kumar. This sequence is called as

    Rashmi Shenoy K

    Dept Of CS&EPage 27

  • 7/31/2019 Unix Command Unit 1

    28/40

    Unix and shell programming

    absolute pathname. The absolute pathname is simply a sequence of directory

    names separated by slashes. An absolute pathname shows a files location with

    reference to the top i.e., root. These slashes act as delimiters to file and directory

    names, except the first slash. The first slash is the root.

    COMMANDS:

    1] pwd: CHECKING CURRENT DIRECTORY

    The user is placed in specific directory when he is logged in. the user can

    move around from one directory to another, but at a time he can be located in only

    one directory. this directory is called current directory.

    At any time, user is able to know what current directory is. The pwd (print

    working directory) command is used to know the present working directory.

    For example:

    $pwd

    /home/kumar

    The pwd displays the absolute pathname.

    2] cd: CHANGING THE CURRENT DIRECTORY

    The user can move around in the file system by using the cd (change

    directory) command. When used with an argument, it changes to the directory

    specified as argument.

    For example:

    $ pwd

    /home/kumar

    $ cd progs

    $pwd

    /home/kumar/progs

    Rashmi Shenoy K

    Dept Of CS&EPage 28

  • 7/31/2019 Unix Command Unit 1

    29/40

    Unix and shell programming

    The command cd progs in the above example means that Change the

    subdirectory to progs under the current directory.

    When the user wants to switch to/bin directory where most of the

    commaonly used UNIX commands are kept, he should use absolute pathname:$pwd

    /home/kumar/progs

    $ cd /bin

    $pwd

    /bin

    cd command can also be used without arguments. The cd command without

    an argument doesnt go to current directory. It switches to home directory.ie, the

    directory where user actually logged into.

    For example:

    1] $pwd

    /home/kumar/progs

    $ cd

    $pwd

    /home/kumar

    2] $ cd /home/Sharma

    $pwd

    /home/Sharma

    $ cd

    $pwd

    /home/kumar

    Rashmi Shenoy K

    Dept Of CS&EPage 29

  • 7/31/2019 Unix Command Unit 1

    30/40

    Unix and shell programming

    The cd command can sometimes fail if the user doesnt have proper

    permission to access the directory.

    3] mkdir: MAKING DIRECTORIES

    Directories are created with the mkdir(make directory) command. Thecommand is followed by names of directories to be created.

    For example:

    A directorypatch is created under the current directory like this:

    $ mkdir patch

    The user can create number of subdirectories with onemkdircommand

    For example: $ mkdir patch dbs doc

    The UNIX system lets the user to create directory trees with just one mkdir

    command

    For example: The following command creates a directory tree:

    $ mkdir pis pis/progs pis/data

    This creates three subdirectoriespis and two subdirectories underpis.

    The order of specifying the argument is important. The user cant create a

    subdirectory before creating the main or parent directory

    For example:

    $ mkdir pis/data pis/progs pis

    mkdir: Failed to make directory pis/data; No such file or directory

    mkdir: Failed to make directory pis/progs; No such file or directory

    Even though the system failed to create the two subdirectories,progand

    data, it still created thepis directory.

    Sometimes, the system refuses to create a directory:

    Rashmi Shenoy K

    Dept Of CS&EPage 30

  • 7/31/2019 Unix Command Unit 1

    31/40

    Unix and shell programming

    For example:

    $ mkdir test

    mkdir: Failed to make directory test; Permission denied

    This can happen due to these reasons

    The directory test may already exist

    There may be an ordinary file by that name in the current directory

    The permissions set for the current directory doesnt permit the creation of

    files and directories by the user.

    4] rmdir: REMOVING DIRCTORIES

    The rmdir (remove directory) command removes directories.

    For example:

    To remove directory pis:

    $ rmdir pis

    It is possible to remove or delete more than one directory with one rmdir

    command.

    For example:

    The three directories and subdirectories which created using mkdir in above

    example can be removed by using rmdir with reversed set of argument:

    $ rmdir pis/data pis/progs pis

    When you delete a directory and its subdirectories, a reverse logic of mksir

    has to be applied. The following directory sequence used by mkdir is invalid in

    rmdir.

    For example:

    $ rmdir pis pis/progs pis/data

    rmdir:directory pis : Directory not empty

    Rashmi Shenoy K

    Dept Of CS&EPage 31

  • 7/31/2019 Unix Command Unit 1

    32/40

    Unix and shell programming

    rmdir has silently deleted the lowest level subdirectoriespis/progs andpis/

    data.

    The error message above leads to two important rules

    You cant delete a directory unless it is empty. You cant remove a subdirectory unless you are placed in a directory

    which is hierarchically above the one you have chosen to remove.

    If you try to remove the directory progs by executing the command from the

    same directory itself:

    $ cd progs

    $pwd

    /home/kumar/pis/progs

    $ rmdir /home/kumar/pis/progs

    rmdir: directory /home/kumar/pis/progs: Directory does not exist

    To remove this directory, the user must position himself in the directory

    above progs, ie, pis and then remove it from there

    $ cd /home/kumar/pis

    $pwd

    /home/kumar/pis

    $ rmdir progs

    The mkdir and rmdir command work only in directories owned by the user.

    ABSOLUTE PATHNAME:

    Many UNIX commands use file and directory names as arguments which are

    presumed to exist in the current directory.

    For example: $ cat login.sqlwill work only if the file login.sqlexists in your

    Rashmi Shenoy K

    Dept Of CS&EPage 32

  • 7/31/2019 Unix Command Unit 1

    33/40

    Unix and shell programming

    current directory. If you are placed in/usrand want to access login.sql in/home/

    kumar, you cant use above command but rather the pathname of the file, ie, $ cat /

    home/kumar/login.sql

    If the first character of the pathname is/ [root]. Such a pathname is calledas absolute pathname. When you have more than one / in the pathname, for each

    such /, you have to descend one level in the file system. Thus kumaris one level

    below home, and two level below root.

    No two files in a UNIX system can have identical absolute pathnames. You

    can have two files with the same name, but in different directories, their pathname

    also will be different. Thus, the file /home/kumar/progs/c2f.plcan coexist with the

    file/home/kumar/safe/c2f.pl

    RELATIVE PATHNAME:

    Using . and .. in relative pathname:

    Navigation becomes easier by using a common ancestor ie, /home as

    reference. UNIX offers a shortcutthe relative pathnamethat uses either the

    current or parent directory as reference, and specifies the path relative to it. A

    relative pathname uses one of these cryptic symbols:

    . (a single dot)This represents the current directory .. (two dots)This represents the parent directory

    Using .. to frame relative pathnames :

    Assume that you are placed in/home/kumar/progs/data/text, you can use ..

    as argument to cd command to move to the parent directory,/home/kumar/progs/

    data

    $pwd

    /home/kumar/progs/data/text

    $ cd..

    $pwd

    Rashmi Shenoy K

    Dept Of CS&EPage 33

  • 7/31/2019 Unix Command Unit 1

    34/40

    Unix and shell programming

    /home/kumar/progs/data

    This method is compact and more useful when ascending hierarchy. You

    can combine any number of such sets of.. separated by /s. when a / is used with ..

    it acquires a different meaning, instead of moving down a level, it moves one levelup. For example, to move to /home, you can always use cd/home. Alternatively

    you can also use a relative pathname:

    $pwd

    /home/kumar/pis

    $ cd../..

    $pwd

    /home

    Using . to frame relative pathname:

    A directory which uses the current directory as argument can also work with a

    single dot.

    $ cp home/sharma/.profile .

    This means that the cp command which also uses a directory as the last argumentcan be used with a dot. This copies the file .profile to the current directory (.).

    ls : LISTING DIRECTORY CONTENTS:

    ls command is used to display the list of files in the system.

    The complete list of filenames in the current directory is arranged I ASCII

    collating sequence, with one filename in each line. It includes directories also.

    For example:

    $ ls

    08_packets.html NUMERICALS FIRST

    Rashmi Shenoy K

    Dept Of CS&EPage 34

  • 7/31/2019 Unix Command Unit 1

    35/40

    Unix and shell programming

    TOC.sh UPPERCASE LETTER NEXT

    calendar LOWERCASE LETTER

    cptodos.sh

    dept.lst

    emp.lst

    helpdir

    progs

    Directories often contain many files. To know whether a particular file is

    available or not, you can use ls command.For example: To know whether the file calendar is present or not

    $ ls calendar

    calendar

    and if perl is not available, the system displays

    $ ls perl

    perl: No such file or directory

    ls command can also be used with multiple filenames and has options that

    list most of the file attributes.

    ls OPTIONS:

    ls has a large number of options.

    1] Output in multiple columns (-x)

    -x option is used to display the output in multiple columns

    For example:

    Rashmi Shenoy K

    Dept Of CS&EPage 35

  • 7/31/2019 Unix Command Unit 1

    36/40

    Unix and shell programming

    $ ls x

    08_packets.html TOC.sh calendar cptodos.sh dept.lst emp.lst

    helpdir progs

    2] Identifying directories and executables (-F)

    The F option is used to identify directories and executable files.

    For example:

    $ ls F

    08_packets.html

    TOC.sh *

    calendar*

    cptodos.sh*

    dept.lst

    emp.lst

    helpdir/

    progs/

    The use of two symbols, * and / as type indicators. The * indicates that the

    file contains executable code and the / indicates the directories.

    3] Showing hidden files (-a)

    There are some hidden files in the system. To see the hidden files a option

    is used with ls command.

    For example:

    $ ls a

    ./

    Rashmi Shenoy K

    Dept Of CS&EPage 36

  • 7/31/2019 Unix Command Unit 1

    37/40

    Unix and shell programming

    ../

    .exrc

    .kshrc

    .profile.rhosts

    .sh_history

    .xdtsupCheck

    .xinitrc

    08_packets.html*

    TOC.sh *

    calendar*

    cptodos.sh*

    dept.lst

    The first two files (. and ..) are special directories. These symbols have the

    same meaning as relative pathname here.

    4] Listing directory contents

    $ ls x helpdir progs

    helpdir:

    forms.obd graphics.obd reports.obd

    progs:

    array.pl cent2fah.pl n2words.pl name.pl

    The contents of the directory are listed, consisting of Oracle documentation

    in the helpdir and a number of perl program files in progs.

    5] Recursive listing (-R)

    Rashmi Shenoy K

    Dept Of CS&EPage 37

  • 7/31/2019 Unix Command Unit 1

    38/40

    Unix and shell programming

    The R option lists all files and subdirectories in a directory tree.

    For example:

    $ ls xR

    08_packets.html TOC.sh calendar cptodos.sh dept.lst emp.lst

    helpdir progs

    ./helpdir:

    forms.obd graphics.obd reports.obd

    ./progs:

    array.pl cent2fah.pl n2words.pl name.pl

    The list shows filenames in three sectionsthe one under the home

    directory and those under the subdirectories helpdirandprogs.

    ./helpdir indicates that helpdir is a subdirectory under.

    THE UNIX FILE SYSTEM:

    The UNIX file system consists of two groups.

    The first group contains the files that are made available during system installation:

    /bin and /usr/bin These are the directories where all the commonly used

    UNIX commands are found.

    /sbin and /usr/sbin There are some commands which the user cannot

    execute. The commands can be executed only by system administrator, such

    commands are found in this directory

    / etc This directory contains the configuration files of the system. The

    login name and password are stored in files /etc/passwd and /etc/shadow /dev This directory contains all device files. These files dont occupy space

    on disk. There could be more subdirectories like pts, dsk and rdsk in this

    directory.

    /lib and /usr/lib This directory contains all library files in binary form

    /usr/include This directory contains the standard header files used by C

    Rashmi Shenoy K

    Dept Of CS&EPage 38

  • 7/31/2019 Unix Command Unit 1

    39/40

    Unix and shell programming

    programs. The statement #include used in most C programs refers

    to the file stdio.h in this directory

    Rashmi Shenoy K

    Dept Of CS&EPage 39

  • 7/31/2019 Unix Command Unit 1

    40/40

    Unix and shell programming

    /usr/share/man This is where the man page is stored. There are separate

    subdirectories that contain the pages for each section. For example, the man

    page of ls can be found in /usr/share/man/man1, where 1 in man1 represents

    Section 1 of the UNIX manual

    The contents of these directories would change as more software and utilities

    are added to the system. Users work with their own file, write programs, send

    and receive mails and create temporary files. The second group contains

    /tmp The directories where users are allowed to create temporary files.

    These files are wiped away regularly by the system.

    /var The variable part of the file system. It contains all outgoing and

    incoming mails

    /home On many systems users are housed here. For example, kumarwouldhave his home directory in/home/kumar.