Unix Basics.pdf

Embed Size (px)

Citation preview

  • 7/30/2019 Unix Basics.pdf

    1/185

  • 7/30/2019 Unix Basics.pdf

    2/185

  • 7/30/2019 Unix Basics.pdf

    3/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice3

    Introduc

    tion

  • 7/30/2019 Unix Basics.pdf

    4/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice4

    4

    What is Unix?

    A multiuser, multi-tasking operating system that runs on a wvariety of computer systems from personal computers tomainframes.

    A Set of programs /procedures which enables users to shareand efficiently manage the resources and allow users to intera

    with the system to run there programs.Windows XP, Linux, HP UX are examples of a operating sysEach Operating System is build for particular type of compuarchitecture. (such as Intel x86 or Sun SPARC).

  • 7/30/2019 Unix Basics.pdf

    5/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice55

    Several people can use a UNIX computer at the same time;UNIX is called a multiuser system.

    A user can also run multiple programs at the same time; hecalled multitasking.

  • 7/30/2019 Unix Basics.pdf

    6/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice66

    Components of OS

    Process ManagementMemory Management

    I/O System Management

    File ManagementProtection/Security System

  • 7/30/2019 Unix Basics.pdf

    7/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice77

    Unix HistoryThe Unix Operating System was born in 1969.

    Invented by Ken Thompson and Dennis Ritchie at Bell Labs

    Unix was originally written in assembly Language.

    Programs written in Assembly are machine - dependent.

    To make Unix portable Ken Thompson and Dennis Ritchierewrote Unix in C (1973). They created C Language tomake Unix portable.

  • 7/30/2019 Unix Basics.pdf

    8/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice88

    Unix HistoryUNIX was originally developed in the early 1970s by

    AT&Ts Bell Laboratories

    It was so useful an operating system that other organizations (expressed interest in developing versions of their own, and werethe source code for free.

    Soon all the large computer vendors were marketing their owUNIX optimized for their own computer architectures, boastingand features (including Microsofts own effort: Xenix)

  • 7/30/2019 Unix Basics.pdf

    9/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice99

    Unix HistoryIt quickly became apparent that, although UNIX systems w

    available everywhere, they seldom were able to interoperatewithout significant effort.

    In 1987, the two leading vendors of UNIX AT&T (System V

    and Sun Microsystems (BSD) combined their efforts to prodSystem V Release 4 (SVR4), foisting what they hoped was anew standard upon the UNIX world

    http://www.levenez.com/unix/

  • 7/30/2019 Unix Basics.pdf

    10/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice1010

    This only served to further divide the industry, and many othemarket players banded together to develop their own open UNIXcalled OSF/1 (Open Software Foundation UNIX version 1).

    To introduce a sense of unity, an organization called X/Open bputting in place a set of open standards that would allow greateinteroperability between UNIXs.

    In 1993, AT&T sold their UNIX business to Novell, who sold X/Open (now The Open Group).

    Linus Torvalds ported a version of UNIX to the PC and gave source code to the community at large Linux was born.

    Unix History

  • 7/30/2019 Unix Basics.pdf

    11/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice1111

    BSD : Berkeley, BSD

    SunOS : Sun Microsystems, BSD/sys 5Solaris : Sun Microsystems, Sys 5/BSDUltrix : Digital Equipment Corporation, BSDOSF 1 : Digital Equipment Corporation, BSD/sy

    HPUX : Hewlett-Packard, Sys 5AIX : IBM, Sys 5 / BSDIRIX : Silicon Graphics, Sys 5GNU/Linux : GNU, BSD/Posix

    Which Unix

  • 7/30/2019 Unix Basics.pdf

    12/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice1212

    UNIX Variant Company AIX IBM

    A/UX AppleFreeBSD (free)HP-UX Hewlett-PackardIRIX Silicon GraphicsLinux (free, PC) VariousNEXT NextSCO-UNIX (PC) Santa-Cruz Organisation

    Solaris SunUltrix DigitalUnixWare (PC) Novell (now SCO)QNX (real-time) Quantum Software

    Unix Variants

  • 7/30/2019 Unix Basics.pdf

    13/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice1313

    Application programs

    DBMS

    FTP

    Mail

    Hardware

    Kernel

    Sh

    csh ksh

    more

    vi

    cppcut

    ed

    Unix Architecture

  • 7/30/2019 Unix Basics.pdf

    14/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice1414

    Hardware

    KernelShell Command interpreter

    Application and utilitiesUser

    Unix Architecture

  • 7/30/2019 Unix Basics.pdf

    15/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice1515

    The kernel is the heart of the operating system. It intehardware and most of the tasks like memory management, taskfile management.

    The shell is the utility that processes your requests. Wcommand at your terminal, the shell interprets the command aprogram that you want. The shell uses standard syntax for all c

    C Shell, Bourne Shell and Korn Shell are most famous shells wavailable with most of the Unix variants.

    Unix Architecture

  • 7/30/2019 Unix Basics.pdf

    16/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice1616

    There are various command and u

    which you would use in your day to day activities.cp, mv, cat and grep etc. are few examples of commands and u

    All data in UNIX is organized into fileare organized into directories. These directories are organized ilike structure called the filesystem.

    Unix Architecture

  • 7/30/2019 Unix Basics.pdf

    17/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice1717

    Core of the UNIX operating system is the kernel

    Communicates between users Software and Computer HDecides how to communicate with PeripheralsDecides how to allocate memory for the user and systemHow to share time among UsersHow to efficiently make use of the processor time

    Records the details of all the running programsManages the files and directories.Largely written in C, with some parts written in AssembPerforms all low level functions.

    Kernel

  • 7/30/2019 Unix Basics.pdf

    18/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice1818

    Memory management.

    Process scheduling.

    File management and security.

    Interrupt handling and error reporting.

    Input / Output services.

    System accounting

    Kernel Functions

  • 7/30/2019 Unix Basics.pdf

    19/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice1919

    Users cannot access Kernel directly.User always interacts through a program called Shell.

    Acts as an interface between the user and the kernel.Shell interacts with Kernel to get the job done.Shell is a command interpreter for UNIXAccepts commands , interprets them and executes it.Provides powerful programming capabilities.System administration.

    Text processing.File management.Allows to customize the users own shell.

    Shell

  • 7/30/2019 Unix Basics.pdf

    20/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice2020

    Shell FunctionsFilename completion

    Command-line history.

    Multiple job control.

    Redirection (program to file).

    Pipes (program to program).

    Storage variables.

  • 7/30/2019 Unix Basics.pdf

    21/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice2121

    ComputerHardware

    OperatingSystem

    ApplicationSoftware

    Userinputs

    Service Request

    Hardware Instr

    Output

    Service Response

    Processing Results

    System interaction

  • 7/30/2019 Unix Basics.pdf

    22/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice2222

    Hardware

    Service Layer

    Command Layer or Shell

    Users

    Unix Kernel

    Service Layer

  • 7/30/2019 Unix Basics.pdf

    23/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice2323

    The Service Layer consists of set of programs that provide the

    following type of services :

    Access to I/O devicesAccess to storage devicesFile manipulation

    Service Layer

  • 7/30/2019 Unix Basics.pdf

    24/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice2424

    Boot Processis the process of starting up a computer from a

    halted or powered-down condition.

    Boot process:The memory-resident code

    Runs self-test.Probes bus for the boot deviceReads the boot program from the boot device.

    Boot program reads in the kernel and passes control to it.Kernel identifies and configures the devices.Initializes the system and starts the system processes.Brings up the system in single-user mode (if necessary).Runs the appropriate startup scripts.Brings up the system for multi-user operation.

  • 7/30/2019 Unix Basics.pdf

    25/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice2525

    Most Unix systems implement a two-stage loading process.

    A small boot program is read into memory from a default odevice.

    This program loads the Kernel and give control to it.

    The path to the kernel is vendor-dependent.

    For example, it is /vmunix on SunOS 4.x, Digital Unix andUltrix, /kernel/unix on SunOS 5.x, /hp-ux on HP-UX, and /IRIX and AIX systems.

    Loading Kernel

  • 7/30/2019 Unix Basics.pdf

    26/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice2626

    Kernel always runs in a fixed amount of memory. Usually amount of physical memory and Used memory when loadincompletes.

    Kernel will start probing for the devices, bus and other harconfigurations attached to the system.

    Devices which are not initialized during the kernel processwork, until it is connected correctly and kernel is restarted.

    Loads the init process, which always run as the process 1

    Loading Kernel

  • 7/30/2019 Unix Basics.pdf

    27/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice2727

    The /etc/init daemon is executed.

    It in turns starts up many other processes and uses the /etc/ito loaded site specific programs and configuration scripts.

    This file usually has /etc/init create processes that allow useto the system.

    The /etc/init daemon considers the system to be in a run levinitialization state at any given time.

    The processes created by init for each of these run levels isthe /etc/inittab file.

    Loading Kernel

  • 7/30/2019 Unix Basics.pdf

    28/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice2828

    UNIX system has several modes of operation called syste( or run levels). These are:

    0 Shutdown State1 Administrative States or S Single User State2 Multi-User State6 Stop and Reboot State

    These states are passed to the init program, which operatesystem in the appropriate state.

    Init Run Levels

  • 7/30/2019 Unix Basics.pdf

    29/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice2929

    Hardware configuration check.

    The operating system file /vmunix ( the UNIX kernel) gets l

    The kernel brings up the swapper and the init program.

    Jobs of init are:

    Takes the system into the multi-user mode.Checks the integrity of the file systems.Mounts the file systemsThe login process gets executed.After successful login, the shell appears.

    Boot Process

  • 7/30/2019 Unix Basics.pdf

    30/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice30

    Login and Logou

    Login

  • 7/30/2019 Unix Basics.pdf

    31/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice3131

    LoginLogin

    As soon as you turn on the power, system starts booting up aprompts you to log into the system.

    Have your userid (user identification) and password ready.Type your userid at the login prompt, then press ENTER. Yo

    case-sensitive, so be sure you type it exactly as your system ainstructed.

    Type your password at the password prompt, then press ENTpassword is also case-sensitive.)

    If you provided correct userid and password then you wouldallowed to enter into the system.

    Read the information and messages that come up on the scr

    Login

  • 7/30/2019 Unix Basics.pdf

    32/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice3232

    LoginLogin

    Seeing prompt indicates that the new shell has beenand ready to accept commands.

    Provides information about the last login.

    Login

  • 7/30/2019 Unix Basics.pdf

    33/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice3333

    Super UserLogin

    Special user in the UNIX systems called root.

    Created during the Unix installation Process.The root user has privileges to do all system administration

    UNIX system has a unique number associated with the usecalled a User ID.

    The root user has ID 0; Therefore, any user with that ID haprivileges.

    Superuser and common user command prompts differ: $ recommon user, while # represents a superuser

    Login

  • 7/30/2019 Unix Basics.pdf

    34/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice3434

    Execute Few commandsLogin

    cal : displays the calendar for the current month

    Login

  • 7/30/2019 Unix Basics.pdf

    35/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice3535

    All Unix systems require passwords to help ensure that your fdata remain your own and that the system itself is secure from

    To start, type at command prompt.

    Enter your old password the one you're currently using.

    Type in your new password.Always keep your password complex enough so that no bod

    guess it. But make sure, you remember it.

    You would need to verify the password by typing it again.

    Change PasswordLogin

    Login

  • 7/30/2019 Unix Basics.pdf

    36/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice3636

    LoginLogout

    Once you have completed all your work, you can logo

    Use the exit command to logout from the Unix SystemTyping the exit commandTyping Ctrl-d(in UNIX command-line programs, Ctrl-d always means:I have finished typing I will be typing nothing further intoprogram)

    Login

  • 7/30/2019 Unix Basics.pdf

    37/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice3737

    Command Description

    halt Brings the system down immediately.

    init 0 Powers off the system using predefined scripts to synchronizeand clean up the system prior to shutdown

    init 6Reboots the system by shutting it down completely and thenbringing it completely back up

    poweroff Shuts down the system by powering off.

    reboot Reboots the system.

    shutdown Shuts down the system.

    The most consistent way to shut down a Unix system propecommand line is to use one of the following commands:

    You need to be the superuser or root (the most privileged acUnix system) to shut down the system.

    ShutdownLogin

    Login

  • 7/30/2019 Unix Basics.pdf

    38/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice3838

    LoginHome Directory

    Just after the login process, a user goes into a default directhat user.

    This default directory has a special significance and is caluser's home directory.

    Every Unix has a HOME directory and it is created duringcreation.

    Usually users' home directories are the same name as the uand are placed in /home

    Ex : if senny is the user, the home would be /home/senny

    Login

  • 7/30/2019 Unix Basics.pdf

    39/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice3939

    gHome Directory

    Different ways of accessing HOME Directory.

    Using cd command will take you to your HOME Director

    When a user logs in

    Using ~ symbol.

    Using $HOME

    Login

  • 7/30/2019 Unix Basics.pdf

    40/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice4040

    gExtrinsic and Intrinsic CommandCommands which are built in to the UNIX Shell are called

    Shell always checks the intrinsic commands first and if theycheck if it is a extrinsic command.

    The command for changing a directory (cd) is a typical intr

    Commands Stored as separate files are called

    These are external commands which are stored as a separateavailable in the path.

    command for listing files (ls) is a typical extrinsic comman

    Login

  • 7/30/2019 Unix Basics.pdf

    41/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice4141

    gChange Your Password

    When you login for the first time, it is always better to change your

    The password can be changed with the command.

    Executing this command, will prompt you for old password for SecuIf the old password is correct,

    Give a new password and confirm the same.

    $ passwdOld password :New password :Re-enter new password :Password changed$

    Login

  • 7/30/2019 Unix Basics.pdf

    42/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice4242

    Shell History and Repeating Comma

    The HP-UX shell keeps the last executed commands in a shell hist

    The command history is saved in a file called .sh_history for each

    This file is kept in the user's home directory .

    User can set number or entries for history (discussed latter).

    Use ESC - K to go to the previous command and K again to go

    Use ESC J to go to the next command and J again for next com

    g

    Login

  • 7/30/2019 Unix Basics.pdf

    43/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice4343

    Command AliasesCommand Aliases provides an option for you to set, your names fo

    It is very helpful, when you need to remember very complex commFor Ex: alias command to . So when I run dir it does what ls d

    To unalias run the following command

    g

    Login

  • 7/30/2019 Unix Basics.pdf

    44/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice4444

    Log in using the user name assigned to you and change your pass

    Try a new password fewer than six characters.

    Also try using a password containing all letters.

    Check if the system date and time are correct.

    Find out the name of your home directory using the pwd comma

    List files in your home directory with the ls -a command and wri

    The names of files that start with a dot (.) character.Create an alias for this command.

    Before logging out, use the history command to list all of the comhave used.

    Lets Understand Betterg

  • 7/30/2019 Unix Basics.pdf

    45/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice45

    Basic Command

    Basic

  • 7/30/2019 Unix Basics.pdf

    46/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice4646

    For each of the following, type the name of the command, aENTERkey (on some UNIX systems this key is labelledRETURNor

    The syntax (structure) of every command entered into the UNIX shel

    Where:prog-name is the name of the program you wish to run(for example, ls)options are single letters prefixed by a (dash, minus signbehaviour of the program (for example, -a). These are alwaymeaning that the program will still do something if no optioarguments are any other words (separated by spaces or tabs) needs to perform the task you wish it to perform (such as a fname). Some programs require a certain number of argumedo not require any.

    Command Syntax

    d ( )Basic

  • 7/30/2019 Unix Basics.pdf

    47/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice4747

    Command Syntax (cont.)Notes:

    . It is not possible to type in the namexpect the shell to know how to open it (the UNIX shell has no conceThe program name must be the name of a binary program or an execuNothing else will run

    . In other words, ls is different from LS

    It is possible to specify the location of the program as well(for example: /bin/ls ./myscript)

    There must be a space between every element of the command-line. cd/ will not run

    The options may be specified in a variety of ways. All of the followin

    ls -lrt ls -l -r -a lslr -t

    Si l C dBasic

  • 7/30/2019 Unix Basics.pdf

    48/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice4848

    Simple Commands

    To know what directory you are currently in.

    You can use the (print working directory) command at any time to find oudirectory name.

    $ pwd/home/senny

    The date command tells you the current date and time

    $ dateThu Aug 29 15:36:23 EDT 2009

    Same command allows you to change the date, but you need to be a superuse

    Si l C dBasic

  • 7/30/2019 Unix Basics.pdf

    49/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice4949

    Simple CommandsWhat if you want to see an unattended terminal on which a user has logged in

    use the whoami command to identify who is logged in.$whoamiSenny

    Other userfull command shorter than the above one

    Tells you the login names of all users logged into the system, along with theirterminal line they are using

    $ whooperator pts/ta Aug 30 16:05Senny pts/tb Aug 30 15:59$

    Si l C dBasic

  • 7/30/2019 Unix Basics.pdf

    50/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice5050

    Simple CommandsAn even shorter command exists in HP-UX, which is, and it does a similar job.

    Additionally, the w command tells how long the system has been up, what the

    time is, and what the logged-in users are doing.

    Curious to know what the system name is or which version of unix is running

    system, use the command.$ -aHP-UX myhp B.11.00 E 9000/800 71678 8-user license

    Si l C dBasic

  • 7/30/2019 Unix Basics.pdf

    51/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice5151

    Simple Commands

    Banners are very useful for printing welcome messages at login time.$ banner Senny

    , for printing the calendar of a particular month.The cal command prints the calendar of the current month, but you can usa number of ways to print the desired calendar.

    A.

    Simple CommandsBasic

  • 7/30/2019 Unix Basics.pdf

    52/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice5252

    Executing Multiple Commands on One Line

    The shell allows users to enter multiple commands on a single line.

    Each command is separated by a semicolon ;Ex : cal;date

    Simple Commands

    G tti H lBasic

  • 7/30/2019 Unix Basics.pdf

    53/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice5353

    Help text is available for every standard UNIX program

    This help is known as the Manual Pages, or man pages

    These are a reference tool, not a how-to guide

    They are used as follows: man prog-name

    For example: man calIt is difficult to determine which program is best for a certain task. A prograthat offers partial help is

    whatisMany versions of the UNIX man pages can be found on the Internet

    Getting Help

  • 7/30/2019 Unix Basics.pdf

    54/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice54

    W ki g ith Fil d Di tFiles and

  • 7/30/2019 Unix Basics.pdf

    55/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice5555

    Working with Files and DirectorSimilar to most operating systems, data in UNIX is stored in files. These

    organised hierarchically into directories (called folders in Windows)

    A useful analogy is to think of a UNIX filesystem as a tree (an upside-dowith the directories being branches, and the files being the leaves

    The top of the tree is called the root directory(called /)

    Every file (and directory) on the tree is named by listing all the branches back to the root, each separated by a /, as follows:

    /usr/tom/documents/reports/sales.txt

    W ki g ith Fil d Di tFiles and

  • 7/30/2019 Unix Basics.pdf

    56/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice5656

    /

    bin usr tmp

    john suetom

    documents email

    returns.txt

    reports proposals

    sales.txt interest.txt invoices.txt

    documents

    etc

    Working with Files and Director

    Working with Files and DirectorFiles and

  • 7/30/2019 Unix Basics.pdf

    57/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice5757

    When working with files and directories, the first and most important inforto find out is: Whats available?

    The program called is used to display file details (most importantly their ndisplay the contents of directories

    Useful options for ls include:

    -l display many file details, including size and security info

    -c arrange list alphabetically in columns

    -r (recursive) display contents of every sub-directory

    Arguments for ls (if there are any) are interpreted as names of either files o

    Working with Files and Director

    Working with Files and DirectorFiles and

  • 7/30/2019 Unix Basics.pdf

    58/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice5858

    If we can identify a directory, we can use the program to go there (e.g.cd

    Documents)

    This means that if we want to manipulate files within that directory, we no to prefix each filename with the directorys name

    The command with no arguments will take us to our home directory

    To determine where you are at any time, use the command

    Working with Files and Director

    Working with Files and DirectorFiles and

  • 7/30/2019 Unix Basics.pdf

    59/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice5959

    The following characters may be used in a filename with no problems:

    a-z A-Z 0-9 . , @ - _ + = :The following characters should be avoided when naming files, because th

    special use with most shells:space ~ ` ! # $ % ^ & * ?

    ( ) ' " [ ] { } ; < > \ |

    There is only one character that is not possible to use within a filename:/

    Working with Files and Director

    Working with Files and DirectorFiles and

  • 7/30/2019 Unix Basics.pdf

    60/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice6060

    Unlike Windows, UNIX has no concept of a file type

    Files do not have associations at least, not using a command-line shell.This means that it is not possible to type in the name of a document andexpect the file to be opened in the appropriate program

    Nor do UNIX filenames have any formal extension a period (.) can beplaced anywhere within a filename, even at the beginning

    Some programs will work more readily with filenames that have certainextensions

    The program file is used to make a guess as to a files contents

    Working with Files and Director

    Working with Files and DirectorFiles and

  • 7/30/2019 Unix Basics.pdf

    61/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice6161

    When using the shell (and many other programs), it is possible to specify

    in a simple manner, by using wildcard characters

    The shells wildcard characters are:* match any characters in the filename(s)

    For example: *.txt mark.* a*b *.*? match any single character

    For example: c?t fred.???? ????x?[] match anysingle character that appears within the brackets

    For example: c[aou]t [a-zA-Z]*.txt [!d]*

    Working with Files and Director

    Working with Files and DirectorFiles and

  • 7/30/2019 Unix Basics.pdf

    62/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice6262

    If a file contains nothing but text (for example, an HTML file), the file can busing a number of programs:

    The simplest program for displaying file contents on the screen

    Allows output to be displayed a page (or a line) at a time

    Display only the first 10 lines of the file(s)

    Display only the last 10 lines of the file(s)

    Working with Files and Director

    Working with Files and DirectorFiles and

  • 7/30/2019 Unix Basics.pdf

    63/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice6363

    Files may be compared to check how similar their contents are

    There are two programs for this, depending upon what is in the file:Used to compare two text files. A complete list of

    output to the screen

    Used to compare two binary (non-text) files. The simple statement about the character location of the

    between the files (if there is one)

    Working with Files and Director

    Working with Files and DirectorFiles and

  • 7/30/2019 Unix Basics.pdf

    64/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice6464

    Files may be duplicated (copied) using the cp commandcp is used in two ways:

    cp file1 file2Create a duplicate of file1 called file2 in the current directory.

    If file2 exists it is overwritten (if you have permission)

    cp file1 [ fileN] dir1All the files file1 fileN are copied to the specified directory

    Working with Files and Director

    Copying Moving and Renaming FFiles and

  • 7/30/2019 Unix Basics.pdf

    65/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice6565

    Copying, Moving and Renaming FSimilarly, files may be moved and renamed using the mv command

    mv is used in two ways:

    mv file1 file2Simply renamefile1 as file2 in the current directory. Iffile2overwritten (if you have permission)

    mv file1 [ fileN] dir1

    All the filesfile1 fileN are moved to the specified directoryd

    Deleting FilesFiles and

  • 7/30/2019 Unix Basics.pdf

    66/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice6666

    Deleting FilesThe program called rm is used to remove (delete) files

    If a file is marked as read-only (more about this later), you will be asked fconfirmation

    This program has several options:

    -i (interactive) Ask for a y/n confirmation before deleting each file

    -f (force) Do not ask for any confirmation, and display no error mesdoes not exist

    -r (recursive) Use with extreme caution!! If any of the arguments isremove the directory and all its contents (including subdirectories)

    Hidden FilesFiles and

  • 7/30/2019 Unix Basics.pdf

    67/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice6767

    Hidden FilesAll files whose names begin with a . are considered hidden files

    This means they are not displayed during a regular ls of that directory, and awildcards(* or ?). For example, they are not removed by the command rm *

    The -a option can be used with ls to list hidden files

    These files are not special in any particular sense. Any file can be renamed

    Hidden files are usually configuration files, such as .profile or .exrc

    The and DirectoriesFiles and

  • 7/30/2019 Unix Basics.pdf

    68/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice6868

    The . and .. DirectoriesTwo useful aliases are automatically created in each directory:

    . This is a shorthand for the current directoryFor example: cp /tmp/*.doc .

    .. This is a shorthand for the parent directory

    For example: mv *.txt ..

    These are treated as hidden

    ~ (tilde) refers to the home directory.

    ~ This is a shorthand for the home directory

    For example: cp /tmp/*.doc ~

    Relative vs Absolute PathsFiles and

  • 7/30/2019 Unix Basics.pdf

    69/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice6969

    Consider the following file/usr/tom/business/reports/june/sales.txt

    There are a number of ways of accessing this file, depending upon your curr

    From /usr/tom

    business/reports/june/sales.txt

    From /usr/tom/business/reports/june

    sales.txtFrom /

    usr/tom/business/reports/june/sales.txt

    From /usr/tom/business/reports/june/drafts

    ../sales.txt

    Relative vs Absolute Paths (conFiles and

  • 7/30/2019 Unix Basics.pdf

    70/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice7070

    (If we had no concept of a current directory, we would always have to use

    filename (path) for any file (known as an absolute path)

    The notion of a current directory was created to allow shorter, simpler spof filenames

    A filename that is specified relative to your current directory is known apath

    Any filename (path) that begins with a / is an absolute path. All others relative paths

    All UNIX programs can handle relative or absolute paths, or a mixture of b

    Working with DirectoriesFiles and D

  • 7/30/2019 Unix Basics.pdf

    71/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice7171

    Working with DirectoriesDirectories can be created using the program

    For example: newdir

    Directories can be removed (deleted) using the program

    For example:

    Directories can only be deleted using if they are empty (contain noeven hidden files)

    To remove a directory that is not empty, use (Note: this will remove subdirectories as well)

    Directories can be renamed and moved using mv in the usual manner

    Finding FilesFiles and D

  • 7/30/2019 Unix Basics.pdf

    72/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice7272

    gThefind program can be used to locate files (amongst other things). It

    find top-directory [criteria and actions]

    For example:

    find /usr/tom -name report.txt print

    Notes:

    The-print option is the default action in some implementations ofhence not necessary

    When using wildcards with the -name criterion, enclose the argument(' ), as follows:

    find . -name '*.txt' -print

    Finding Files (cont.)Files and D

  • 7/30/2019 Unix Basics.pdf

    73/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice7373

    g ( )Thefind program can also be used

    To find files based on other criteria to perform actions on the files fothan display their names)

    For example:

    find . -type f -exec rm -i {} \;

    Files can be found on the basis of any file attribute

    Archiving FilesFiles and D

  • 7/30/2019 Unix Basics.pdf

    74/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice7474

    gConsider the following tasks you may wish to perform on a collection of f

    Move them to another location on the hard diskBack them up onto removable media (such as tape)

    Send them electronically to another machine (perhaps over the Inter

    For these purposes it is necessary to create an archive a single file (or imagecontains all the information about the files and directories (including the filemodification dates and contents)

  • 7/30/2019 Unix Basics.pdf

    75/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice75

    Vi Editor

    Understanding vi

  • 7/30/2019 Unix Basics.pdf

    76/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice7676

    Understanding vivi (short for visual editor) is a UNIX text editor a

    full-screen program used to edit text files, including:

    HTML documentsShell scripts

    Configuration files

    Source code (C, C++, etc)It evolved from the line-based editing programs ed and ex, and shares ma

    editing commands

    vi is notoriously unintuitive and difficult to learn, but conversely, once leamost powerful, and feature-rich editors in the world (on any platform)

    Understanding vi (cont.)

  • 7/30/2019 Unix Basics.pdf

    77/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice7777

    g ( )Owing to the limitations of primitive early keyboards, and unlike mosteditors, when using vi, you will always find yourself in one of two mo

    Command mode, where each key typed represents an editing comma

    Insert mode, where each key typed (except ESC) represents text that insert into the document

    The diagram on the next page summarises this

    Understanding vi (cont.)

  • 7/30/2019 Unix Basics.pdf

    78/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice7878

    g ( )

    Command mode Insert mode

    Shell

    a, A, i, I, o, O, s, S, c, C, etc

    ESC

    Manipulating Files

  • 7/30/2019 Unix Basics.pdf

    79/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice7979

    p gLike most text editors and word processors, vi can be started with a docor without as an "empty canvass", as follows:

    To use vi to edit a file:vi filename(s)

    For example:

    vi script1vi *.txt

    Or to start vi with no file:

    vi

    Manipulating Files (cont.)

  • 7/30/2019 Unix Basics.pdf

    80/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice8080

    p g ( )Once vi has been started, you will find yourself in Command Mode

    To perform some simple editing:

    Use (for example) a to "append" text after the current cursor positionMode)

    Enter some text, for example:The quick brown fox

    Jumps over the Lazy Dog

    Press ESC to return to Command Mode

    We will now look at the many ways in which we may quit vi and save the

    Manipulating Files (cont.):w write (save) the file (only if a name has been specified)

  • 7/30/2019 Unix Basics.pdf

    81/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice8181

    :w write (save) the file (only if a name has been specified)

    :w file write to the specified file (save as)

    :q quit (only if no changes have been made)

    :wq save and then quit:x or ZZ save the file (if changes have been made), then quit

    :q! abandon any changes and quit

    :w! write to a read-only file (that you own)

    :e file open the specified file (if no changes have been made)

    :e! File open the specified file (abandon any changes)

    :e# or ^6 open the last file edited

    :n and :n! open the next file specified on the command line

    :rew rewind to the first file specified on the comm

    :f or ^g display current file details

    Moving Around

  • 7/30/2019 Unix Basics.pdf

    82/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice8282

    gPC keyboard special keys (arrow keys, Page Up, End, etc) sometimes work

    Any command listed below with a can be prefixed with a number n to mo

    Moving on the current line

    SPACE or l Move ahead one character

    BACKSPACE or h Move back one character

    $ Move to the last character on the line^ or 0 Move to the first character on the line

    fX Move to (find) the next instance of ch

    ; Move to the next instance of characte

    Moving Around (cont.)

  • 7/30/2019 Unix Basics.pdf

    83/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice8383

    g ( )Moving between lines

    ENTER or j or + Move to the next line

    k or - Move to the previous line

    ^f Move forward one page (page dow

    ^b Move back one page (page up)

    ^d Move down half a page

    ^u Move up half a page

    G Go to last line in file

    1G Go to first line in file

    nG Go to line n in file

    Moving Around (cont.)

  • 7/30/2019 Unix Basics.pdf

    84/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice8484

    Other move commands

    /pattern Move to the next occurrence of pattern

    n (N) Move to the next (previous) occurrence

    w (b) Move forward (back) one word

    % Find the matching bracket: ( ) [ ] and { }

    ]] ([[) Move to the next (previous) C function

    mx Mark a line with label x

    'x Go to line labelled x

    Basic Editing

  • 7/30/2019 Unix Basics.pdf

    85/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice8585

    The following commands take you into Insert Mode, where text is then return to Command Mode:

    a (i) Append (insert) text after (before) the current curs

    A (I) Append text to the end (beginning) of the line

    o (O) Start (open) a new line after (before) the current li

    s Substitute the current character with text

    cw Change the remainder of the word to new text

    Basic Editing (cont.)

  • 7/30/2019 Unix Basics.pdf

    86/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice8686

    The following commands are also used for basic editing, but do not take you

    x (X) Delete (cut) the char under (before) the cursor

    dd Delete (cut) the current line

    p Put (paste) the recently deleted text

    yy Yank (copy) the current line

    rx Replace the current character with xThe following commands special commands are very useful:

    u Undo the last command

    U Restore the current line to how it was whenyou arrived on it

    . Repeat the last command

    Advanced EditingThe following commands are simply useful:

  • 7/30/2019 Unix Basics.pdf

    87/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice8787

    The following commands are simply useful:>> (

  • 7/30/2019 Unix Basics.pdf

    88/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice8888

    It is possible to specify options that modify the general behaviour of vi

    Use :set to list currently set options (or :set all to list every option)

    "On/off" options (for example, ai) can be specified as follows:

    :set ai (turn option on)

    :set noai (turn option off)

    "Value" options (for example, ts) can be specified as follows:

    : set ts=4

    Configuring vi (cont.)Useful options include:

  • 7/30/2019 Unix Basics.pdf

    89/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice8989

    p :Option Name Type Purpose

    ai autoindent on/off Cause new lines to inherit the indentation o

    ic ignorecase on/off Searches will be case-insensitive

    nu numbers on/off display line numbers

    sw shiftwidth value The number of spaces the shift with >> a

    ts tabstop value The number of spaces to use when display

    If you would prefer these options to be in effect every time you start into a file in your home directory called .exrc

    Ensure every line starts with set

  • 7/30/2019 Unix Basics.pdf

    90/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice90

    Security

    Users and GroupsUNIX i " lti " ti t

  • 7/30/2019 Unix Basics.pdf

    91/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice9191

    UNIX is a "multi-user" operating systemThis means:

    More than one user may interact with (log on to) the system at anymoment

    Each user has a separate set of access privileges for system resouras files)

    Operating System Simultaneous users? Separate pr

    UNIX Y Y

    Windows 2000/XP Y Y

    Windows NT N Y

    Windows 95/98 N N

    Users and Groups (cont.)A i ID h ll h h l i id if h

    Sec

  • 7/30/2019 Unix Basics.pdf

    92/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice9292

    A user is a system ID that allows each user that logs in to identify themsresource-access purposes

    When a collection of users require similar access to a resource, each usercollection may be made a member of a group, and the group given access resource

    The following table shows security-related commands:

    Command Purpose

    who am i Display login information, including user name

    id Display current user and group information

    su username Temporarily run a shell as another user

    newgrp groupname Switch current group

    File Protection Overview (cont

  • 7/30/2019 Unix Basics.pdf

    93/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice9393

    The three permission letters (r, w, and x) mean the following:

    Permission For Files For Directories

    r read view contents (e.g. ls)

    w modify (write) create or delete files

    x execute access (e.g. cd)

    Changing File Permissions

  • 7/30/2019 Unix Basics.pdf

    94/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice9494

    The program used to change file permissions is chmod (short for chanmode)

    There are two distinct methods of using chmod:Symbolic mode

    Numeric modeBoth methods have the following usage:chmod permissions filename(s)

    Only the permissions differs between methodsSymbolic mode is used mainly by beginners. Once a user is comfortawith chmod, they almost always use Numeric mode

    chmod Symbolic Mode

  • 7/30/2019 Unix Basics.pdf

    95/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice9595

    In symbolic mode, permissions are specified by using letters, as follows

    chmod u+w file1 Give the owner write permissio

    chmod g-r file1 Remove read permission for the grThe most common letters are:

    Persons Action Permission

    u owner + add r read

    g group - remove w writeo other = set x execute

    a all

    chmod Symbolic Mode (cont.

  • 7/30/2019 Unix Basics.pdf

    96/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice9696

    Multiple permissions may be specified as follows:

    Give the owner and others write permission, and remove read apermission for the user

    As you can see, to set all 9 permissions could be time-consuming

    chmod Numeric ModeI i d i i ifi d b i h b

  • 7/30/2019 Unix Basics.pdf

    97/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice9797

    In numeric mode, permissions are specified by using three numbersAll permissions are specified in one command

    If we allow that r=4, w=2 and x=1, we can set permissions for each of thtypes of persons (owner, group & others) by adding together the numberscorresponding to the permission we wantFor example:

    chmod 640 file1 rw-r-----

    Give the owner read and write permission, the group read permissionpermissions to anyone else

    chmod 070 file1 ---rwx---

    Give full permissions to the group only

    chmod Numeric Mode (cont.)While there are hundreds of legal combinations, only a few are used comm

  • 7/30/2019 Unix Basics.pdf

    98/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice9898

    Changing File OwnershipChanging the owner of a file is done using the chown program as follow

  • 7/30/2019 Unix Basics.pdf

    99/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice9999

    Changing the owner of a file is done using the chown program, as followchown owner filename(s)

    For example:chown fred *.doc

    Notes:

    Ownership of a file has nothing to do with the location of the file.

    words, a file may be owned by fred but sitting in tom's home directoYou cannot change any permissions on a file that you don't own, so

    changing many things, change ownership last

    If you change the ownership of a file, you cannot changeit back

    Changing File GroupChanging the group ownership of a file is done using the chgrp program

  • 7/30/2019 Unix Basics.pdf

    100/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice100100

    Changing the group ownership of a file is done using the chgrp programchgrp group filename(s)

    For example:chgrp marketing data*

    Notes:

    If you are a member of a group, and that group has permission over

    and when you try to exercise that permission you get a "permission duse the newgrp command to switch your current group

  • 7/30/2019 Unix Basics.pdf

    101/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice101

    Searching File con

    Using grep Searching Fi

  • 7/30/2019 Unix Basics.pdf

    102/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice102102

    grep (global regular expression print) commandIt does the job in a number of ways. You can search for text strings in on

    or many files.

    grep also displays the name of the file in which the string is found.

    It can also display the location in the file where the string is found.

    h fi d h th ti l i t

    Using grep Searching Fi

  • 7/30/2019 Unix Basics.pdf

    103/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice103103

    how you can find whether a particular user existsgrep command on the /etc/passwd file.

    grep sennysenny@ssathya1:~$ grep senny /etc/passwdsenny:x:1001:1001:senny,,,,:/home/senny:/bin/bash

    grep -cCounts number of strings occurance.

    grep vDoes the reverse, return the results in which the search string does not exis

    Using grep Searching Fi

  • 7/30/2019 Unix Basics.pdf

    104/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice104104

    If you want to search using a string of multiple words, enclose the words

    For example, if you want to search for "Mark Black" in /etc/passwd, you command.$ grep "Mark Black" /etc/passwdmblack:ipCg:2388:125:Mark Black,,,:/home/mblack:/usr/bin/sh$

    For a case-insensitive search of "Mark Black," use the following comman$ grep -i "mark black" /etc/passwdmblack:ipCg:2388:125:Mark Black,,,:/home/mblack:/usr/bin/sh

    Using grep Searching Fi

  • 7/30/2019 Unix Basics.pdf

    105/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice105105

    The grep command can be used to search multiple files for a matching strYou need to specify all file names in which you want to search for the tex

    For example, if you search for the word root in the /etc/passwd and /etc/grfollowing result is displayed.$ grep root /etc/passwd /etc/group/etc/passwd:root:8JgNSmFv806dA:0:3:,,,:/home/root:/sbin/sh/etc/group:root::0:root/etc/group:other::1:root,hpdb/etc/group:bin::2:root,bin

    Finding FilesSearching FiThe find command is used to search for a file on a system

  • 7/30/2019 Unix Basics.pdf

    106/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice106106

    The find command is used to search for a file on a system.

    For example, if you want to find all files that start with my in the /etc direct

    subdirectories, the command is:$ find /etc -name "my*"/etc/profile/etc/protocols$In a similar way, the find command can be used to find files that are newer certain file. The search can also be made on file types and file permissions.

    Please refer to man pages for more information on the find command.

  • 7/30/2019 Unix Basics.pdf

    107/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice107

    Combining Progra- Pipes and Filte

    Standard OutputMost running UNIX programs produce output

    Pipe

  • 7/30/2019 Unix Basics.pdf

    108/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice108108

    Most running UNIX programs produce output

    Such output usually ends up on the screen (the user's monito

    This output can be "redirected" to one of two other "places":A fileAnother program

    To redirect output to a file, the ">" symbol is used

    For example:

    lsl > listingA file called listing is created in the current directory that contains the o

    the ls program

    Standard Output (cont.)To redirect output to another program the "|" ("pipe") symbol is use

    Pipe

  • 7/30/2019 Unix Basics.pdf

    109/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice109109

    To redirect output to another program, the | ( pipe ) symbol is useFor example:

    who | wcWhen this command is typed on the command line, the shell does thfollowing:

    Starts the who program

    Starts the wc program

    Connects the two in such a way that the output of the first prog"piped" to the second, where it is used as input

    Standard InputThe previous diagram implies (correctly) that programs can take inpu

    Pipe

  • 7/30/2019 Unix Basics.pdf

    110/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice110110

    Not many programs take input

    By default, a program's input comes from the keyboard

    An appropriate use of the shell can cause a program totake input from one of two other places:

    A file

    Another programInput is read from a file by using the "

  • 7/30/2019 Unix Basics.pdf

    111/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice111111

    The input and output described above are known as Standard InpuStandard Output

    The diagram below summarises this:

    Running UNIXprogram

    Keyboard

    File

    Program

    Standard Input Standard Ou

    Standard Input and Output (conWhy would we want to do this? Why would we want to connect pro

    Pipe

  • 7/30/2019 Unix Basics.pdf

    112/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice112112

    y y ptogether, or read data from files?

    Most of the command-line utilities that come with UNIX adhere to thphilosophy that "complex" tasks may be performed by combining simprograms

    This "roll-your-own" approach has made UNIX very successful, and foundation of shell-scripting

    About FiltersA filter is a UNIX command-line utility that has the following propert

    Pipe

  • 7/30/2019 Unix Basics.pdf

    113/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice113113

    It takes standard input

    It performs some processing on the data it reads

    It produces output based upon that input

    For example, wc is a filter. The processing it performs is counting linecharacters. ls, however, is not a filter (it takes no input)

    Filters are used to process the data produced by other programs and the

    Common FiltersThe following programs are filters that are used regularly in UNIX:

    Fil P i d S d d I

    Pipe

  • 7/30/2019 Unix Basics.pdf

    114/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice114114

    Filter Processing done to Standard Input

    cat None

    more Pagination

    grep Removal of lines that do not contain certain text

    sort Sorting

    wc Counting of lines, words and/or characters

    tee Duplication write to files and screen

    sed Basic editing

    awk Anything

    Searching for Text in FilesIn the previous table, it was said that grep's processing is "removal of lines t

    certain text"

    Pipe

  • 7/30/2019 Unix Basics.pdf

    115/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice115115

    certain textThis can be put more meaningfully in two other ways:

    is used to search for text in files (or Standard Input)is a true filter, in every sense of the word

    grep has the following usage:

    grep pattern filename(s)For example:

    grep Mark *.txtuses its own set of wildcards

    Use grep with find to locate files in all directories

    Standard ErrorEach program actually produces two sets of output:

    Pipe

  • 7/30/2019 Unix Basics.pdf

    116/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice116116

    Running UNIXprogram

    Screen

    File

    Progra

    Keyboard

    File

    Standard Error

    Program

    File

    Screen

    Standard Error (cont.)To redirect Standard Error to a file use "2>

    Pipe

  • 7/30/2019 Unix Basics.pdf

    117/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice117117

    To redirect Standard Error to a file, use 2>(Standard Output can also be redirected using "1>")

    This means that a command's output can be redirected to two separate filneed be, as follows:

    command1 > fileA 2> fileBIt is possible to redirect all output to the same place, as follows:

    command1 > fileA 2>&1Any form of output may be redirected to /dev/null if it is not wanted at al

  • 7/30/2019 Unix Basics.pdf

    118/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice118

    UNIX File System

    Hierarchy

    UNIX files and directories are arranged in a systematic way like a tree strucdirector is represented b the sl sh s mbol (/)

    Unix File System

  • 7/30/2019 Unix Basics.pdf

    119/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice119119

    directory is represented by the slash symbol (/).

    Every directory contains a particular set of files related to some defined pu

    Example : All files in the /etc directory are related to some sort of system c

    Static and Dynamic FilesStatic files and directories contain information that usually does not chang

    installation

  • 7/30/2019 Unix Basics.pdf

    120/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice120120

    installation.

    The files that fall into this category are executable files, libraries, applicat

    This part contains /sbin, /usr, and the application directories under /opt

    They are also called shared files.

    The dynamic files are those which keeps changing regularly.

    Static and Dynamic Files

  • 7/30/2019 Unix Basics.pdf

    121/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice121121

    Like the /var directory.which holds most of the log files. In fact, the name var also comes from "v

    You place temporary files in the /tmp directory.Most of the applications also put their temporary files in the /var/tmp dire

    continuously changes

    /home can also be called as dynamic, as it changes when the user is createuser adds and make some changes.

    To keep the logical division of files and directories, HP-UX keeps all adirectories separate from system directories so that these two types of fil

    Unix File System

  • 7/30/2019 Unix Basics.pdf

    122/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice122122

    directories separate from system directories so that these two types of filmixed with each other.

    executable and no executable files and directories are also kept separateother

    The Root Directory ( / )All of the file system is viewed with reference to the root directory

  • 7/30/2019 Unix Basics.pdf

    123/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice123123

    This is the top-level directory in the tree structure and hence the name roo

    It is represented by a slash character (/).root directory or any other directory is not related to physical disks in any

    The directory structure is just a logical arrangement regardless of the numbyour system or how these disks are partitioned.

    The Device Directory (/dev)The device directory contains all of the device files

    Device files are used to represent devices attached to the system.

  • 7/30/2019 Unix Basics.pdf

    124/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice124124

    character devices provide only a serial stream of input or output;

    block devices are randomly accessible;

    The Home Directory (/home)This directory contains the home directories of all system users.

    All users are owners of their respective directories and have full rights othese directories

    The size of this directory usually depends on the number of users and vas users add and delete files in their home directories.

    The /etc DirectoryAll of the system configuration files are placed in this directory.

  • 7/30/2019 Unix Basics.pdf

    125/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice125125

    The files here control system startup and shutdown procedures as well as the

    shutdown of server and daemon processes.Files in the /etc/rc.config.d directory are configuration and control files for sand shutdown and other server and daemon processes.

    Files in the /etc/opt directory contain configuration files for applications instsystem.

    The System Binary Directory (/sThis directory contains executable files needed at boot time.

  • 7/30/2019 Unix Basics.pdf

    126/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice126126

    Under HP-UX, this directory also contains system scripts required at starttime.

    The most important script in the /sbin directory is the script that controls asystem startup and shutdown processes.

    Another important program is , which is used to control different systemsingle-user mode, multiuser mode, etc.

    There is the /sbin/init.d directory that contains all scripts executed at systshutdown or at the time when the system changes its state

    There are directories with names /sbin/rc0.d, /sbin/rc1.d, and so on. Thesedifferent runlevels or system states in conjunction with the /sbin/init progra

    The Application Directory (/opThe /opt directory is used for installing applications on an HP-UX syste

  • 7/30/2019 Unix Basics.pdf

    127/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice127127

    Each application has a subdirectory in this directory. Starting from that

    subdirectory, each application may have its own directory tree containing files, manual pages, libraries, and so forth.

    The Temporary File Directory (/tmThis directory has permissions for everybody to create or delete files.

  • 7/30/2019 Unix Basics.pdf

    128/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice128128

    Most of the time, temporary files are placed in this directory and are delto time.

    This directory contains user commands, applications, and utilities.

    /usr Directory

  • 7/30/2019 Unix Basics.pdf

    129/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice129129

    Contains contributed software from other sources.

    Header and include files used for programming.

    Libraries for programming and machine-dependent database files. A user look into this directory to check the existence of certain libraries in case thcompilation or run-time errors.

    Many system administration commands are placed here.

    Manual pages for HP-UX commands.

    The Variable Files Directory (/vThis directory contains basically three types of files: log files, spool files, a

    files created by applications.

  • 7/30/2019 Unix Basics.pdf

    130/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice130130

    The files in /var grow and shrink on a continual basis

    Spool Files (/var/spool)

    Spool files related to print services are kept in the /var/spool/lp directory. Selectronic mail go into /var/mail. Other spool files are kept in a directory und

    Temporary var files (/var/tmp)

    Temporary files generated by some applications go into the /var/tmp directo

    The lost+found DirectoryThese files are usually created by the command, which is used to check

    integrity from time to time and at boot time The files that have no valid lin

  • 7/30/2019 Unix Basics.pdf

    131/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice131131

    integrity from time to time and at boot time. The files that have no valid lindirectory. Each file system contains one lost+found directory.

    If you want to find out in which directory an executable file or command use the which command. For example, if you want to see in which directory

    The which Command

  • 7/30/2019 Unix Basics.pdf

    132/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice132132

    located, use the following procedure.$ which ls/usr/bin/ls$

    The which command finds all files that are located in your search path.

    Another command is whereis, which can search and locate source, binamanual pages. The following command shows two files that can be used

    d d th th t th l g f th d

    The whereis Command

  • 7/30/2019 Unix Basics.pdf

    133/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice133133

    command and the path to the manual page for the command.$ whereis catcat: /sbin/cat /usr/bin/cat /usr/share/man/man1.Z/cat.1

    /home user home directories, eg /home/kim

    /etc configuration files

    The Summary

  • 7/30/2019 Unix Basics.pdf

    134/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice134134

    /etc configuration files

    /usr applications and data, eg /usr/bin

    /var data which changes, eg /var/spool/mail

    /dev devices, special files

    /lost+found where files are recovered to by fsck after file system corruption.normally empty

    /tmp temporary files, often cleared during bootup

    /proc system information, eg /proc/cpuinfo

    /bin programs required during bootup

    /sbin programs required during bootup which would only be run the thesuperuser (root)

  • 7/30/2019 Unix Basics.pdf

    135/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice135

    UNIX Jobs

    Job ControlA job is a running process in memory.

    Job control is a shell feature that moves jobs from the foreground to bac

  • 7/30/2019 Unix Basics.pdf

    136/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice136136

    Job control is a shell feature that moves jobs from the foreground to bacsuspends running jobs, and restarts suspended jobs.

    You can also put a job into a wait state, where it waits for another job tothen restarts.

    The shell provides a mechanism to carry out these tasks with the help ofshell commands.

    Foreground and Background JobA user starts a program or command, in UNIX terminology a job is ini

    T t t th j b h t it f th j b t fi i h d t th

  • 7/30/2019 Unix Basics.pdf

    137/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice137137

    To start another job, you have to wait for the job to finish and get the scommand prompt again. Such jobs are called foreground jobs.

    All these jobs are foreground jobs, as the user can interact with the runprogram.

    Note : You cannot execute the next command until the existing one is c

    A background job is one that gives you back the shell command promprunning.

    Background Jobs

  • 7/30/2019 Unix Basics.pdf

    138/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice138138

    Say, you have an program which does complete system scan which run

    complete. Such jobs can be run in background and you can do some othethat is running.

    To start a job in the background you can put the & symbol at the endcommand prompt.

    You may also run programs in the background that need input by rediresome file on the disk.

    If a background job sends its output to stdout, it is displayed on your teunless you have redirected it.

    Once a job is started in the background, the shell prints the job ID and numbers of the command that started the job on the terminal display.

    Use the job ID to bring a job into the foreground at any time

    Foreground and Background Job

  • 7/30/2019 Unix Basics.pdf

    139/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice139139

    $jobs

    This will list you the number of jobs that are currently running and stopA plus symbol (+) in the output of this command shows that this is the job

    a minus sign (-) shows which job is scheduled to be run next after the cujob.

    A -l switch with the jobs command shows the PID of all jobs.

    Suspending a Foreground JobMany times you start a job in the foreground and then you want to do som

    without abandoning the program running in the foreground.

  • 7/30/2019 Unix Basics.pdf

    140/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice140140

    g p g g g

    To suspend a running job, you can use the key sequence represented by tvalue in the sttya output

    To find the value of the susp sequence, use the sttya command. If this vnot set, you can use the stty command to set its value as follows.

    $ stty susp ^z

    The (^) symbol shows the control key. After this command, you can use tsequence to suspend a job.

    Resuming Suspended Jobs and BriBackground Jobs to the Foregrou

    All suspended jobs can be resumed with the foreground (fg) command. T

  • 7/30/2019 Unix Basics.pdf

    141/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice141141

    All suspended jobs can be resumed with the foreground (fg) command. Tcommand is used to bring background jobs to the foreground.

    To bring a job to the foreground, you need to know the job id with the jocommand .

    If you don't specify any job ID, the current job is brought into the foregr

    Job numbers are used with the percent (%) symbol with the fg commandalso use the command name with the fg command, which is sometimes

    Use the stty command to set the value of susp equal to .Start vi editor to edit a file file1.Suspend this vi session using the combination

    Job Control - Lab

  • 7/30/2019 Unix Basics.pdf

    142/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice142142

    Suspend this vi session using the combination.Use the jobs command to list background jobs.Start another vi session with file file2.Also suspend this session.Bring the first vi session to the foreground using the fg command. Close

    and then bring the second vi session to the foreground and close it.

    Moving Jobs to the BackgrounAs soon as you suspend a foreground job, it goes into a stopped state.

    h b k b k d h h b (b k d

  • 7/30/2019 Unix Basics.pdf

    143/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice143143

    You can start the job, keeping it in background with the bg (background

    To send a foreground job into the background, first suspend it and then uIf there is more than one suspended job, you need to provide a job ID to

    bring a particular job into running state from stopped state.

    There is no direct way to stop a running job.

    Stopping a Running Job

  • 7/30/2019 Unix Basics.pdf

    144/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice144144

    We can bring a background job into the foreground and then suspend it.

    You may need to stop a running job temporarily when it is taking a lot ofresources and you want to run a more important job.

    If you want to wait for background jobs to be finished, just use the wait c

    Waiting for Background Jobs to F

  • 7/30/2019 Unix Basics.pdf

    145/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice145145

    This command stops the command prompt until all background jobs are

    If you want to stop the wait command. Use Ctrl + c keys

    We can also wait for a particular job to finish by specifying the job numbargument to the wait command.

    $ wait %2

    Environment

  • 7/30/2019 Unix Basics.pdf

    146/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice146

    andShell Variables

    Environment and Shell VariableWhen a shell executes a command, UNIX creates a process in memory f

    command.

  • 7/30/2019 Unix Basics.pdf

    147/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice147147

    This process is called the child process of the shell. Because the shell crprocess, the shell is called the parent process of the command process.

    All child processes inherit environment variables from their parent (the

    shell variables are set locally by the shell and are not visible to any child

    Each child gets a copy of the environment variables and is allowed to mathese variables.

    Note : The changes are local to the child process and can't reflect back.

    Comparison of Shell and Environment Environment Variables Shell Variables

    Also called global variables Also called local variables

  • 7/30/2019 Unix Basics.pdf

    148/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice148148

    Also called global variables Also called local variablesInherited by all child processes Not inherited by children

    Usually contain system-specific information Usually used to keep temshell programs

    Setting and Displaying Variablesyou can set a variable at the command prompt just by entering a variable

    by an "=" sign and the value you want to assign to the variable.

  • 7/30/2019 Unix Basics.pdf

    149/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice149149

    VAR3=TestVar The command prompt appears without any message

    Note: There is no space character on either side of the "=" symbol. If youaround the = sign, the shell interprets it as a command and displays an erro

    The echo CommandThe echo command is used to view the value of a particular shell variabl

    View the value of our newly created shell variable VAR3, use the follow

  • 7/30/2019 Unix Basics.pdf

    150/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice150150

    TestVar $

    Note : The $ symbol tells the echo command that the argument is a variable, string.

    The result of using the above command without $ is as follows.$ echo VAR3

    VAR3

    $

    Listing All VariablesList all the variables known to your current shell, use the set command.$setANT_HOME=/mnt/llw/antAPP=soar

  • 7/30/2019 Unix Basics.pdf

    151/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice151151

    APP_ENVIRONMENT=ITGCLASSPATH=/mnt/soar/soarlib/jdom.jar:/mnt/ims/java/1.5.0.16.00/jre/lib/rt.jar:/mnt/soar/soarlib/sax.jar:/mnt/soar/soarlib/xerces.jar:/mnt/soar/document

    product/6.5/dctm-server.jar:/mnt/soar/documentum/dctm.jar:/mnt/soar/documentum/config:/mnt/soar/documentum/product/6.5/bin:/mnt/soar/documentum/config:/mnt/soar/documentum/dctm.jar:/mnt/soar/documentum/dfc/dfc.jar:/mnt/ims/java/1.5.0.16.00/lib/classes.zip:/mnt/ims/java/1.5.0.16.00/lib/tools.zip:/mnt/soar/soarl/Tidy.jar:/mnt/soar/soarlib/activation.jar:/mnt/llw/ant/lib/ant.jar:/mnt/soar/soarlib/collections.jar:/mnt/soar/soarlib/commons-fileupload-1.2.1.jar:/mnt/soar/soarlib/jaxp-api.jar:/mnt/soar/soarlib/jsdk23.jar:/mnt/soar/soarlib/log4j.jar:/mnt/soar/soarlib/mail.jar:/mnt/soar/soarlib/struts.jar:/mnt/soar/soarlib/soarwebapp.jar:/mntoar/soarlib/dom4j-1.6.1.jar:/mnt/soar/soarlib/jaxen.jar:/mnt/soar/soarlib/commons-io-1.4.jar:/mnt/soar/soarlib/commons-vfs.jar:/mnt/soar/soarlib/vfs.jar:/mnt/soar/soarlib/commons-logging.jar:/mnt/soar/soarlib/commons-beanutils.jar:.::/opt/bea/wls10/weblogic10/server/lib/weblogic.jar:/opt/bea/wls10/weblogic10/server/lib/api.jar:/opt/bea/wls10/weblogic10/server/lib/webc_sp.jarCOLUMNS=177

    This list will change from system to system on what is set .

    Variable Containing More Than One W Variable has a value that contains space characters in it.

    For Example : If you try to set a variable containing spaces in the normal waget an error message as follows.$ NAME itt

  • 7/30/2019 Unix Basics.pdf

    152/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice152152

    $ NAME=senny ritter

    sh: ritter : not found.The shell thought that you were setting a variable NAMEwith value senny

    a UNIX command. The shell then tried to execute this command and failed.

    Set variables containing multiple words, we use single or double quotes

    $ NAME=Senny Ritter"$ echo $NAMESenny Ritter

    $ NAME=Senny Ritter'$

    The echo command can be used to display a variable and additional text at tFor example, just after displaying the NAME variable, we want to display theuse command echo $NAME7?$ echo $NAME7

    The echo Command

  • 7/30/2019 Unix Basics.pdf

    153/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice153153

    $ echo $NAME7

    sh: NAME7: Parameter not set.$The shell actually started looking for a variable name NAME7 instead of NA

    it. To avoid this ambiguity, we use {} to separate a variable from the rest of th$ echo ${NAME}7Senny Ritter7$

    Many UNIX users put {} around variable names to avoid any ambiguity. Thused any place a shell variable is used with some other text.

    Modifying a VariableAssigning a new value to the same variable name modifies the previous v

    variable.

    It can be done in two ways. If we just assign a new value, the old value of

  • 7/30/2019 Unix Basics.pdf

    154/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice154154

    destroyed.

    We can also append to the old value by putting the variable name on the rof the = symbol at the time of assignment.

    For example, if we want to add a third part to the NAME variable, it can bfollows.$ NAME="$NAME Junior"

    $$ echo $NAMESenny Ritter Junior $

    Difference between single()- and doublecharacters

    Consider the above command example by replacing the double quotes withquotes and watch the result carefully

  • 7/30/2019 Unix Basics.pdf

    155/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice155155

    quotes and watch the result carefully.

    $ NAME='$NAME Junior'$$ echo $NAME$NAME Junior $

    This is not what we wanted! What happens is that single-quote characters dexpand any variable name inside them to its value. Instead, anything inside tquotes is taken as is and assigned to the variable.

    Note: Be careful when using single quotes! The same rule applies when you and double quotes with other commands.

    A shell variable can be removed by the unset command on HP-UX. Please command is not available in all UNIX shells.$ NAME=Senny Ritter"$ h $NAME

    Removing a Variable

  • 7/30/2019 Unix Basics.pdf

    156/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice156156

    $ echo $NAMESenny Ritter $ unset NAME$ echo $NAMEsh: NAME: Parameter not set.$

    Assigning Output of a Command to a The back quote character is displayed when you press the "~" key without

    It is used to assign the result of a command to a variable. If you want to assiname to a variable NAME, you can use the following command.

  • 7/30/2019 Unix Basics.pdf

    157/185

    2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice1571