Linux Yourself Linux Yourself Concept and Programming Sunil K. Singh First edition published 2022 by CRC Press 6000 Broken Sound Parkway NW, Suite 300, Boca Raton, FL 33487-2742 and by CRC Press 2 Park Square, Milton Park, Abingdon, Oxon, OX14 4RN © 2022 Taylor & Francis Group, LLC CRC Press is an imprint of Taylor & Francis Group, LLC Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use. The authors and p ublishers have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been obtained. If any copyright material has not been acknowledged, please write and let us know so we may rectify in any future reprint. Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission from the publishers. For permission to photocopy or use material electronically from this work, access www.copyright.com or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. For works that are not available on CCC, please contact [email protected] Trademark notice: Product or corporate names may be trademarks or registered trademarks and are used only for identification and explanation without intent to infringe. ISBN: 978-1-138-33328-4 (hbk) ISBN: 978-1-032-03707-3 (pbk) ISBN: 978-0-429-44604-7 (ebk) Typeset in Palatino by codeMantra This book is dedicated to To my mother, Smt. Sita Singh, For unmatched love and encouragement. You have always been my source of support and inspiration: “Thank you, Mom for always teaching me that the greatest endeavour happens outside from your comfort region.” Contents Preface ..........................................................................................................................................xvii Acknowledgments ....................................................................................................................xxiii Author ..........................................................................................................................................xxv Section I Linux Concept 1. Getting Started ........................................................................................................................3 1.1 Getting Started with OS ...............................................................................................3 1.2 A Brief Overview of Unix and Linux .........................................................................4 1.3 Unix/Linux History ......................................................................................................5 1.4 The GNU Project ...........................................................................................................8 1.4.1 Open-Source Software ....................................................................................9 1.5 Features and Advantages of Linux ...........................................................................10 1.5.1 Advantages .....................................................................................................11 1.6 Linux Distributions ....................................................................................................12 1.7 Installation Procedure and Issues ............................................................................12 1.7.1 Linux Installation ...........................................................................................12 1.7.2 Key Issues to Install Linux ...........................................................................13 1.7.3 Key Steps of Linux Installation ....................................................................14 1.8 Linux Architecture .....................................................................................................15 1.9 Shell and Its Features ..................................................................................................16 1.10 An Overview of Kernels ............................................................................................18 1.10.1 Kernel Module ................................................................................................19 1.10.2 Linux Versions ................................................................................................20 1.11 The GNOME and KDE Desktops ..............................................................................21 1.11.1 The Window Manager for GUI ....................................................................21 1.11.2 GNOME Desktop ...........................................................................................21 1.11.3 KDE Desktop ..................................................................................................22 1.12 Boot Loaders ................................................................................................................22 1.12.1 BIOS .................................................................................................................23 1.13 Linux Interface: GUI and CUI ...................................................................................24 1.14 Login and Logout ........................................................................................................26 1.14.1 Login ................................................................................................................26 1.14.2 Logout ..............................................................................................................27 1.14.3 Switching Users..............................................................................................29 1.14.4 Shutdown ........................................................................................................29 1.15 Start-Up Scripts and Run Levels ...............................................................................30 1.15.1 Start-Up Script ................................................................................................30 1.15.2 Run Levels (init, inittab, and rc Files) .........................................................30 1.16 Summary ......................................................................................................................31 1.17 Review Exercises .........................................................................................................31 References ...............................................................................................................................33 vii viii Contents 2. Linux Commands .................................................................................................................35 2.1 Command Syntax, Options, and Arguments .........................................................36 2.2 Internal and External Commands ............................................................................38 2.3 Command Location and User Commands .............................................................39 2.3.1 U ser Commands ............................................................................................44 2.3.2 U niversal Commands ...................................................................................44 2.3.3 S ystem Commands ........................................................................................44 2.4 Communication and Other Commands ..................................................................44 2.7 S ummary ......................................................................................................................51 2.8 R eview Exercises .........................................................................................................52 References ...............................................................................................................................53 3. The Shell .................................................................................................................................55 3.1 What is a Shell? ............................................................................................................55 3.2 Why Use a Shell in Linux? .........................................................................................56 3.3 The Login Shell (Shell Prompt) .................................................................................56 3.4 Command Line Structure of Shells .........................................................................57 3.5 s h Command ................................................................................................................58 3.6 Basics and Interpretive Cycle of Shells ....................................................................59 3.7 Starting a Terminal Shell ...........................................................................................60 3.8 Shell Variables: User-Defined and Predefined ........................................................61 3.8.1 G lobal Variables .............................................................................................61 3.8.2 L ocal Variables ...............................................................................................64 3.8.2.1 V ariable “Type” ..............................................................................64 3.8.2.2 Creating and Setting User-Defined Variables: =, $, set(export), unset ............................................................................64 3.8.2.3 How to Set a Local variable into a Global Environment Variable ............................................................................................66 3.8.2.4 How to Unset a Local Variable from a Global Environment Variable ....................................................................67 3.8.2.5 How to Set the PATH Environment Variable .............................67 3.9 Various Shell Types .....................................................................................................68 3.9.1 Bourne Shell (sh) ............................................................................................69 3.9.2 Bourne Again Shell (bash) ............................................................................69 3.10 C ommand Execution ..................................................................................................71 3.10.1 S equence Commands ....................................................................................72 3.10.2 G rouped Commands .....................................................................................73 3.10.3 C hained Commands .....................................................................................73 3.10.4 C ondition Commands ...................................................................................74 3.11 Standard Input/Output Redirection ........................................................................76 3.12 P ipes ..............................................................................................................................78 3.13 t ee Command ..............................................................................................................78 3.14 x args Command ..........................................................................................................79 3.15 Backslash (\) and Quotes ...........................................................................................80 3.16 Building Shell Commands .........................................................................................82 3.17 S hell Scripts ..................................................................................................................83 3.18 S ummary .....................................................................................................................84 3.19 R eview Exercises .........................................................................................................85 References ...............................................................................................................................87 Contents ix 4. vi Editor ..................................................................................................................................89 4.1 I ntroduction .................................................................................................................89 4.1.1 I nvoke vi ..........................................................................................................89 4.2 Modes in vi Editor ......................................................................................................91 4.3 Start, Edit, and Close Files .........................................................................................93 4.3.1 E diting .............................................................................................................94 4.3.2 Saving Your Work and Quitting ..................................................................94 4.3.3 Open and Recover File from a Crash (:recover and -r) .............................94 4.4 Various vi Commands ................................................................................................95 4.4.1 Moving the Cursor .........................................................................................96 4.4.2 Inserting or Appending Text. .......................................................................96 4.4.3 R eplacing Text ................................................................................................97 4.4.4 U ndoing Mistakes ..........................................................................................97 4.4.5 S creen Navigation ..........................................................................................98 4.4.6 D eleting Text ...................................................................................................98 4.4.7 Cutting, Pasting, and Copying Text ..........................................................100 4.5 G lobal Replacement ..................................................................................................101 4.6 C ommand Combinations .........................................................................................105 4.7 v i Programming ........................................................................................................107 4.8 vim (vi Improved) and nvi (New vi) ......................................................................110 4.9 GNOME Editor: gedit ...............................................................................................112 4.9.1 Key Features of gedit ...................................................................................112 4.10 Emacs Editor and Commands .................................................................................113 4.10.1 Starting and Quitting emacs ......................................................................114 4.11 Summary ....................................................................................................................116 4.12 R eview Exercises .......................................................................................................116 References .............................................................................................................................118 5. Regular Expressions and Filters ......................................................................................119 5.1 R egular Expressions .................................................................................................119 5.1.1 What’s the Variance between BRE and ERE? ..........................................120 5.1.2 Meaning of Various Characters and Metacharacters in Regular Expressions ....................................................................................120 5.2 g rep Family ................................................................................................................120 5.2.1 grep Associated with Exit Status ...............................................................123 5.3 Other Regular Filters (with Examples) ..................................................................127 5.3.1 cat: Concatenate Files and Display the File Contents .............................127 5.3.2 T he comm Command: to Compare Two Sorted Files Line by Line .......129 5.3.3 T he cut Command: Remove Sections from Each Line of Files ..............130 5.3.4 T he expand Command: to Convert Tabs into Spaces...............................130 5.3.5 T he compress Command: to Compress Data ............................................131 5.3.6 T he fold Command: to Break Each Line of Input Text to Fit in Specified Line Width ...................................................................................131 5.3.7 T he head Command: to Display the Starting Part of File .......................132 5.3.8 T he more Command: File Checking Filter for Control Viewing ...........133 5.3.9 T he less Command: to Scroll and View Text ............................................133 5.3.10 T he nl Command: to Number the Lines in a File ....................................134 5.3.11 Perl: Practical Extraction and Report Language......................................136 5.3.12 T he pr Command: Formatting Text Files for Printing ............................136