Basic Unix Training

  • Upload
    vivek

  • View
    230

  • Download
    0

Embed Size (px)

Citation preview

  • 8/12/2019 Basic Unix Training

    1/40

    UNIX TRAINING IBASICS

  • 8/12/2019 Basic Unix Training

    2/40

    Tejas Networks Confidential6 September, 20002

    What is Unix?

    Manages computer resources (for example, CPUtime, memory etc.,)

    Invented at AT&T Bell Labs in early 70s.Some workstation variants SunOS and Solaris (From Sun Microsystems), HP-UX

    (From Hewlett Packard), AIX (from IBM)

    Some PC & Mac variants NetBSD, OpenBSD, Linux

  • 8/12/2019 Basic Unix Training

    3/40

    Tejas Networks Confidential6 September, 20003

    Why Unix?

    Multiple users can use it at the same time (unlikewindows where one has to logout before someoneelse can use it!)Resources are used effectivelySecureAvailable on many systems from PCs tosupercomputersUsage transparency - basic commands are same

  • 8/12/2019 Basic Unix Training

    4/40

    Tejas Networks Confidential6 September, 20004

    Using Unix

    Every user has A User Name Password (a secret code) dont share this!!! A working area called home directory

    One or more users are clubbed into a group. Forexample, people working on the same project.A user avails Unix services by logging in. Thiscreates a login session. Enter your login name System prompts for the password Enter your password

  • 8/12/2019 Basic Unix Training

    5/40

    Tejas Networks Confidential6 September, 20005

    Using Unix (contd)

    After logging in Unix runs a program for youcalled a shell and you land up in your homedirectory.The shell displays a shell prompt where you cantype in the commands.A user can stop using unix by logging out. This

    closes the login session. Type logout on the screen OR Type exit on the screen

  • 8/12/2019 Basic Unix Training

    6/40

    Tejas Networks Confidential6 September, 20006

    What is Shell?

    Shell is a program that reads the commandsthat you type in, executes them and display

    the results for you.Shell sets up your working environment.Different shells are available on Unix. For

    example, csh and bash.

  • 8/12/2019 Basic Unix Training

    7/40

    Tejas Networks Confidential6 September, 20007

    Directories and Files

    UNIX computer systems store data on disk drivesusing a structure called a filesystem . There are many different types of filesystems User accesses directories and files in the same way

    irrespective of the filesystem type.

    UNIX filesystems store information in a hierarchy

    of directories and files.A filesystem is created when a disk is formattedwith the proper UNIX utilities/commands.

  • 8/12/2019 Basic Unix Training

    8/40

    Tejas Networks Confidential6 September, 20008

    Directories and Files (contd) - Example

    Unix Root Directory/

    bin dev etc home lib proc sbin tmp usr var

    modules bin lib tmp

  • 8/12/2019 Basic Unix Training

    9/40

    Tejas Networks Confidential6 September, 20009

    Directories and Files (contd)

    Directories are like folders in the filesystem .Users can group files together inside thedirectories.Directories are represented as files withspecial properties.The top level of the UNIX filesystem issymbolized by the " /" character. Thisdirectory has a special name - it is called the"root " directory .

  • 8/12/2019 Basic Unix Training

    10/40

    Tejas Networks Confidential6 September, 200010

    Unix Directories and Files

    The directories contain data in the form offiles.

    Files have a name associated with them.They are CASE SENSITIVE ! (Contrast toWindows)

    In Unix all the commands are stored as filesin the directory system.

  • 8/12/2019 Basic Unix Training

    11/40

    Tejas Networks Confidential6 September, 200011

    Get a Feel for Unix Commands

    At the shell prompt type cd /. This changes theworking area to the root directory of thefilesystem.

    At the shell prompt type in the word ls. Thisdisplays the list of directories and files containedin the root directory.Type cd. This takes you right back to the homedirectory. Type ls again. What do you see? (Thisis an exercise for you)Type echo $HOME at your shell prompt. This

    prints your home directory name.

  • 8/12/2019 Basic Unix Training

    12/40

    Tejas Networks Confidential6 September, 200012

    How do I identify Unix files and directories?

    Any file (note that directory is also a file) is identified apath name. Path name basically is a list of directories separated by /

    to the point where the file exists in the filesystem. Absolute path describes the path from the root of the file system.

    Example: the absolute path of your home directory is/home/.

    Relative path describes the path with respect to the currentdirectory. Example: relative path of the file example.txt in your bindirectory in your home directory is (assuming it exists),

    bin/example.txt

  • 8/12/2019 Basic Unix Training

    13/40

    Tejas Networks Confidential6 September, 200013

    Gee! I dont know how to use this command!

    If you know that a command exists in Unix forsome purpose and dont know how to use it,DONOT PANIC! Unix has an Online help system.How to use Unix help system. Type man commandname at the shell prompt. Type man man to get help on the help system.

    Exercise: Type man man and find out what it says Type man cd and find out what it says

  • 8/12/2019 Basic Unix Training

    14/40

    Tejas Networks Confidential6 September, 200014

    Working with files Basic File Commands

    ls (list) list files anddirectoriescat (concatenate)

    print one or more filesto the screenmv (move) renamesa filecp (copy) copies thecontents of a file to anew one.

    rm (remove) delete afile.less look at thecontents of a file onescreen at t time.tail look at the endof the filediff report thedifferences betweenfiles

  • 8/12/2019 Basic Unix Training

    15/40

    Tejas Networks Confidential6 September, 200015

    Working with Directories Basic Commands

    mkdir (make directory) create a directoryif it doesnt exist.

    rmdir (remove directory) remove adirectory (if its empty)

    pwd (present working directory) display

    the directory youre in.

  • 8/12/2019 Basic Unix Training

    16/40

    Tejas Networks Confidential6 September, 200016

    File Access Permissions

    Since Unixs are used by multiple users, permissions areassociated with files and directories to protect them againstmodification/deletion by others.

    Types of permissions For reading a file For writing a file For executing a file

    Level of permissions

    Owner/Creator of the files (u = user) Group in which the owner of the file belongs to (g = group) For the rest of the world (o = others) Everyone (a = all)

  • 8/12/2019 Basic Unix Training

    17/40

    Tejas Networks Confidential6 September, 200017

    File Access Permissions (contd)

    Unix command to change the file access permissions at various levels: chmod

    (change mode) Usage: chmod level+permission filenameLevel (u = user/owner, g=group, o=others, a=all)Permission type (r=read, w=write, x=execute)

    Example, chmod g+w file.txt gives the write permission to the people belonging to file.txtsowners group.

  • 8/12/2019 Basic Unix Training

    18/40

    Tejas Networks Confidential6 September, 200018

    File Access Permissions (contd) ChangingOwner

    chown (Change Owner) Usage: chown username filename

    Example: chown trainee file.txt changes theowner of the file.txt to trainee. Note that theuser ID should be created in the system for thecommand to succeed.

  • 8/12/2019 Basic Unix Training

    19/40

    Tejas Networks Confidential6 September, 200019

    File Archiving/Compressing Commands

    Archiving puttingtogether multiple filesinto a single one. Tar read/write data

    from/to tape OR multi-file archive on disk.

    Cpio read/write datafrom/to tape OR multi-file archive on disk

    Compressing shrinkingthe file size. compress compression

    utility (compressed fileswith suffix .Z) gzip/gunzip GNU

    compression/uncompression utility. Compressed file

    suffix is .gz zip/unzip public domainsoftware compatible withDOS zip.

  • 8/12/2019 Basic Unix Training

    20/40

    Tejas Networks Confidential6 September, 200020

    Unix Processes

    Unix lets multiple users run commands atthe same time.

    Unix lets an individual users run commandsat the same time.When a command is executing in thecomputers memory, its called a process. Each process has a unique integer toidentify it, called PID (Process ID).

  • 8/12/2019 Basic Unix Training

    21/40

    Tejas Networks Confidential6 September, 200021

    Whos running now?? The ps command!

    Check the status of the processes: ps(process status)

    ps optionsType ps ef or ps ax to display all processes in theUnix systemType ps fu trainee to display all processes associatedwith the user trainee

  • 8/12/2019 Basic Unix Training

    22/40

    Tejas Networks Confidential6 September, 200022

    ps command continued!

    How to check if the process is running?Check the process state S Sleeping R Runnable

  • 8/12/2019 Basic Unix Training

    23/40

    Tejas Networks Confidential6 September, 200023

    View the processes interactively!

    Top look at the process activity in realtime

    Run the command top and see what it shows The information shown on the screen isconfigurable.

    Exercise: View only selected fields. Exercise: View only selected processes.

  • 8/12/2019 Basic Unix Training

    24/40

    Tejas Networks Confidential6 September, 200024

    How to terminate processes?

    If you want a process to terminate, you canuse the command kill.

    Usage: kill [optional signal name] PIDUnix sends a signal to process. Process dies when it receives a signal (default

    behavior).The default may be overridden.How do you ensure that a process is killed? Run the command kill KILL PID

  • 8/12/2019 Basic Unix Training

    25/40

    Tejas Networks Confidential6 September, 200025

    Jobs and Job control

    You will have to wait till the command completes before you can execute the next command.Such commands are called foreground ORinteractive commands.How to save time due to foreground commands? Detach the running command from the terminal and run

    it in the background.

    Background commands are called jobs. Jobs can be Suspended/resumed Brought to foreground

  • 8/12/2019 Basic Unix Training

    26/40

    Tejas Networks Confidential6 September, 200026

    Know Thy Shell!

    Shell gives you the working environment.Environment is a set of initializations. Theinitializations are done in: /etc/profile (DO NOT EDIT! Common for all users) ~/.profile ~/.bashrc

    What can you initialize? Shell variables Shell functions Command aliases

  • 8/12/2019 Basic Unix Training

    27/40

    Tejas Networks Confidential6 September, 200027

    Know thy Shell Variables!

    Strings of characters that hold values. Example, $HOMEstands for your home directory.To see all your built- in shell variables type set at yourshell prompt.Setting the value of a shell variable: At the prompt type, variablename=value

    Displaying the value of a shell variable: At the prompt, type echo $variablename

    Example: Type MYVAR=simple string at the shell prompt. Type echo $MYVAR at the shell prompt. What do you see?

  • 8/12/2019 Basic Unix Training

    28/40

    Tejas Networks Confidential6 September, 200028

    Know Shell Variables Of Your Environment

    Some environment variables are special: $HOME (your home directory) $PATH (directories which are to be searched

    for commands)Exercise: Adding current directory to your search path

    $TERM (your terminal type)

    $SHELL (name of your shell) $PWD (current working directory) $OLDPWD (previous working directory)

  • 8/12/2019 Basic Unix Training

    29/40

    Tejas Networks Confidential6 September, 200029

    Special Characters of Shell

    Shell uses some special characters ~ denotes your home directory.

    Example: cd ~harish will take you to the homedirectory of the user harish.Recap: Also denoted by $HOME.

    . Denotes the current directory

    .. Denotes the directory that contains thecurrent directory (also known as parentdirectory).

  • 8/12/2019 Basic Unix Training

    30/40

    Tejas Networks Confidential6 September, 200030

    Special Characters (contd) Filenamesubstitution

    * substitute any character(s) at the position. Example: Type ls *.c. Displays all the C

    source files in the working directory.? substitute any character at a given position. Example: Type ls file?.c. Displays files with

    names file1.c file2.c . filex.c etc., Exercises

  • 8/12/2019 Basic Unix Training

    31/40

    Tejas Networks Confidential6 September, 200031

    Filename Substitution (contd)

    How to specify a range of characters at a position? Enclose the range in [ and ].

    Example: [A-Z], [a-z], [0-9]. Exercise: Putting things together. What wouldthe command ls [1 -3]?abcd*.* do?

  • 8/12/2019 Basic Unix Training

    32/40

    Tejas Networks Confidential6 September, 200032

    Commands - Redirecting Input and Output

    Shell lets you send the output of a command to a file ofyour choice by using > for creating/overwriting or >> appending

    between the command and the output filename. Type ls l / > /tmp/out.txt at your shell prompt. The directory

    contents of the root directory is now in the file out.txt in the /tmpdirectory.

    Shell lets you specify the input to a command using

  • 8/12/2019 Basic Unix Training

    33/40

    Tejas Networks Confidential6 September, 200033

    I/O Redirection - Pipes

    How to create? command1 | command2

    What does it do? To send the output from command1 as the input to

    command2.

    What is it used for? To build complex shell commands.

    Example: Type ps aux|less. What will it do? You can see the

    output of the ps command one screenful at a time.

  • 8/12/2019 Basic Unix Training

    34/40

    Tejas Networks Confidential6 September, 200034

    Programming Shell! - Scripts

    Shell script A collection of Unixcommands which are executed together.

    Whats the use? Combine the individual commands to perform

    specialized and complex tasks.

    How does the shell let me combine thecommand? Provides a simple programming language!

  • 8/12/2019 Basic Unix Training

    35/40

    Tejas Networks Confidential6 September, 200035

    Quick Diversion Editing your programswith Vi!

    Vi Screen oriented editor Extremely powerful

    Mapping keyboard strokes to commandsPattern substitutionCutting and pasting textAnd much more!

    Addictive : Youll never leave it once you know it! Two modes Command Mode (controlling the editing) Editing Mode (enter the text)

  • 8/12/2019 Basic Unix Training

    36/40

    Tejas Networks Confidential6 September, 200036

    Editing with Vi (contd) Entering andQuitting!

    Entering Vi Type vi OR vi filename. By default the Vi is in command mode when you enter.

    Quitting In the quit command you can optionally provide the filename

    Type :wq [optional_filename] to save and quit

    Type :q to quit (the edited file should be alreadysaved) Type :q! to quit without saving

  • 8/12/2019 Basic Unix Training

    37/40

    Tejas Networks Confidential6 September, 200037

    Editing with Vi (contd) - Command Mode

    How to enter? Type ESCAPE key on the key board

    Enter editing mode :Press i to insert new characters at a position Press a to append characters after current position

    Save the edited text : Type :w filename Find and replace text : Type %s/oldpattern/newpattern/

    Navigate through lines and wordsPress h: one character left Press l: one character right Press k: one line up Press j : one line down

  • 8/12/2019 Basic Unix Training

    38/40

    Tejas Networks Confidential6 September, 200038

    Editing with Vi (Contd)

    More infohttp://www.thomer.com/thomer/vi/vi.html http://www.vim.org/

    http://www.thomer.com/thomer/vi/vi.htmlhttp://www.vim.org/http://www.vim.org/http://www.thomer.com/thomer/vi/vi.html
  • 8/12/2019 Basic Unix Training

    39/40

    Tejas Networks Confidential6 September, 200039

    Shell Programming Your First Program

    Type vi example -1.sh at your shell prompt. Enter editing mode

    Type in echo Hello world in the vi screen. Save and quitType chmod a+x example.sh at your command

    prompt. Remember: You need execute permissionto execute anything in Unix.Type ./example -1.sh. What do you see?

  • 8/12/2019 Basic Unix Training

    40/40