The Linux Command Line - Stellenbosch University

The Linux Command Line

Second Internet Edition

William E. Shotts, Jr.

A Book

Copyright ?2008-2013, William E. Shotts, Jr.

This work is licensed under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License. To view a copy of this license, visit the link above or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.

Linux? is the registered trademark of Linus Torvalds. All other trademarks belong to their respective owners.

This book is part of the project, a site for Linux education and advocacy devoted to helping users of legacy operating systems migrate into the future. You may contact the project at .

This book is also available in printed form, published by No Starch Press and may be purchased wherever fine books are sold. No Starch Press also offers this book in electronic formats for most popular e-readers:

Release History

Version 13.07

Date July 6, 2013

Description Second Internet Edition.

09.12

December 14, 2009 First Internet Edition.

09.11

November 19, 2009 Fourth draft with almost all reviewer feedback incorporated and edited through chapter 37.

09.10

October 3, 2009

Third draft with revised table formatting, partial application of reviewers feedback and edited through chapter 18.

09.08

August 12, 2009

Second draft incorporating the first editing pass.

09.07

July 18, 2009

Completed first draft.

Table of Contents

Introduction....................................................................................................xvi

Why Use The Command Line?.....................................................................................xvi What This Book Is About..............................................................................................xvii Who Should Read This Book.......................................................................................xvii What's In This Book.....................................................................................................xviii How To Read This Book..............................................................................................xviii

Prerequisites............................................................................................................xix Why I Don't Call It "GNU/Linux"...........................................................................xix

Acknowledgments..........................................................................................................xx Your Feedback Is Needed!............................................................................................xx What's New In The Second Internet Edition.................................................................xxi Further Reading............................................................................................................xxi Colophon.......................................................................................................................xxi

Part 1 ? Learning The Shell..............................................................1

1 ? What Is The Shell?.....................................................................................2

Terminal Emulators..........................................................................................................2 Your First Keystrokes......................................................................................................2

Command History.......................................................................................................3 Cursor Movement.......................................................................................................3

A Few Words About Mice And Focus....................................................................3 Try Some Simple Commands..........................................................................................4 Ending A Terminal Session..............................................................................................5

The Console Behind The Curtain..........................................................................5 Summing Up....................................................................................................................5 Further Reading...............................................................................................................6

2 ? Navigation...................................................................................................7

Understanding The File System Tree..............................................................................7 The Current Working Directory........................................................................................7 Listing The Contents Of A Directory................................................................................8 Changing The Current Working Directory.......................................................................9

Absolute Pathnames..................................................................................................9 Relative Pathnames...................................................................................................9 Some Helpful Shortcuts............................................................................................11

Important Facts About Filenames........................................................................11

i

Summing Up..................................................................................................................12

3 ? Exploring The System.............................................................................13

More Fun With ls...........................................................................................................13 Options And Arguments............................................................................................14 A Longer Look At Long Format.................................................................................16

Determining A File's Type With file................................................................................17 Viewing File Contents With less....................................................................................17

What Is "Text"?.....................................................................................................17 Less Is More........................................................................................................19 A Guided Tour................................................................................................................19 Symbolic Links...............................................................................................................23 Hard Links.....................................................................................................................24 Summing Up..................................................................................................................24 Further Reading.............................................................................................................24

4 ? Manipulating Files And Directories........................................................25

Wildcards.......................................................................................................................25 Character Ranges................................................................................................27 Wildcards Work In The GUI Too..........................................................................27

mkdir ? Create Directories.............................................................................................28 cp ? Copy Files And Directories....................................................................................28

Useful Options And Examples..................................................................................29 mv ? Move And Rename Files......................................................................................30

Useful Options And Examples..................................................................................30 rm ? Remove Files And Directories...............................................................................31

Useful Options And Examples..................................................................................31 Be Careful With rm!.............................................................................................32

ln ? Create Links............................................................................................................33 Hard Links................................................................................................................33 Symbolic Links..........................................................................................................33

Let's Build A Playground................................................................................................34 Creating Directories..................................................................................................34 Copying Files............................................................................................................34 Moving And Renaming Files.....................................................................................35 Creating Hard Links..................................................................................................37 Creating Symbolic Links...........................................................................................38 Removing Files And Directories...............................................................................39 Creating Symlinks With The GUI.........................................................................40

Summing Up..................................................................................................................41 Further Reading.............................................................................................................41

5 ? Working With Commands........................................................................42

What Exactly Are Commands?......................................................................................42 Identifying Commands...................................................................................................43

type ? Display A Command's Type...........................................................................43 which ? Display An Executable's Location...............................................................43 Getting A Command's Documentation..........................................................................44 help ? Get Help For Shell Builtins............................................................................44 --help ? Display Usage Information..........................................................................45

ii

man ? Display A Program's Manual Page................................................................45 apropos ? Display Appropriate Commands..............................................................47 whatis ? Display A Very Brief Description Of A Command.......................................47

The Most Brutal Man Page Of Them All..............................................................48 info ? Display A Program's Info Entry.......................................................................48 README And Other Program Documentation Files................................................49 Creating Your Own Commands With alias....................................................................50 Summing Up..................................................................................................................52 Further Reading.............................................................................................................52

6 ? Redirection................................................................................................53

Standard Input, Output, And Error.................................................................................53 Redirecting Standard Output.........................................................................................54 Redirecting Standard Error............................................................................................55

Redirecting Standard Output And Standard Error To One File................................56 Disposing Of Unwanted Output................................................................................57

/dev/null In Unix Culture......................................................................................57 Redirecting Standard Input............................................................................................57

cat ? Concatenate Files............................................................................................57 Pipelines........................................................................................................................59

The Difference Between > and |..........................................................................60 Filters........................................................................................................................61 uniq - Report Or Omit Repeated Lines.....................................................................61 wc ? Print Line, Word, And Byte Counts..................................................................62 grep ? Print Lines Matching A Pattern......................................................................62 head / tail ? Print First / Last Part Of Files................................................................63 tee ? Read From Stdin And Output To Stdout And Files..........................................64 Summing Up..................................................................................................................65

Linux Is About Imagination..................................................................................65

7 ? Seeing The World As The Shell Sees It..................................................67

Expansion......................................................................................................................67 Pathname Expansion...............................................................................................68 Pathname Expansion Of Hidden Files.................................................................69 Tilde Expansion........................................................................................................69 Arithmetic Expansion................................................................................................70 Brace Expansion......................................................................................................71 Parameter Expansion...............................................................................................72 Command Substitution.............................................................................................73

Quoting..........................................................................................................................74 Double Quotes..........................................................................................................75 Single Quotes...........................................................................................................76 Escaping Characters................................................................................................77 Backslash Escape Sequences............................................................................77

Summing Up..................................................................................................................78 Further Reading.............................................................................................................78

8 ? Advanced Keyboard Tricks.....................................................................79

Command Line Editing..................................................................................................79 Cursor Movement.....................................................................................................79

iii

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download