Table Of ContentThe Linux Command Line
Second Internet Edition
William E. Shotts, Jr.
A LinuxCommand.org Book
Copyright ©2008-2013, William E. Shotts, Jr.
This work is licensed under the Creative Commons Attribution-Noncommercial-No De-
rivative 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 Fran-
cisco, 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 LinuxCommand.org project, a site for Linux education and advo-
cacy devoted to helping users of legacy operating systems migrate into the future. You
may contact the LinuxCommand.org project at http://linuxcommand.org.
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 elec-
tronic formats for most popular e-readers: http://nostarch.com/tlcl.htm
Release History
Version Date Description
13.07 July 6, 2013 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
Modifying Text...........................................................................................................80
Cutting And Pasting (Killing And Yanking) Text........................................................80
The Meta Key......................................................................................................81
Completion....................................................................................................................81
Programmable Completion..................................................................................83
Using History.................................................................................................................83
Searching History.....................................................................................................84
History Expansion.....................................................................................................86
script....................................................................................................................86
Summing Up..................................................................................................................86
Further Reading.............................................................................................................87
9 – Permissions..............................................................................................88
Owners, Group Members, And Everybody Else............................................................89
Reading, Writing, And Executing...................................................................................90
chmod – Change File Mode.....................................................................................92
What The Heck Is Octal?.....................................................................................93
Setting File Mode With The GUI...............................................................................95
umask – Set Default Permissions............................................................................96
Some Special Permissions..................................................................................98
Changing Identities........................................................................................................99
su – Run A Shell With Substitute User And Group IDs............................................99
sudo – Execute A Command As Another User.......................................................101
Ubuntu And sudo...............................................................................................101
chown – Change File Owner And Group................................................................102
chgrp – Change Group Ownership.........................................................................103
Exercising Our Privileges............................................................................................103
Changing Your Password............................................................................................106
Summing Up................................................................................................................107
Further Reading..........................................................................................................107
10 – Processes.............................................................................................108
How A Process Works.................................................................................................108
Viewing Processes......................................................................................................109
Viewing Processes Dynamically With top..............................................................111
Controlling Processes.................................................................................................113
Interrupting A Process............................................................................................114
Putting A Process In The Background....................................................................114
Returning A Process To The Foreground...............................................................115
Stopping (Pausing) A Process................................................................................116
Signals.........................................................................................................................117
Sending Signals To Processes With kill.................................................................117
Sending Signals To Multiple Processes With killall................................................120
More Process Related Commands.............................................................................120
Summing Up................................................................................................................121
Part 2 – Configuration And The Environment.............................123
11 – The Environment..................................................................................124
iv
What Is Stored In The Environment?..........................................................................124
Examining The Environment..................................................................................124
Some Interesting Variables.....................................................................................126
How Is The Environment Established?.......................................................................127
What's In A Startup File?........................................................................................128
Modifying The Environment.........................................................................................130
Which Files Should We Modify?.............................................................................130
Text Editors.............................................................................................................130
Using A Text Editor.................................................................................................131
Why Comments Are Important..........................................................................134
Activating Our Changes..........................................................................................135
Summing Up................................................................................................................135
Further Reading..........................................................................................................135
12 – A Gentle Introduction To vi.................................................................136
Why We Should Learn vi.............................................................................................136
A Little Background ....................................................................................................137
Starting And Stopping vi..............................................................................................137
Compatibility Mode............................................................................................138
Editing Modes..............................................................................................................139
Entering Insert Mode..............................................................................................140
Saving Our Work....................................................................................................140
Moving The Cursor Around.........................................................................................141
Basic Editing................................................................................................................142
Appending Text.......................................................................................................142
Opening A Line.......................................................................................................143
Deleting Text...........................................................................................................144
Cutting, Copying, And Pasting Text........................................................................145
Joining Lines...........................................................................................................147
Search-And-Replace...................................................................................................147
Searching Within A Line..........................................................................................147
Searching The Entire File.......................................................................................147
Global Search-And-Replace...................................................................................148
Editing Multiple Files...................................................................................................150
Switching Between Files.........................................................................................151
Opening Additional Files For Editing......................................................................151
Copying Content From One File Into Another........................................................152
Inserting An Entire File Into Another.......................................................................153
Saving Our Work.........................................................................................................154
Summing Up................................................................................................................155
Further Reading..........................................................................................................155
13 – Customizing The Prompt.....................................................................156
Anatomy Of A Prompt..................................................................................................156
Trying Some Alternative Prompt Designs....................................................................158
Adding Color................................................................................................................159
Terminal Confusion............................................................................................160
Moving The Cursor......................................................................................................162
Saving The Prompt......................................................................................................163
Summing Up................................................................................................................164
v
Further Reading..........................................................................................................164
Part 3 – Common Tasks And Essential Tools.............................165
14 – Package Management..........................................................................166
Packaging Systems.....................................................................................................166
How A Package System Works...................................................................................167
Package Files.........................................................................................................167
Repositories............................................................................................................167
Dependencies.........................................................................................................168
High And Low-level Package Tools........................................................................168
Common Package Management Tasks.......................................................................169
Finding A Package In A Repository........................................................................169
Installing A Package From A Repository.................................................................169
Installing A Package From A Package File.............................................................170
Removing A Package.............................................................................................170
Updating Packages From A Repository..................................................................171
Upgrading A Package From A Package File...........................................................171
Listing Installed Packages......................................................................................172
Determining If A Package Is Installed.....................................................................172
Displaying Info About An Installed Package...........................................................173
Finding Which Package Installed A File.................................................................173
Summing Up................................................................................................................173
The Linux Software Installation Myth.................................................................174
Further Reading..........................................................................................................175
15 – Storage Media.......................................................................................176
Mounting And Unmounting Storage Devices..............................................................176
Viewing A List Of Mounted File Systems................................................................178
Why Unmounting Is Important...........................................................................181
Determining Device Names....................................................................................182
Creating New File Systems.........................................................................................185
Manipulating Partitions With fdisk..........................................................................185
Creating A New File System With mkfs..................................................................188
Testing And Repairing File Systems............................................................................189
What The fsck?..................................................................................................189
Formatting Floppy Disks..............................................................................................189
Moving Data Directly To/From Devices.......................................................................190
Creating CD-ROM Images..........................................................................................191
Creating An Image Copy Of A CD-ROM.................................................................191
Creating An Image From A Collection Of Files.......................................................191
A Program By Any Other Name.........................................................................192
Writing CD-ROM Images.............................................................................................192
Mounting An ISO Image Directly............................................................................192
Blanking A Re-Writable CD-ROM...........................................................................193
Writing An Image....................................................................................................193
Summing Up................................................................................................................193
Further Reading..........................................................................................................193
Extra Credit..................................................................................................................193
vi
16 – Networking............................................................................................195
Examining And Monitoring A Network.........................................................................196
ping.........................................................................................................................196
traceroute...............................................................................................................197
netstat.....................................................................................................................198
Transporting Files Over A Network..............................................................................199
ftp............................................................................................................................199
lftp – A Better ftp.....................................................................................................202
wget........................................................................................................................202
Secure Communication With Remote Hosts...............................................................202
ssh..........................................................................................................................203
Tunneling With SSH..........................................................................................206
scp And sftp............................................................................................................207
An SSH Client For Windows?............................................................................208
Summing Up................................................................................................................208
Further Reading..........................................................................................................208
17 – Searching For Files..............................................................................209
locate – Find Files The Easy Way...............................................................................209
Where Does The locate Database Come From?..............................................211
find – Find Files The Hard Way...................................................................................211
Tests.......................................................................................................................212
Operators................................................................................................................214
Predefined Actions..................................................................................................217
User-Defined Actions..............................................................................................219
Improving Efficiency...............................................................................................220
xargs.......................................................................................................................220
Dealing With Funny Filenames..........................................................................221
A Return To The Playground..................................................................................221
Options...................................................................................................................224
Summing Up................................................................................................................225
Further Reading..........................................................................................................225
18 – Archiving And Backup.........................................................................226
Compressing Files.......................................................................................................226
gzip.........................................................................................................................227
bzip2.......................................................................................................................229
Don’t Be Compressive Compulsive...................................................................230
Archiving Files.............................................................................................................230
tar............................................................................................................................230
zip...........................................................................................................................236
Synchronizing Files And Directories............................................................................238
Using rsync Over A Network...................................................................................240
Summing Up................................................................................................................241
Further Reading..........................................................................................................241
19 – Regular Expressions...........................................................................243
What Are Regular Expressions?............................................................................243
grep.............................................................................................................................243
vii
Description:You've experienced the shiny, point-and-click surface of your Linux computer—now dive below and explore its depths with the power of the command line. 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