You are previewing The Linux Command Line.
O'Reilly logo
The Linux Command Line

Book Description

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

Table of Contents

  1. The Linux Command Line
    1. Acknowledgments
    2. Introduction
      1. Why Use the Command Line?
      2. What This Book Is About
      3. Who Should Read This Book
      4. What’s in This Book
      5. How to Read This Book
      6. Prerequisites
    3. I. Learning the Shell
      1. 1. What Is the Shell?
        1. Terminal Emulators
        2. Your First Keystrokes
          1. Command History
          2. Cursor Movement
        3. Try Some Simple Commands
        4. Ending a Terminal Session
      2. 2. Navigation
        1. Understanding the Filesystem Tree
        2. The Current Working Directory
        3. Listing the Contents of a Directory
        4. Changing the Current Working Directory
          1. Absolute Pathnames
          2. Relative Pathnames
          3. Some Helpful Shortcuts
      3. 3. Exploring the System
        1. More Fun with ls
          1. Options and Arguments
          2. A Longer Look at Long Format
        2. Determining a File’s Type with file
        3. Viewing File Contents with less
        4. A Guided Tour
        5. Symbolic Links
      4. 4. Manipulating Files and Directories
        1. Wildcards
        2. mkdir—Create Directories
        3. cp—Copy Files and Directories
        4. mv—Move and Rename Files
        5. rm—Remove Files and Directories
        6. ln—Create Links
          1. Hard Links
          2. Symbolic Links
        7. Let’s Build a Playground
          1. Creating Directories
          2. Copying Files
          3. Moving and Renaming Files
          4. Creating Hard Links
          5. Creating Symbolic Links
          6. Removing Files and Directories
        8. Final Note
      5. 5. Working with Commands
        1. What Exactly Are Commands?
        2. Identifying Commands
          1. type—Display a Command’s Type
          2. which—Display an Executable’s Location
        3. Getting a Command’s Documentation
          1. help—Get Help for Shell Builtins
          2. --help—Display Usage Information
          3. man—Display a Program’s Manual Page
          4. apropos—Display Appropriate Commands
          5. whatis—Display a Very Brief Description of a Command
          6. info—Display a Program’s Info Entry
          7. README and Other Program Documentation Files
        4. Creating Your Own Commands with alias
        5. Revisiting Old Friends
      6. 6. Redirection
        1. Standard Input, Output, and Error
          1. Redirecting Standard Output
          2. Redirecting Standard Error
          3. Redirecting Standard Output and Standard Error to One File
          4. Disposing of Unwanted Output
          5. Redirecting Standard Input
            1. cat—Concatenate Files
        2. Pipelines
          1. Filters
          2. uniq—Report or Omit Repeated Lines
          3. wc—Print Line, Word, and Byte Counts
          4. grep—Print Lines Matching a Pattern
          5. head/tail—Print First/Last Part of Files
          6. tee—Read from Stdin and Output to Stdout and Files
        3. Final Note
      7. 7. Seeing the World as the Shell Sees It
        1. Expansion
          1. Pathname Expansion
          2. Tilde Expansion
          3. Arithmetic Expansion
          4. Brace Expansion
          5. Parameter Expansion
          6. Command Substitution
        2. Quoting
          1. Double Quotes
          2. Single Quotes
          3. Escaping Characters
        3. Final Note
      8. 8. Advanced Keyboard Tricks
        1. Command Line Editing
          1. Cursor Movement
          2. Modifying Text
          3. Cutting and Pasting (Killing and Yanking) Text
        2. Completion
        3. Using History
          1. Searching History
          2. History Expansion
        4. Final Note
      9. 9. Permissions
        1. Owners, Group Members, and Everybody Else
        2. Reading, Writing, and Executing
          1. chmod—Change File Mode
            1. Octal Representation
            2. Symbolic Representation
          2. Setting File Mode with the GUI
          3. umask—Set Default Permissions
        3. Changing Identities
          1. su—Run a Shell with Substitute User and Group IDs
          2. sudo—Execute a Command as Another User
          3. chown—Change File Owner and Group
          4. chgrp—Change Group Ownership
        4. Exercising Your Privileges
        5. Changing Your Password
      10. 10. Processes
        1. How a Process Works
          1. Viewing Processes with ps
          2. Viewing Processes Dynamically with top
        2. Controlling Processes
          1. Interrupting a Process
          2. Putting a Process in the Background
          3. Returning a Process to the Foreground
          4. Stopping (Pausing) a Process
        3. Signals
          1. Sending Signals to Processes with kill
          2. Sending Signals to Multiple Processes with killall
        4. More Process-Related Commands
    4. II. Configuration and the Environment
      1. 11. The Environment
        1. What Is Stored in the Environment?
          1. Examining the Environment
          2. Some Interesting Variables
        2. How Is the Environment Established?
          1. Login and Non-login Shells
          2. What’s in a Startup File?
        3. Modifying the Environment
          1. Which Files Should We Modify?
          2. Text Editors
          3. Using a Text Editor
          4. Activating Our Changes
        4. Final Note
      2. 12. A Gentle Introduction to vi
        1. Why We Should Learn vi
        2. A Little Background
        3. Starting and Stopping vi
        4. Editing Modes
          1. Entering Insert Mode
          2. Saving Our Work
        5. Moving the Cursor Around
        6. Basic Editing
          1. Appending Text
          2. Opening a Line
          3. Deleting Text
          4. Cutting, Copying, and Pasting Text
          5. Joining Lines
        7. Search and Replace
          1. Searching Within a Line
          2. Searching the Entire File
          3. Global Search and Replace
        8. Editing Multiple Files
          1. Switching Between Files
          2. Opening Additional Files for Editing
          3. Copying Content from One File into Another
          4. Inserting an Entire File into Another
        9. Saving Our Work
      3. 13. Customizing the Prompt
        1. Anatomy of a Prompt
        2. Trying Some Alternative Prompt Designs
        3. Adding Color
        4. Moving the Cursor
        5. Saving the Prompt
        6. Final Note
    5. III. Common Tasks and Essential Tools
      1. 14. Package Management
        1. Packaging Systems
        2. How a Package System Works
          1. Package Files
          2. Repositories
          3. Dependencies
          4. High- and Low-Level Package Tools
        3. Common Package Management Tasks
          1. Finding a Package in a Repository
          2. Installing a Package from a Repository
          3. Installing a Package from a Package File
          4. Removing a Package
          5. Updating Packages from a Repository
          6. Upgrading a Package from a Package File
          7. Listing Installed Packages
          8. Determining Whether a Package Is Installed
          9. Displaying Information About an Installed Package
          10. Finding Which Package Installed a File
        4. Final Note
      2. 15. Storage Media
        1. Mounting and Unmounting Storage Devices
          1. Viewing a List of Mounted Filesystems
          2. Determining Device Names
        2. Creating New Filesystems
          1. Manipulating Partitions with fdisk
          2. Creating a New Filesystem with mkfs
        3. Testing and Repairing Filesystems
        4. Formatting Floppy Disks
        5. Moving Data Directly to and from Devices
        6. Creating CD-ROM Images
          1. Creating an Image Copy of a CD-ROM
          2. Creating an Image from a Collection of Files
        7. Writing CD-ROM Images
          1. Mounting an ISO Image Directly
          2. Blanking a Rewritable CD-ROM
          3. Writing an Image
        8. Extra Credit
      3. 16. Networking
        1. Examining and Monitoring a Network
          1. ping—Send a Special Packet to a Network Host
          2. traceroute—Trace the Path of a Network Packet
          3. netstat—Examine Network Settings and Statistics
        2. Transporting Files over a Network
          1. ftp—Transfer Files with the File Transfer Protocol
          2. lftp—A Better ftp
          3. wget—Non-interactive Network Downloader
        3. Secure Communication with Remote Hosts
          1. ssh—Securely Log in to Remote Computers
          2. scp and sftp—Securely Transfer Files
      4. 17. Searching for Files
        1. locate—Find Files the Easy Way
        2. find—Find Files the Hard Way
          1. Tests
            1. Operators
          2. Actions
            1. Predefined Actions
            2. User-Defined Actions
            3. Improving Efficiency
          3. A Return to the Playground
          4. Options
      5. 18. Archiving and Backup
        1. Compressing Files
          1. gzip—Compress or Expand Files
          2. bzip2—Higher Compression at the Cost of Speed
        2. Archiving Files
          1. tar—Tape Archiving Utility
          2. zip—Package and Compress Files
        3. Synchronizing Files and Directories
          1. rsync—Remote File and Directory Synchronization
          2. Using rsync over a Network
      6. 19. Regular Expressions
        1. What Are Regular Expressions?
        2. grep—Search Through Text
        3. Metacharacters and Literals
        4. The Any Character
        5. Anchors
        6. Bracket Expressions and Character Classes
          1. Negation
          2. Traditional Character Ranges
          3. POSIX Character Classes
        7. POSIX Basic vs. Extended Regular Expressions
        8. Alternation
        9. Quantifiers
          1. ?—Match an Element Zero Times or One Time
          2. *—Match an Element Zero or More Times
          3. +—Match an Element One or More Times
          4. { }—Match an Element a Specific Number of Times
        10. Putting Regular Expressions to Work
          1. Validating a Phone List with grep
          2. Finding Ugly Filenames with find
          3. Searching for Files with locate
          4. Searching for Text with less and vim
        11. Final Note
      7. 20. Text Processing
        1. Applications of Text
          1. Documents
          2. Web Pages
          3. Email
          4. Printer Output
          5. Program Source Code
        2. Revisiting Some Old Friends
          1. cat—Concatenate Files and Print on Standard Output
          2. sort—Sort Lines of Text Files
          3. uniq—Report or Omit Repeated Lines
        3. Slicing and Dicing
          1. cut—Remove Sections from Each Line of Files
          2. paste—Merge Lines of Files
          3. join—Join Lines of Two Files on a Common Field
        4. Comparing Text
          1. comm—Compare Two Sorted Files Line by Line
          2. diff—Compare Files Line by Line
          3. patch—Apply a diff to an Original
        5. Editing on the Fly
          1. tr—Transliterate or Delete Characters
          2. sed—Stream Editor for Filtering and Transforming Text
          3. aspell—Interactive Spell Checker
        6. Final Note
        7. Extra Credit
      8. 21. Formatting Output
        1. Simple Formatting Tools
          1. nl—Number Lines
          2. fold—Wrap Each Line to a Specified Length
          3. fmt—A Simple Text Formatter
          4. pr—Format Text for Printing
          5. printf—Format and Print Data
        2. Document Formatting Systems
          1. The roff Family and TEX
          2. groff—A Document Formatting System
        3. Final Note
      9. 22. Printing
        1. A Brief History of Printing
          1. Printing in the Dim Times
          2. Character-Based Printers
          3. Graphical Printers
        2. Printing with Linux
        3. Preparing Files for Printing
          1. pr—Convert Text Files for Printing
        4. Sending a Print Job to a Printer
          1. lpr—Print Files (Berkeley Style)
          2. lp—Print Files (System V Style)
          3. Another Option: a2ps
        5. Monitoring and Controlling Print Jobs
          1. lpstat—Display Print System Status
          2. lpq—Display Printer Queue Status
          3. lprm and cancel—Cancel Print Jobs
      10. 23. Compiling Programs
        1. What Is Compiling?
          1. Are All Programs Compiled?
        2. Compiling a C Program
          1. Obtaining the Source Code
          2. Examining the Source Tree
          3. Building the Program
          4. Installing the Program
        3. Final Note
    6. IV. Writing Shell Scripts
      1. 24. Writing Your First Script
        1. What Are Shell Scripts?
        2. How to Write a Shell Script
          1. Script File Format
          2. Executable Permissions
          3. Script File Location
          4. Good Locations for Scripts
        3. More Formatting Tricks
          1. Long Option Names
          2. Indentation and Line Continuation
        4. Final Note
      2. 25. Starting a Project
        1. First Stage: Minimal Document
        2. Second Stage: Adding a Little Data
        3. Variables and Constants
          1. Creating Variables and Constants
          2. Assigning Values to Variables and Constants
        4. Here Documents
        5. Final Note
      3. 26. Top-Down Design
        1. Shell Functions
        2. Local Variables
        3. Keep Scripts Running
        4. Final Note
      4. 27. Flow Control: Branching with if
        1. Using if
        2. Exit Status
        3. Using test
          1. File Expressions
          2. String Expressions
          3. Integer Expressions
        4. A More Modern Version of test
        5. (( ))—Designed for Integers
        6. Combining Expressions
        7. Control Operators: Another Way to Branch
        8. Final Note
      5. 28. Reading Keyboard Input
        1. read—Read Values from Standard Input
          1. Options
          2. Separating Input Fields with IFS
        2. Validating Input
        3. Menus
        4. Final Note
        5. Extra Credit
      6. 29. Flow Control: Looping with while and until
        1. Looping
        2. while
        3. Breaking out of a Loop
        4. until
        5. Reading Files with Loops
        6. Final Note
      7. 30. Troubleshooting
        1. Syntactic Errors
          1. Missing Quotes
          2. Missing or Unexpected Tokens
          3. Unanticipated Expansions
        2. Logical Errors
          1. Defensive Programming
          2. Verifying Input
        3. Testing
          1. Stubs
          2. Test Cases
        4. Debugging
          1. Finding the Problem Area
          2. Tracing
          3. Examining Values During Execution
        5. Final Note
      8. 31. Flow Control: Branching with case
        1. case
          1. Patterns
          2. Combining Multiple Patterns
        2. Final Note
      9. 32. Positional Parameters
        1. Accessing the Command Line
          1. Determining the Number of Arguments
          2. shift—Getting Access to Many Arguments
          3. Simple Applications
          4. Using Positional Parameters with Shell Functions
        2. Handling Positional Parameters En Masse
        3. A More Complete Application
        4. Final Note
      10. 33. Flow Control: Looping with for
        1. for: Traditional Shell Form
        2. for: C Language Form
        3. Final Note
      11. 34. Strings and Numbers
        1. Parameter Expansion
          1. Basic Parameters
          2. Expansions to Manage Empty Variables
          3. Expansions That Return Variable Names
          4. String Operations
        2. Arithmetic Evaluation and Expansion
          1. Number Bases
          2. Unary Operators
          3. Simple Arithmetic
          4. Assignment
          5. Bit Operations
          6. Logic
        3. bc—An Arbitrary-Precision Calculator Language
          1. Using bc
          2. An Example Script
        4. Final Note
        5. Extra Credit
      12. 35. Arrays
        1. What Are Arrays?
        2. Creating an Array
        3. Assigning Values to an Array
        4. Accessing Array Elements
        5. Array Operations
          1. Outputting the Entire Contents of an Array
          2. Determining the Number of Array Elements
          3. Finding the Subscripts Used by an Array
          4. Adding Elements to the End of an Array
          5. Sorting an Array
          6. Deleting an Array
        6. Final Note
      13. 36. Exotica
        1. Group Commands and Subshells
          1. Performing Redirections
          2. Process Substitution
        2. Traps
        3. Asynchronous Execution
          1. wait
        4. Named Pipes
          1. Setting Up a Named Pipe
          2. Using Named Pipes
        5. Final Note
    7. Index
    8. About the Author
    9. Colophon
    10. A. Updates