+ All Categories
Home > Documents > MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji...

MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji...

Date post: 06-Apr-2018
Category:
Upload: dr-panduranga-rao-mv
View: 220 times
Download: 0 times
Share this document with a friend

of 203

Transcript
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    1/203

    INFORMATION TECHNOLOGY PROGRAMMES

    Bachelor of Science in Information Technology - B.Sc. (IT)Master of Science in Information Technology - M.Sc. (IT)

    In

    collaboration

    with

    KUVEMPU UNIVERSITY

    M.Sc.(IT) - 3rd Semester

    MSIT - 3ALinux Internals

    Directorate of Distrance Education

    Kuvempu UniversityShankaraghatta, Shimoga District, Karnataka

    Universal Education TrustBangalore

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    2/203

    Titles in this Volume :

    MSIT - 3A Linux Internals

    Prepared by UNIVERSAL EDUCATION TRUST (UET)

    Bangalore

    First Edition : May 2005

    Copyright byUNIVERSAL EDUCATION TRUST, Bangalore

    All rights reserved

    No Part of thi s Book may be reproduced

    in any form or by any means without the written

    permission from Universal Education Trust, Bangalore.

    All Product names and company names mentioned

    herein are the property of their respective owners.

    NOT FOR SALE

    For personal use of Kuvempu University

    IT - Programme Students only.

    Corrections & Suggestions

    for Improvement of Study material

    are invited by Universal Education Trust, Bangalore.

    E-mail : [email protected]

    Printed at :

    Mayur Print Ads

    Bangalore - 20

    Ph : 94480 - 42724

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    mailto:[email protected]://www.pdffactory.com/http://www.pdffactory.com/mailto:[email protected]
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    3/203

    LINUX INTERNALS

    (MSIT - 3A)

    : Contributing Authors :

    Sri. M.V. Panduranga RaoResearch Scholar,

    National Inst itute of Technology, Karnataka

    Formerly KREC, Suratkal, Mangalore.

    Lecturer, Dept. of Information Science & Engg.

    JNNCE, Shimoga - 577204.

    &

    Smt. K. RoopaLecturer, Dept. of C.Sc. & Engg.

    JNNCE, Shimoga - 577204

    Under the guidance of

    Smt. K. Roopa Dr. K. Chandrashekar ShetProfessor

    Department of Computer Engineering

    National Inst itute of Technology, Karnataka

    Surathkal, Mangalore, INDIA

    P.O. Srinivasnagar - 575025

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    4/203

    MSIT 3A Linux Internals V

    Contents

    Chapter 1

    INTRODUCTION 1

    1.0 Introduction and Objectives........................................................ 1

    1.1 Unix and Linux: Features ......................................................... 2

    1.2 Unix System Architecture ........................................................ 3

    1.3 Unix Philosophy ...................................................................... 4

    1.4 What is Linux? ........................................................................ 4

    1.5 Using a Linux System: Login .................................................... 5

    1.6 Linux Command Line .............................................................. 5

    1.7 Logging Out ........................................................................... 6

    1.8 Command Syntax .................................................................... 6

    1.9 Files ...................................................................................... 6

    1.10 Linux Directories...................................................................... 7

    1.11 File System Organization............................................................ 7

    1.12 Creating Files with cat............................................................... 10

    1.13 Displaying Files Contents with cat.............................................. 10

    1.14 Deleting Files with rm .............................................................. 11

    1.15 Unix Command Feedback .......................................................... 11

    1.16 Copying and Renaming Files with cp and mv .............................. 11

    1.17 Filename Completion ................................................................. 12

    1.18 Command History .................................................................... 12

    1.19 Summary.................................................................................. 12

    1.20 Check Your Progress................................................................. 13

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    5/203

    VI Chapter 1 - Introduction

    Chapter 2

    LINUX COMMANDS 18

    2.0 Introduction and objectives ........................................................ 18

    2.1 Basic Linux Commands............................................................. 19

    2.2 Help commands........................................................................ 21

    2.3 General commands.................................................................... 22

    2.4 File management commands...................................................... 24

    2.5 Finding files.............................................................................. 27

    2.6 Linux Files and File Permission................................................... 27

    2.7 File names and permission characters.......................................... 282.8 Examples on files...................................................................... 28

    2.9 Setting Keystrokes.................................................................... 30

    2.10 Keys and terminal configuration ................................................. 30

    2.11 Summary.................................................................................. 31

    2.12 Check Your Progress................................................................. 31

    Chapter 3

    PROCESSES 37

    3.0 Introduction and objectives......................................................... 373.1 Looking at Processes................................................................. 38

    3.2 Process IDs............................................................................. 38

    3.3 Viewing Active Processes.......................................................... 39

    3.4 ps Output Formats..................................................................... 39

    3.5 Killing a Process....................................................................... 41

    3.6 Creating Processes................................................................... 42

    3.7 Creating Using system()............................................................ 42

    3.8 Creating Using system calls fork, exec..................................... 43

    3.9 Creating by Calling fork............................................................. 43

    3.10 Creating by Using the exec Family.............................................. 443.11 Creating by Using forkand exec Together................................... 45

    3.12 Process Scheduling.................................................................... 45

    3.13 Signals..................................................................................... 46

    3.14 Some Linux daemons................................................................. 47

    3.15 popular daemons....................................................................... 48

    3.16 Zombie Proces.......................................................................... 50

    3.17 Threads.................................................................................... 51

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    6/203

    MSIT 3A Linux Internals VII

    3.18 Processes Vs. Threads.............................................................. 52

    3.19 Summary.................................................................................. 53

    3.20 Check Your Progress................................................................. 54

    Chapter 4

    SHELL PROGRAMMING 60

    4.0 Introduction and Objectives........................................................ 60

    4.1 Creating a shell script................................................................ 61

    4.2 Meta characters........................................................................ 62

    4.3 Positional Parameters and Shell Variables.................................... 634.4 Built-in shell commands and variables.......................................... 64

    4.5 Iteration, control and if statements.............................................. 66

    4.6 Shell operators ........................................................................ 68

    4.7 The test Command.................................................................... 69

    4.8 An example script..................................................................... 71

    4.9 Environment Variables .............................................................. 72

    4.10 Summary.................................................................................. 73

    4.11 Check Your Progress................................................................. 74

    Chapter 5

    FILE SYSTEM 79

    5.0 Introduction and Objectives ....................................................... 79

    5.1 Filesystem Overview ................................................................ 80

    5.2 Types of Files........................................................................... 80

    5.3 Directory pointer....................................................................... 83

    5.4 Directory Hierarchy ................................................................. 83

    5.5 Pathnames .............................................................................. 84

    5.6 Current Directory ..................................................................... 85

    5.7 Dot (.) and DotDot(..) .............................................................. 855.8 Moving and Copying Files ......................................................... 86

    5.9 Removing Files ........................................................................ 86

    5.10 Operations on Directories ......................................................... 87

    5.11 Inodes .................................................................................... 87

    5.12 Options of Inodes ................................................................... 88

    5.13 Links ...................................................................................... 88

    5.14 Hard links ............................................................................... 89

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    7/203

    5.15 Soft links ................................................................................. 89

    5.16 Access Control and UID .......................................................... 90

    5.17 Categories of Access Control .................................................... 91

    5.18 Access Control Example ........................................................ 91

    5.19 Changing Access Previleges: hmod............................................ 91

    5.20 chmod symbolically .................................................................. 92

    5.21 chmod numerically ................................................................... 92

    5.22 umask Masking Privileges ......................................................... 92

    5.23 Filesystem Structure ................................................................. 93

    5.24 /etc/fstab - Example ................................................................ 93

    5.25 Special Files - /dev ................................................................ 935.26 Special Files - /proc ............................................................... 94

    5.27 Mounting Additional Volumes .................................................... 94

    5.28 Mounting shared filesystems ..................................................... 95

    5.29 Summary ................................................................................ 95

    5.30 Check Your Progress................................................................ 96

    Chapter 6

    FILE SYSTEM ADMINISTRATION 102

    6.0 Introduction and objectives ........................................................ 1026.1 Inode in depth ......................................................................... 103

    6.2 Inode table entry ...................................................................... 103

    6.3 Links: relationship with inode ..................................................... 104

    6.4 Hard links ............................................................................... 105

    6.5 Soft links ................................................................................. 105

    6.6 Non-native filesystems ............................................................ 106

    6.7 Disk checking and recovery (fsck) ............................................. 106

    6.8 Check a Linux ext2 filesystem (e2fsck)....................................... 107

    6.9 Disk free space (df).................................................................. 107

    6.10 Disk usage (du)......................................................................... 108

    6.11 Disk partitioning concepts .......................................................... 108

    6.12 A Partitioned Disk ................................................................... 109

    6.13 Making and changing partitions .................................................. 109

    6.14 How many partitions? ............................................................... 110

    6.15 What size partitions? ................................................................. 110

    6.16 BIOS problems with LILO and partitions .................................... 110

    6.17 Disk partitioning tools ................................................................ 111

    VIII Chapter 1 - Introduction

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    8/203

    6.18 Using fdisk()............................................................................. 111

    6.19 The fdisk interface ................................................................... 112

    6.20 Interactive commands in fdisk..................................................... 112

    6.21 Making Linux filesystems (mke2fs) ............................................ 113

    6.22 Block devices ........................................................................... 113

    6.23 Character devices ..................................................................... 114

    6.24 Summary ................................................................................. 114

    6.25 Check Your Progress................................................................. 115

    Chapter 7

    SYSTEM ADMINISTRATION 123

    7.0 Introduction and objectives......................................................... 123

    7.1 The superuser account............................................................... 124

    7.2 User accounts /etc/passwd......................................................... 124

    7.3 Groups..................................................................................... 125

    7.4 Managing Linux users................................................................ 127

    7.5 User reference files................................................................... 128

    7.6 User modifications.................................................................... 128

    7.7 Examples of making changes to permissions................................ 129

    7.8 Some administration commands.................................................. 1307.9 Summary.................................................................................. 132

    7.10 Check Your Progress................................................................. 132

    Chapter 8

    EDITORS AND TOOLS 135

    8.0 Introduction and objectives......................................................... 135

    8.1 Linux VI editor......................................................................... 136

    8.2 The emacs editor...................................................................... 138

    8.3 System and network configuration............................................... 1408.4 X Configuration........................................................................ 140

    8.5 Library and kernel dependency management................................ 141

    8.6 General Diagnostic.................................................................... 141

    8.7 To remotely use Xwindows from Ms Windows............................. 142

    8.8 Network application commands.................................................. 143

    8.9 Summary.................................................................................. 144

    8.10 Check Your Progress................................................................. 145

    MSIT 3A Linux Internals IX

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    9/203

    Chapter 9

    ISTALLATION OF LINUX 148

    9.0 Introduction and objectives......................................................... 148

    9.1 Devices of installation................................................................ 149

    9.2 Partitioning hard disk................................................................. 150

    9.3 Partition sizes........................................................................... 150

    9.4 Mount points............................................................................. 151

    9.5 Mounting partitions at startup..................................................... 151

    9.6 The fstab file............................................................................ 152

    9.7 Know your hardware................................................................. 1539.8 Creating the Linux boot disk....................................................... 154

    9.9 Making a diskette under MS-DOS............................................... 154

    9.10 Making a diskette under a Linux-Like OS..................................... 155

    9.11 Beginning the installation of Linux .............................................. 155

    9.12 Installation options - class and method ........................................ 156

    9.13 Manual configurations................................................................ 156

    9.14 Package installations.................................................................. 158

    9.15 Standard linux partition filesystem types....................................... 159

    9.16 Network administration tools....................................................... 162

    9.17 Summary.................................................................................. 1649.18 Check Your Progress................................................................. 164

    Chapter 10

    GLOSSARY 169

    X Chapter 1 - Introduction

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    10/203

    Chapter 1

    Introduction

    1.0 INTRODUCTION AND OBJECTIVES

    Linux is an operating system that was first created at the University of Helsinki in Finland by a

    young student named Linus Torvalds. At this time the student was working on a UNIX system

    that was running on an expensive platform. Because of his low budget, and his need to work at

    home, he decided to create a copy of the UNIX system in order to run it on a less expensive platform,

    such as an IBM PC. He began his work in 1991 when he released version 0.02 and worked steadily until1994 when version 1.0 of the Linux Kernel was released.

    There are no royalty or licensing fees for using Linux, and the source code can be modified to fit your

    needs. Because it comes with source code to the kernel, it is quite portable. Linux runs on more CPUs

    and platforms than any other computer operating system.

    Linux is a true multi-tasking operating system similar to its brother, UNIX. It uses sophisticated, state-

    of-the-art memory management to control all system processes. That means that if a program crashes

    you can kill it and continue working with confidence. Another benefit is that Linux is practically immunized

    against all kinds of viruses that we find in other operating systems.

    Objectives

    At the end of this unit, You would be able to

    know important features of Linux

    Unix system Architecture

    Significance of using a Linux System

    Chapter 1 - Introduction 11

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    11/203

    Chapter 1 - Introduction12

    Linux command Line

    Exhibit login, logoff, Issue basic commands & switching to another account

    Linux directories and file system organization

    Know commands related to file retrievals

    1.1 Unix and Linux: features

    Linux is based on Unix.

    Unix philosophy.

    Unix commands.

    Unix standards and conventions.

    There is some variation between Unix operating systems.

    Especially regarding system administration.

    Often Linux-specific things in these areas.

    Important features of Linux

    Full multitasking and 32-bit support. Linux, like all other versions of UNIX, is a real multitaskingsystem, allowing multiple users to run many programs on the same system at once. Linux is

    also a full 32-bit operating system, utilizing the special protected-mode features of Intel

    80386 and later processors and their work-alikes. Linux inherited all the features of UNIX

    operating system, henceforth we have used the name Linux or UNIX for learning the concepts.

    The X Window System. The X Window System is the de facto industry-standard graphics

    system for UNIX machines. A complete version of the X Window system, known as XFree86,

    is available for Linux. The X Window System is a very powerful graphics interface, supporting

    many applications.

    TCP/IP (Transmission Control Protocol/Internet Protocol) support. This is the set of protocols

    that links millions of university and business computers into a worldwide network known asthe Internet. With an Ethernet connection, you can have access to the Internet or to a local

    area network from your Linux system. Using SLIP (Serial Line Internet Protocol) or PPP

    (Point to Point Protocol), you can access the Internet over phone lines with a modem.

    Virtual memory and shared libraries. Linux can use a portion of your hard drive as virtual

    memory, expanding your total amount of available RAM. Linux also implements shared

    libraries, allowing programs that use standard subroutines to find the code for these subroutines

    in the libraries at runtime. This saves a large amount of space on your system; each application

    doesnt store its own copy of these common routines.

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    12/203

    MSIT 3A Linux Internals 13

    The Linux kernel uses no code from AT&T or any other proprietary source. Much of the

    software available for Linux is free. In fact, a large number of utilities in Linux are developed

    by the GNU project at the Free Software Foundation in Cambridge, Massachusetts. However,

    Linux enthusiasts, hackers, programmers, and recently even commercial companies from all

    over the world have contributed to the growing pool of Linux software.

    Linux supports (almost) all of the features of commercial versions of UNIX. In fact, some of

    the features found in Linux may not be available on other proprietary UNIX systems.

    GNU software support. Linux supports a wide range of free software written by the GNU

    Project, including utilities such as the GNU C and C++ compiler, gawk, groff, and so on.

    Many of the essential system utilities used by Linux are GNU software.

    Linux is compatible with the IEEE POSIX.1 standard. Linux has been developed with software

    portability in mind, thus supporting many important features of other UNIX standards.

    Virtual memory support. Linux utilizes all of your systems memory, without memory limits

    or segmentation through the use of a virtual memory manager.

    Built-in support for networking, multitasking, and other features. Youll see this touted as

    New Technology in systems such as Windows NT. In fact, UNIX (and now, Linux) has

    implemented this new technology for more than 15 years.

    Linux is cheaper to get than most commercially available UNIX systems and UNIX clones.

    If you have the patience and access to the Internet, the only price you pay for Linux is yourtime. Linux is freely available on the Internet.

    1.2 UNIX SYSTEM ARCHITECTURE

    The shell and the window environment are programs.

    Programs only access to hardware is via the kernel.

    Kernel understands only system calls, but not user commands.

    Shell converts user commands to system calls and pass on to the kernel.

    Kernel is the core part of Linux, hence it is also called the operating system.

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    13/203

    Chapter 1 - Introduction14

    1.3 UNIX PHILOSOPHY

    Fig. 1.1 Unix Architecture

    Multi-user.

    A user needs an account to use a computer.

    Each user must log in.

    Complete separation of different users files and configuration settings.

    Small components.

    Each component should perform a single task.

    Multiple components can be combined and chained together for more complex tasks.

    An individual component can be substituted for another, without affecting other

    components.

    1.4 WHAT IS LINUX?

    Linux kernel.

    Developed by Linus Torvalds. Strictly speaking, Linux is just the kernel.

    Associated utilities.

    Standard tools found on (nearly) all Linux systems.

    Many important parts come from the GNUproject.

    Free Software Foundations project to make a free Unix.

    Some claim the OS as a whole should be GNU/Linux.

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    14/203

    MSIT 3A Linux Internals 15

    Linux distributions.

    Kernel plus utilities plus other tools, packaged up for end users.

    Generally with installation program.

    Distributors include: Red Hat, Debian, SuSE, Mandrake.

    1.5 USING A LINUX SYSTEM: LOGIN

    Login prompt displayed.

    When Linux first loads after booting the computer. After another user has logged out.

    Need to enter a username and password.

    The login prompt may be graphical or simple text.

    If text, logging in will present a shell.

    If graphical, logging in will present a desktop.

    Some combination of mouse key and keystrokes will make a terminal window appear.

    A shell runs in the terminal window.

    1.6 LINUX COMMAND LINE

    The shell is where commands are invoked.

    A command is typed at a shell prompt.

    Prompt usually ends in a dollar sign ($)[bash Bourne again shell orsh Bourne shell].

    After typing a command press Enter to invoke it.

    The shell will try to obey the command.

    Another prompt will appear.

    Example:

    $ date

    Thu Jun 14 12:28:05 BST 2001

    $

    The dollar represents the prompt in this course do not type it.

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    15/203

    1.7 LOGGING OUT To exit from the shell, use the exit command.

    Pressing Ctrl+D at the shell prompt will also quit the shell.

    Quitting all programs should log you out.

    If in a text-only single-shell environment, exiting the shell should be sufficient.

    In a window environment, the window manager should have a log out command for this

    purpose.

    After logging out, a new login prompt should be displayed.

    1.8 COMMAND SYNTAX

    Most commands take parameters.

    Some commands require them.

    Parameters are also known as arguments.

    For example, echo simply displays its arguments:$ echo

    $ echo Hello there

    Hello there

    Commands are case-sensitive.

    Usually lower-case.

    $ echo whisper

    whisper

    $ ECHO SHOUT

    bash: ECHO: command not found.

    1.9 FILES

    Data can be stored in a file.

    Each file has a filename.

    A label referring to a particular file.

    Chapter 1 - Introduction16

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    16/203

    Permitted characters include letters, digits, hyphens (-), underscores (_), and dots (.)

    Case-sensitive NewsCrew.mov is a different file fromNewScrew.mov.

    The functions usually performed on a file are as follows:

    v Opening a file for processing.

    v Reading data from a file for processing.

    v Writing data to a file after processing.

    v Closing a file after all the necessary processing has been done.

    1.10 LINUX DIRECTORIES

    As you probably already know from working in graphics mode, in Linux the directories use

    the slash (/) as a separator (Windows uses backslash (\)). In other words it works just like

    websites or ftp servers.

    Any directory which starts with a slash, such as /usr/bin, means it is an absolute name

    - the name specifies the entire sequence of directories from the root directory (/) up to the

    specific directory being requested (bin). Thus, it doesnt matter which directory is the

    current directory when you specify that name, it will always point to the /usr/bin directory.

    On the other hand a directory which does not start with a slash is relative to the current

    directory. For example the directory bin will point to different directories depending on

    whether you are in the root directory (in which case it will point to /bin). It may reside in

    the /usr directory (in which case it will point to /usr/bin) or in the /usr/local directory

    (in which case it will point to /usr/local/bin).

    The same applies to files - if you specifyfile.txt it is assumed to be in the current directory,

    while if you specify /tmp/file.txt it will always point to file.txt in the temporary directory.

    Two special directory names are the current directory, represented by a single period (.) and

    the parent directory, represented by a double period (..). Thus, if you are in the /home/

    sandbox directory and type in ls .. , it will list the contents of the parent directory, which is/home.

    1.11 FILE SYSTEM ORGANIZATION

    In this section, you will learn about the ways LINUX provides for organizing files so that you

    can easily locate and use them. LINUX has provided the directory as a way of organizing

    files. The directory is a special file under which you can have files or more directories (also

    MSIT 3A Linux Internals 17

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    17/203

    referred to as subdirectories). You can visualize the LINUX file structure as a bottom-up

    tree with the root at the top. Thus, the top-level directory is called the root directory and is

    denoted by a single / (forward slash). All the directories and files belong to the root directory.

    Below is a list of some common directories that are found in Linux and Unix systems, and

    what they are used for.

    /

    v This is the root directory, inside which all other directories reside

    v This is similar to the root directory of a drive in Windows (C:\), except that in Linux even

    different hard disks reside within this root.

    /bin

    v This stands for binary, and contains program (executable) files. This (and otherbin

    directories) is where commands such as ls can be found.

    v In Windows, the c:\windows\commands holds some of command-line programs, but others

    are scattered in various other directories.

    /dev

    v This stands for devices. It contains a number of special pseudo-files that are used to

    access the physical hardware that make up, or are connected to, your computer. Forexample the parallel-port would be a file called lp0 in this directory, while the hard disk

    would be hda, and its first partition would be hda0.

    v Windows / DOS uses a similar method, however in Windows these are not in any particular

    directory. Devices have names like LPT1, COM1 or CON - any time you try to access

    a file with that name from any directory, you will get the parallel printer, serial port or

    console, respectively.

    /etc

    v This is where (almost) all system-wide configuration information is stored. Almost all

    configuration information is stored in text files, so you can go into this directory and havea look around with a text viewer if you like. Some of the files are quite cryptic though.

    v There is no equivalent in Windows, where configuration data can be stored anywhere,

    including the registry, INI files and other data files in various directories.

    /home

    v This is where users home directories are usually found. Thus, if you created a user

    called sandbox, there will be a directory with the same name in this directory, which will

    be that users home directory.

    Chapter 1 - Introduction18

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    18/203

    v The nearest equivalent in Windows is c:\windows\profiles, where some user-specific

    data is held, together with c:\My Documents, where user-created documents go. However

    other data can be written in many other directories.

    /lib

    v This is where the library files are found. Libraries are files containing reusable functions

    and routines for programs to use.

    v There is no equivalent in Windows / DOS.

    /mnt

    v This is where storage devices other than the hard disks are mounted. This directoryusually contains subdirectories called cdrom, floppy, etc., which - when these devices

    are mounted - show the contents of the CD-ROM or floppy disk respectively. Your

    Windows drives may also be automatically mounted in this directory.

    v There is no equivalent in Windows / DOS.

    /opt

    v This is where optional components of the system are installed. Products such as KDE,

    Gnome and Oracle may be installed into this directory.

    v The nearest thing in Windows is the c:\Program Files directory.

    /tmp

    v This is a temporary directory. All files placed in here will automatically be deleted

    eventually.

    v The equivalent in Windows / DOS is c:\windows\temp.

    /usr

    v Contains a copy of most of the directories in the root. For example, there is a bin

    directory containing programs, a lib directory containing libraries, etc. Usually, core

    Linux files are contained in the root directories, while non-core files are in the /usrsubdirectories.

    v There is no equivalent in Windows / DOS.

    /var

    v Stands for various. Among the files stored here are the system log files, spool files

    and other data files.

    v There is no equivalent in Windows / DOS.

    MSIT 3A Linux Internals 19

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    19/203

  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    20/203

    1.14 DELETING FILES WITH RM To delete a file, use the rm (remove) command.

    Simply pass the name of the file to be deleted as an argument:

    $ rm shopping_list

    The file and its contents are removed.

    There is no recycle bin.

    There is no unrm command.

    The ls command can be used to confirm the deletion.

    1.15 UNIX COMMAND FEEDBACK

    Typically, successful commands do not give any output.

    Messages are displayed in the case of errors.

    The rm command is typical.

    If it manages to delete the specified file, it does so silently.

    There is no File shopping_list has been removed message.

    But if the command fails for whatever reason, a message is displayed.

    The silence can be be off-putting for beginners.

    It is standard behaviour, and doesnt take long to get used to.

    1.16 COPYING AND RENAMING FILES WITH CP AND MV

    To copy the contents of a file into another file, use the cp command:

    $ cp CV.pdf old-CV.pdf

    To rename a file use the mv (move) command:

    $ mv commitee_minutes.txt committee_minutes.txt

    Similar to using cp then rm.

    For both commands, the existing name is specified as the first argument and the new name

    as the second.

    If a file with the new name already exists, it is overwritten.

    MSIT 3A Linux Internals 21

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    21/203

  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    22/203

    The operating system controls all the resources of the computer system and that control is given to the

    user. The user runs his applications to achieve the goal.

    1.20 CHECK YOUR PROGRESS

    I. Exercises

    1. a. Log in.

    b. Log out.

    c. Log in again. Open a terminal window, to start a shell.

    d. Exit from the shell; the terminal window will close.

    e. Start another shell. Enter each of the following commands in turn.

    date

    whoami

    hostname

    uname

    uptime

    2. a. Use the ls command to see if you have any files.

    b. Create a new file using the cat command as follows:

    $ cat > hello.txt

    Hello world!

    This is a text file.

    Press Enter at the end of the last line, then Ctrl+D to denote the end of the file.

    c. Use ls again to verify that the new file exists.

    d. Display the contents of the file.

    e. Display the file again, but use the cursor keys to execute the same command again

    without having to retype it.

    3. a. Create a second file. Call itsecret-of-the-universe, and put in whatever content you

    deem appropriate.

    b. Check its creation with ls.

    MSIT 3A Linux Internals 23

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    23/203

    c. Display the contents of this file. Minimise the typing needed to do this:

    Scroll back through the command history to the command you used to create the file.

    Change that command to displaysecret-of-the-universe instead of creating it.

    4. After each of the following steps, use ls and cat to verify what has happened.

    a. Copysecret-of-the-universe to a new file called answer.txt. Use Tab to avoid typing the

    existing files name in full.

    b. Now copy hello.txtto answer.txt. Whats happened now?

    c. Delete the original file, hello.txt.d. Rename answer.txtto message.

    e. Try asking rm to delete a file called missing. What happens?

    f. Try copyingsecret-of-the-universe again, but dont specify a filename to which to

    copy. What happens now?

    II. Choose the correct answer:

    1. Linux is invented by_________.

    a) Linus Torvalds b) Dennis Ritchie c) James Gosling d)None of these.

    2. Operating system is a collection of _______________.

    a) S/w programs b) system programs c) Application programs d) all of these.

    3. Linux components include the __________.

    a) Kernel b) shell c) file system & user programs d) all of these.

    4. NFS means _________________________.

    a) Network File System

    b) Network FAT System

    c) NetBios System

    d) None of the above

    5. Device Drivers control the interaction between the _________________.

    a) I/p device & operating system

    b) Operating system & o/p device

    Chapter 1 - Introduction24

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    24/203

    c) Operating system & hardware device

    d) All of the above

    6. The standard way of mounting file systems onto the _________ directory.

    a) dev b) mnt c) bin d) all of these

    7. Linux supports ___________ file systems.

    a) Single b) NFS c) multiple d) all of these

    8. Linux support __________.

    a) multitasking only

    b) 32-bit

    c) Networking

    d) All of the above.

    9. In Linux the directories uses the ____ as a separator.

    a) / b) \ c) // d) all of these

    10. shell is a ___________.

    a) command interpreter

    b) operating system

    c) system call

    d) All of the above

    III. Say True or False

    1. Linux is a 16-bit operating system. True/False

    2. Linux is a multi-processor operating system. True/False

    3. Linux supports many different file systems. True/False

    4. Block size for floppy device is 1024 bytes. True/False

    5. KERNEL accepts instructions from the shell and carries them out True/False

    MSIT 3A Linux Internals 25

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    25/203

    IV. Essay Type Questions1) What is Linux? Write brief features regarding Linux.

    2) Explain the architecture of Unix.

    3) Explain the concept of filesystem.

    4) Elucidate the functions usually performed on a file.

    5) Explain how the directories are manipulated in Linux.

    6) Describe how the file systems are arranged? Give the types.

    7) Exhibit the commands for login, logoff & switching to another account.

    8) Enumerate the commands related to file retrivals.[ Ex: cat, rm, cp, mv ]

    V. Further readings and other activities

    1. The student shall get more information using man or info or any other help command for

    pwd, mkdir, cd, rmdir, sh, csh, ksh

    Ex: man pwd OR info ls OR help cd OR apropos cp

    2. Get new commands from the list given at the end of any man command information at the

    placesee also.

    3. Using man intro, get the information about different kinds of commands present in your kindof linux system.

    4. For further readings you can refer the following books

    Title:Linux internals

    Auihors: M.V.Panduranga Rao and K.Roopa, JNNCE, Shimoga

    http://www.pandurangarao.i8.com/doc/linux/linuxbook.html

    http://www.raomvp.bravepages.com/doc/linux/linuxbook.html

    A complete reference for you to get more information.

    You can also extract solutions for exercises.

    Title: Linux in a Nutshell

    Author: Jessica Perry Hekman and the staff of OReilly & Associates; ISBN 1-56592-

    167-4; 1997. A complete reference for Linux.

    Title: Harley Hahns Student Guide to UNIX, second edition; McGraw- Hill; ISBN 0-

    07-025492-3; 1996. Not just for students, this is a complete and very readable guide to

    LINUX and networking.

    Chapter 1 - Introduction26

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pandurangarao.i8.com/doc/linux/linuxbook.htmlhttp://www.raomvp.bravepages.com/doc/linux/linuxbook.htmlhttp://www.pdffactory.com/http://www.pdffactory.com/http://www.raomvp.bravepages.com/doc/linux/linuxbook.htmlhttp://www.pandurangarao.i8.com/doc/linux/linuxbook.html
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    26/203

    VI. Solutions

    II. Choose the correct answer:

    II. 1. a 2. b 3. d 4. a 5. d 6. b 7. d 8. d 9. a 10. a

    III. Say True or False

    III 1. false 2. true 3. true 4. false 5. true

    MSIT 3A Linux Internals 27

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    27/203

    Chapter 2 - Linux Commands28

    Chapter 2

    Linux Commands

    2.0 INTRODUCTION AND OBJECTIVES

    Linux is a complete multitasking, multi-user operating system that behaves like the UNIX operating

    system in terms of kernel behavior and peripheral support. Linux has all the features of UNIX,

    plus several recent extensions that add new versatility to Linux. All source code for Linux and its

    utilities is freely available.The Linux kernel was originally developed for the Intel 80386 CPUs protected mode. The 80386 was

    designed with multitasking in mind (despite the fact that most of the Intel CPUs are used with single-

    tasking DOS), and Linux makes good use of the advanced features built into the CPUs instruction set.

    Memory management is especially strong with the 80386 (compared to earlier CPUs). A floating-point

    emulation routine allows Linux to function on machines that do not have math coprocessors (such as the

    SX series of Intel CPUs).

    Linux allows shared executables so that if more than one copy of a particular application is loaded

    (either by one user running several identical tasks, or several users running the same task), all the tasks

    can share the same memory. This process, called copy-on-write pages, makes for much more efficient

    use of RAM.

    The Linux kernel also supports demand paging, which means that only sections of a program that are

    necessary are read into RAM. To further optimize memory usage, Linux uses a unified memory pool.

    This pool enables all free memory on the system to be used as disk cache, effectively speeding up access

    to frequently used programs and data. As memory usage increases, the amount of cache is automatically

    adjusted.

    To support large memory requirements when only small amounts of physical RAM are available,

    Linux supports swap space. Swap space enables pages of memory to be written to a reserved area of a

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    28/203

    MSIT 3A Linux Internals 29

    disk and treated as an extension of physical memory. By moving pages back and forth between the swap

    space and RAM, Linux can effectively behave as if it had more physical RAM than it does, albeit at the

    cost of some speed due to the hard drives slower access.

    Linux uses dynamically shared libraries extensively. Dynamically shared libraries use a common library

    section for many different applications, effectively reducing the size of each application. Linux does allow

    full library linking (called statically linked libraries) for portability to machines that may not have the

    dynamic libraries.

    To make Linux widely acceptable, it supports a number of different filesystems, including those

    compatible with DOS and OS/2. Linuxs own primary filesystem, called ext2fs, is designed for optimal

    use of the disk.

    Linux is ideally suited for application development and experimentation with new languages. Several

    different compilers, including C, C++, Fortran, Pascal, Modula-2, LISP, Ada, Basic, and Smalltalk, come

    with the distribution software. Many of the Linux compilers, tools, debuggers, and editors are from the

    Free Software Foundations GNU project.

    After completing this module you should be able to understand and utilize these features of the Linux

    commands:

    Basic Linux commands

    Help commands

    General commands

    File management commands

    Finding files

    Linux files and file Permission

    File names and permission characters

    Example to extract information from files

    Setting keystrokes

    Keys and terminal configuration

    2.1 BASIC LINUX COMMANDS

    A UNIX command is a series of characters that we type.

    To invoke a command, simply type the command name, followed by arguments (if any), to

    indicate to the shell that we are done typing and are ready for the command to be executed,

    press Enter.

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    29/203

    Chapter 2 - Linux Commands30

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    30/203

    MSIT 3A Linux Internals 31

    2.2 HELP COMMANDS

    any_commandhelp | more

    Display a brief help on a command (works with most commands). For example, try

    cp help |more

    help works similar to DOS /h switch. The more pipe is needed when the output is

    longer than one screen.

    mantopic

    Display the contents of the system manual pages (help) on the topic. Press q to quit the

    viewer. Try man man if we need any advanced options. The command info topic works

    similar to man topic, yet it may contain more up-to-date information. Manual pages can be

    hard to readthey were written for UNIX programmers. Try any_commandhelp for a

    brief, easier to digest help on a command. Some programs also come with README or

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    31/203

    other info fileshave a look to the directory /usr/share/doc. To display manual page from a

    specific section, We may use something like: man 3 exit (this displays an info on the command

    exit from section 3 of the manual pages) or man -a exit (this displays man pages for exit from

    all sections). The man sections are: Section 1-User Commands, Section 2-System Calls,

    Section 3-Subroutines, Section 4-Devices, Section 5-File Formats, Section 6-Games, Section

    7-Miscellaneous, Section 8-System Administration, Section 9, Section n-New.

    infotopic

    Display the contents of the info on a particular command. info is a replacement for man

    pages so it contains the most recent updates to the system documentation. Use and

    to move around or we may get confused. Press q to quit. A replacement for

    the somewhat confusing info browsing system might be pinfo - try if we like it any better.

    apropos topic

    Give me the list of the commands that have something to do with my topic. If does not work

    run $ updatedb as root.

    whatis topic

    Give me a short list of commands matching my topic. whatis is similar to apropos (see the

    command above)they both use the same database. But whatis searches keywords, while

    apropos also searches the descriptions of the keywords.

    help command

    Display brief info on a bash (shell) built-in command. Using help with no commandprints the

    list of all bash built-in commands. The shortest list of bash built-in commands would probably

    include: alias, bg, cd, echo, exit, export, fg, help, history, jobs, kill, logout, pwd, set, source,

    ulimit, umask, unalias, unset.

    kdehelpcenter &

    (in X-terminal). Browse the whole system help using the graphical KDE help navigator.

    Normally, KDE help is invoked by pressing the appropriate icon on the KDE control panel.

    Use gnome-help-browser for the GNOME equivalent. (The older KDE versions used

    kdehelp& instead of kdehelpcenter&).

    2.3 GENERAL COMMANDS

    pwd

    Print working directory, i.e., display the name of my current directory on the screen.

    Chapter 2 - Linux Commands32

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    32/203

    hostname

    Print the name of the local host (the machine on which We are working). Use netconf (as

    root) to change the name of the machine.

    whoami

    Print my login name.

    id username

    Print user id (uid) and his/her group id (gid), effective id (if different than the real id) and the

    supplementary groups.

    whoDetermine the users logged on the machine.

    last

    Show listing of users last logged-in on the system. Really good idea to check it from time to

    time as a security measure on our system.

    history | more

    Show the last (1000 or so) commands executed from the command line on the current

    account. The | more causes the display to stop after each screen. To see what another

    user was doing on the system, login as root and inspect his/her history. The history iskept in the file .bash_history in the user home directory.

    uname -a

    (= Unix name with option all) Info on our (local) server. We can also use guname (in X-

    window terminal) to display the info more nicely.

    free

    Memory info (in kilobytes). Shared memory is the memory that can be shared between

    processes (e.g., executable code is shared). Buffered and cashed memory is the part

    that keeps parts of recently accessed filesit can be shrunk if more memory is needed by

    processes.

    set

    Show the current user environment (in full).

    echo $PATH

    Show the content of the environment variable PATH. This command can be used to show

    other environment variables as well. Use set to see the full environment.

    MSIT 3A Linux Internals 33

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    33/203

    ls or dir

    List the contents of the current directory. The command dir is an alias to ls so these two

    commands do exactly the same thing. The file listing is normally color-coded: dark blue=

    directories, light grey = regular files, green = executable files, magenta = graphics files, red

    = compressed (zipped) files, light blue = symbolic links, yellow = device files, brown = FIFO

    (First-In First-Out named pipes).

    ls -al |more

    List the content of the current directory, all files (also those starting with a dot), and in a long

    form. Pipe the output through the more command, so that the display pauses after each

    screenful. The ls command has several very useful options. Some of these may have shortcuts

    (aliases) to avoid clumsy typing. Try ll (=long ls, an alias to ls -l). Another option We use

    quite often is ls-ad (list all the subdirectories in my current directory, but dont list their

    contents).

    cd directory

    Change directory. Using cd without the directory name will take us to our home directory.

    cd - will take us to our previous directory and is a convenient way to toggle between two

    directories. cd .. will take me one directory up (very useful).

    shutdown -h now

    (as root) Shut down the system to a halt. Mostly used for a remote shutdown. Use for a shutdown at the console (which can be done by any user).

    halt or reboot or init 6

    (as root, three commands) Halt or reboot the machine. Used for remote shutdown, simpler

    to type than the previous command. Also great if the computer hangs (if we lose control

    over the keyboard)We can telnet to it from another machine on the network and remotely

    reboot it. We use for normal shutdown at the console of a local computer.

    2.4 FILE MANAGEMENT COMMANDS rm files

    Remove (delete) files. We must own the file in order to be able to remove it (or be root).

    On many systems, We will be asked for a confirmation of deletion; if We dont want this, use

    the - f (=force) option, e.g., rm -f * will remove all files in my current working directory, no

    questions asked.

    mkdir directory

    Make a new directory.

    Chapter 2 - Linux Commands34

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    34/203

    rmdir directory

    Remove an empty directory.

    rm -r files

    (recursive remove) Remove files, directories, and their subdirectories. Careful with this

    command as rootWe can easily remove all files on the system with such a command

    executed on the top of our directory tree, and there is no undelete in Linux (yet). But if We

    really wanted to do it (reconsider), here is how (as root): rm -rf /*

    rm -rf files

    (recursive force remove). As above, but skip the prompt for confirmation, if one is set on our

    system. Careful with this command particularly as rootsee the command above.

    cat filename | more

    View the content of a text file called filename, one page a time. The | is the pipe

    symbol (on many American keyboards it shares the key with \). more makes the output

    stop after each screenful. For long files, it is sometimes convenient to use the commands

    head and tail that display just the beginning and the end of the file, or less that enables

    scrolling up and down. If we happened to use cat a binary file and our terminal displays

    funny characters afterwards, we can restore it with the command reset.

    cat filename | less, less filename

    (two commands, use either) Scroll a content of a text file. Press q when done. less isroughly an equivalent to more , the command we know from DOS, but often less is more

    convenient than more because it lets me scroll both up and down.

    head filename

    Print first 10 lines of the (long) text file.

    tail filename

    Print last 10 lines of a long or growing text file. Use tail -f filename for tail to follow the file

    as it growsreally handy for continuing inspection of log files.

    ispell filename

    Spell check an ASCII text file. AbiWord, WordPerfect, StarOffice and other word processors

    come with as-we-type spellchecking, so we really dont have to worry about the simple

    ispell unless we need it.

    touch filename

    Change the date/time stamp of the filefilename to the current time. Create an empty file if

    the file does not exist. You can change the stamp to any date using touch -t 200201311759.30

    (year 2002 January day 31 time 17:59:30).

    MSIT 3A Linux Internals 35

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    35/203

    There are three date/time values associated with every file on an ext2 filesystem:

    - the time of last access to the file (atime)

    - the time of last modification to the file (mtime)

    - the time of last change to the files inode (ctime).

    Touch will change the first two to the value specified, and the last one always to the current

    system time. They can all be read using the stat command.

    stat filename

    Print general info about a file (the contents of the so-called inode).

    strings filename | more

    Display the strings contained in the binary file calledfilename. strings could, for example,

    be a useful first step to a close examination of an unknown executable.

    od

    (=octal dump). Display contents as octal numbers. This can be useful when the output contains

    non-printable characters. For example, a filename may contain non-printable characters and

    be a real pain. This can also be handy to view binary files.

    Examples: dir | od -c | more

    sort -f filename

    Arrange the lines in filename according to the ascii order. The option -f tells sort to ignore

    the upper and lower character case.

    uniq

    (=unique) Eliminate duplicate lines in sorted input. Example: sort myfile | uniq

    fold -w 30 -s my_file.txt > new_file.txt

    Wrap the lines in the text file my_file.txt so that there is 30 characters per line. Break the

    lines on spaces. Output goes to new_file.txt.

    rev filename > filename1

    Print the filefilename, each line in reversed order. In the example above, the output is

    directed to the filefilename1.

    paste file1 file2 > file3

    Merge two or more text files on lines using as delimiter (use option d= to specify

    our own delimiter(s).

    Chapter 2 - Linux Commands36

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    36/203

    join file1 file2 > file3

    Join lines of two files on a common field. join parallels the database operation join tables,

    but works on text tables. The default is to join on the first field of the first table, and the

    default delimiter is white space.

    2.5 FINDING FILES

    find / -name filename

    Find the file called filename on our filesystem starting the search from the root directory /

    . The filename may contain wildcards (*,?).

    locate filename

    Find the file name which contains the string filename. Easier and faster than the previous

    command but depends on a database that normally rebuilds at night, so we cannot find a file

    that was just saved to the filesystem. To force the immediate update of the database, We

    may do (as root): updatedb&.

    which executable_name

    Show me the full path to the executable that would run if we just typed its name on the

    command line. For example, this command:

    whereis command

    Print the locations for the binary, source, and manual page files of the command command.

    2.6 LINUX FILES AND FILE PERMISSION

    Linux files are setup so access to them is controlled. There are three types of access:

    1. read

    2. write

    3. execute

    Each file belongs to a specific user and group. Access to the files is controlled by user, group,

    and what is called other. The term, other, is used to refer to someone who is not the user

    (owner) of the file, nor is the person a member of the group the file belongs to. When talking

    about setting permissions for other users to use, it is commonly referred to as setting the

    world execute, read, or write bit since anyone in the world will be able to perform the

    operation if the permission is set in the other category.

    MSIT 3A Linux Internals 37

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    37/203

    2.7 FILE NAMES AND PERMISSION CHARACTERS File names can be up to 256 characters long with -, _, and . characters along with

    letters and numbers. When a long file listing is done , there are 10 characters that are shown

    on the left that indicate type and permissions of the file. File permissions are shown according

    to the following syntax example: drwxrwxrwx

    There are a total of 10 characters in this example for all Linux / Unix files. The first character

    indicates the type of file, and the next three indicate read, write, and execute permission for

    each of the three user types, user, group and other. Since there are three types of permission

    for three users, there are a total of nine permission bits. The table below shows the syntax:

    v Character 1 is the type of file: - is ordinary, d is directory, l is link.

    v Characters 2-4 show owner permissions. Character 2 indicates read permission,

    character 3 indicates write permission, and character 4 indicates execute permission.

    vCharacters 5-7 show group permissions. Character 5=read, 6=write, 7=execute

    v Characters 8-10 show permissions for all other users. Character 8=read, 9=write,

    10=execute

    There are 5 possible characters in the permission fields. They are:

    r = read - This is only found in the read field.

    w = write - This is only found in the write field.

    x = execute - This is only found in the execute field.

    s = setuid - This is only found in the execute field.

    If there is a - in a particular location, there is no permission. This may be found in any

    field of read, write, or execute.

    2.8 EXAMPLES ON FILES

    Type ls -l and a listing like the following is displayed:

    Chapter 2 - Linux Commands38

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    38/203

    total 10

    drwxrwxrwx 4 george team1 122 Dec 12 18:02 Projects

    -rw-rw-rw- 1 george team1 1873 Aug 23 08:34 test

    -rw-rw-rw- 1 george team1 1234 Sep 12 11:13 datafile

    Which means the following:

    The fields are as follows:

    1. Type field: The first character in the field indicates a file type of one of the following:

    d = directory

    l = symbolic link

    s = socket

    p = named pipe

    - = regular file

    c= character (un buffered) device file special

    b=block (buffered) device file special

    2. Permissions are explained above.

    3. Links: The number of directory entries that refer to the file. In our example, there are four.

    4. The files owner in our example is George.

    5. The group the file belongs to. In our example, the group is team1.

    6. The size of the file in bytes

    7. The last modification date. If the file is recent, the date and time is shown. If the file is not in

    the current year, the year is shown rather than time.

    8. The name of the file.

    MSIT 3A Linux Internals 39

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    39/203

    2.9 SETTING KEYSTROKES The stty command - Used to set up keystrokes on terminals [try $ stty a].

    The command will bind the Backspace key(^H) to the erase function.

    Characters that are specified:

    v eof - End of file

    v eol - End of line

    v eol2 - Alternate character for ending a line

    v erase - Erase the last character typed

    v intr - The specified character will send an interrupt signal

    v kill - Erase the current line.

    v quit - Sends a quit signal.

    v start - Restart output after having stopped it.

    v stop - Stops the output.

    v susp - Sends a terminal stop signal

    v switch - Switch to a different shell layer

    v werase - erase the last word typed.

    setterm foreground white Sets white foreground, and blue

    background blue background

    setterm store Stores the present terminal values as default

    2.10 KEYS AND TERMINAL CONFIGURATION

    Special command line keys:

    v CTRL U - Delete the line

    v CTRL C - Abort command

    v CTRL Z - Suspend command, Type fg to put it back into the foreground

    v - Auto complete command

    v F1 through F6 - Select a different virtual terminal

    Chapter 2 - Linux Commands40

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    40/203

    v F7 - Toggle to the first X terminal, if one is running.

    v F1 - Toggle to the first text terminal from an X session.

    v CTRL D - End of file, exits the shell

    v CTRL W - Delete a word backwards

    v CTRL / - Quit, weaker than CTRL C but does a core dump.

    v CTRL S - Stop scrolling, may use scroll lock for this function.

    v CTRL Q - Resume scrolling

    2.11 SUMMARY

    A UNIX command is a series of characters that we type. These characters consist of words

    that are separated by whitespace. Whitespace is the result of typing one or more Space or

    Tab keys. The first word is the name of the command. The rest of the words are called the

    commands arguments.

    The arguments give the command information that it might need, or specify varying behavior

    of the command. You can use command options to fine-tune the actions of a Linux command.

    Quite often, a Linux command will do almost, but not quite what you want it to do. Instead of

    making you learn a second command, Linux lets you modify the basic, or default, actions of

    the command by using options.

    Most Linux commands are very flexible. When you enter a Linux command, there are

    several ways to tailor the basic command to your specific needs. We will look at the two

    main ways used to modify the effect of a command:

    v Specifying or redirecting a commands input and output

    v Using command options

    Standard LINUX commands makes shell scripts more versatile. The power of scripts comes

    from coupling system commands and shell directives with simple programming constructs.

    2.12 CHECK YOUR PROGRESS

    I. Exercises

    Manipulating files

    In this exercise, well create, rename and delete files. Find out if the system has one or more printers

    as well as the appropriate command to use for printing.

    MSIT 3A Linux Internals 41

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    41/203

    Go to home directory. Enter cd

    Copy distant file to working directory. Entercp /etc/passwd myfile

    Create new directory. Enter mkdir temp

    List working directory. Enter ls F

    Move file to new directory. Enter mv myfile temp

    Change working directory. Enter cd temp

    Copy file to working directory. Enter cp myfilemyfile.two

    Print the file. Enter the printer command and the filename

    List filenames with wildcard. Enter ls -l myfile*

    Remove files. Enter rm myfile*

    Go up to parent directory. Enter cd ..

    Remove directory. Enter rmdir temp

    Verify that directory was removed. Enterls F

    II. Choose the correct answer:

    1) tac _______________________.

    a) concatenates 2 files

    b) listing a file backwards from its end

    c) reverses each line of file

    d) none of the above

    2) rmdir remove directory iff _____________.

    a) Directory must be empty of all files

    b) Directory maynt be empty

    c) Both a) & b)

    d) None of the above

    Chapter 2 - Linux Commands42

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    42/203

    3) touch command is used for ____________.

    a) Updating access/modification times of a file to current system time

    b) Creating a new file

    c) Both a) & b)

    d) None of the above

    4) Diff reports ____________________

    a) Differences between two files

    b) At what point the files differ

    c) Removes duplicate lines

    d) None of the above.

    5) egrep an acronym for __________________________.

    a) evaluation global regular expression print

    b) extensible global regular expression print

    c) extended global regular expression print

    d) none of the above.

    6) traceroute works within ______

    a) LAN

    b) WAN

    c) Over the internet

    d) All of the above

    7) If finger is used without an argument ___________________________

    a) it gives information concerning users currently logged in

    b) it gives information about a particular user on a network

    c) it gives information about all users

    d) none of the above

    MSIT 3A Linux Internals 43

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    43/203

    8) Protocol used for connecting to a remote host _______.

    a) ftp

    b) telnet

    c) tcp/ip

    d) none of the above

    9) df command is used for__________ purpose.

    a) Communication

    b) System & administrative

    c) Networking

    d) File & archiving

    10) Command used for shutting down the system ______

    a) halt

    b) exit

    c) quit

    d) logout

    11) The following are Network related Commands _______

    a) Wall

    b) ftp

    c) host

    d) none of the above

    Chapter 2 - Linux Commands44

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    44/203

    III. Say True or False1. mv is equivalent to a combination of cp & rm True/False

    2. mkdir p creates a new directory with necessary parent directories. True/False

    3. expr concatenates and evaluates the arguments according to the given

    operation True/False

    4. join pastes together only those lines with a common numerical label. True/False

    5. tr a-z \* < filename changes all the lowercase letters in filename to asterisks True/False

    6. exec shell builtin replaces the present process with a specified command. True/False

    7. kill command resumes a process. True/False

    IV. Essay Type Questions

    1. Write a short notes on the following with relevant options.

    a. cat & tac

    b. chmod

    c. touch

    d. sorte. grep

    2. Explain any three help, file and text Processing Commands.

    3. Write in detial about tar command with the relvant options.

    4. Give the differentiation between locate, find & slocate.

    5. Give a short note on the following communication commands.

    a. cat

    b. rm

    6. List the differences between the following.

    a. mv & cp

    b. tac & rev

    c. expr & let

    d. cmp & diff

    MSIT 3A Linux Internals 45

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    45/203

  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    46/203

    MSIT 3A Linux Internals 47

    Chapter 3

    Processes

    3.0 INTRODUCTION AND OBJECTIVES

    Arunning instance of a program is called aPROCESS. If we have two terminal windows showing

    on our screen, then we are probably running the same terminal program twicewe have two

    terminal processes. Each terminal window is probably running a shell; each running shell is

    another process. When we invoke a command from a shell, the corresponding program is executed in a

    new process; the shell process resumes when that process completes.

    Advanced programmers often use multiple cooperating processes in a single application to enable the

    application to do more than one thing at once, to increase application robustness, and to make use of

    already-existing programs.

    Most of the process manipulation functions described in this chapter are similar to those on other

    UNIX systems. Most are declared in the header file ; check the man page for each function to

    be sure.

    ObjectivesAfter completing this module you should be able to understand and utilize these features of the Linux

    processes:

    Looking at processes

    Process IDs

    Analysis of active processes

    Different output formats of ps

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    47/203

    Killing a process

    Processes creation mechanisms

    Using system, system calls - fork, exec

    Calling fork, using the exec Family

    Using fork and exec Together

    Process scheduling

    Signals and Linux daemons

    Most useful daemons

    Zombie process and threads

    Differentiating processes and threads

    3.1 LOOKING AT PROCESSES

    Even as we sit down at our computer, there are processes running. Every executing program

    uses one or more processes. Lets start by taking a look at the processes already running on

    the computer.

    3.2 PROCESS IDS Each process in a Linux system is identified by its uniqueprocess ID , sometimes referred to

    aspid. Process IDs are 16-bit numbers that are assigned sequentially by Linux as new

    processes are created.

    Every process also has a parent process. Thus, we can think of the processes on a Linux

    system as arranged in a tree, with the init process at its root. The parent process ID, or

    ppid, is simply the process ID of the processs parent.

    When referring to process IDs in a C or C++ program, always use the pid_t typedef, which

    is defined in .A program can obtain the process ID of the process its running

    in with the getpid() system call, and it can obtain the process ID of its parent process withthe getppid() system call. For instance, the program in Listing below prints its process ID

    and its parents process ID.

    Listing (print-pid.c) Printing the Process ID

    #include

    #include

    int main ()

    Chapter 3 - Processes48

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    48/203

    {

    printf (The process ID is %d\n, (int) getpid ());

    printf (The parent process ID is %d\n, (int) getppid ());

    return 0;

    }

    Observe that if we invoke this program several times, a different process ID is reported

    because each invocation is in a new process. However, if we invoke it every time from the

    same shell, the parent process ID (that is, the process ID of the shell process) is the same.

    3.3 VIEWING ACTIVE PROCESSES

    The ps command displays the processes that are running on our system. The Linux version

    ofps has lots of options because it tries to be compatible with versions ofps on several other

    UNIX variants. These options control which processes are listed and what information about

    each is shown.

    By default, invoking ps displays the processes controlled by the terminal or terminal window

    in which ps is invoked. For example:

    $ ps

    PID TTY TIME CMD

    21693 pts/8 00:00:00 bash

    21694 pts/8 00:00:00 ps

    This invocation ofps shows two processes. The first, bash, is the shell running on this

    terminal. The second is the running instance of the ps program itself. The first column,

    labeled PID, displays the process ID of each.

    For a more detailed look at whats running on our Linux system, invoke this:

    $ps -e -o pid,ppid,command

    The -e option instructs ps to display all processes running on the system. The -o

    pid,ppid,command option tells ps what information to show about each process in this

    case, the process ID, the parent process ID, and the command running in this process.

    3.4 PS OUTPUT FORMATS

    With the -o option to the ps command, we specify the information about processes that we

    MSIT 3A Linux Internals 49

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    49/203

    want in the output as a comma-separated list. For example, ps -o

    pid,user,start_time,commanddisplays the process ID, the name of the user owning the

    process, the wall clock time at which the process started, and the command running in the

    process. See the man page forps for the full list of field codes. We can use the -f(full

    listing), -l (long listing), or-j (jobs listing) options instead to get three different preset listing

    formats.

    Here are the first few line and last few lines of output from this command on the system. We

    may see different output, depending on whats running on our system.

    $ ps -e -o pid,ppid,command

    PID PPID COMMAND1 0 init [5]

    2 1 [kflushd]

    3 1 [kupdate]

    ...

    21725 21693 xterm

    21727 21725 bash

    21728 21727 ps -e -o pid,ppid,command

    Note that the parent process ID of the ps command, 21727, is the process ID of bash, the

    shell from which we invoked ps. The parent process ID ofbash is in turn 21725, the processID of the xterm program in which the shell is running.

    Using ps provides us a snapshot of the systems active processes. The following are the

    fields from the output of the ps command that are important in terms of performance tuning:

    Field Description

    F Flags that indicate the processs current state and are calculated by adding each

    of the hexadecimal values:

    00 Process has terminated

    01 System process, always in memory02 Process is being traced by its parent

    04 Process is being traced by parent, and is stopped

    08 Process cannot be awakened by a signal

    10 Process is in memory and locked, pending an event

    20 Process cannot be swapped

    S The current state of the process, as indicated by one of the following letters:

    Chapter 3 - Processes50

    PDF created with pdfFactory Pro trial version www.pdffactory.com

    http://www.pdffactory.com/http://www.pdffactory.com/
  • 8/3/2019 MSIT-3A Linux Internals, Unix and shell programming, M.V. Panduranga Rao, K.C. Shet, Roopa krishnaji Rao.

    50/203

    O Process is currently running on the processorS Process is sleeping, waiting for an I/O event (including terminal I/O) to complete

    R Process is ready to run

    I Process is idle

    Z Process is a zombie process (it has terminated, and the parent is not waiting

    but is still in the process table)

    T Process is stopped because of parent tracing it

    X Process is waiting for more memory

    3.5 KILLING A PROCESS

    While logged in as root, type ps -ax | more orps -aux | more. We will get a list of all

    processes running on our computer. We will see the process id (PID), process status (STAT)

    various statistics, and the command name. We can kill a process by typing kill and the

    PID number right afterwards similar to the line below.

    $ kill 1721

    We can also stop and restart processes by sending them various signals


Recommended