Cover
Copyright
Contents
Preface
1: Welcome to Linux and Macos
The History of UNIX and GNU–Linux
The Heritage of Linux: UNIX
Fade to 1983
Next Scene, 1991
The Code Is Free
Have Fun!
What Is So Good About Linux?
Why Linux Is Popular with Hardware Companies and Developers
Linux Is Portable
The C Programming Language
Overview of Linux
Linux Has a Kernel Programming Interface
Linux Can Support Many Users
Linux Can Run Many Tasks
Linux Provides a Secure Hierarchical Filesystem
The Shell: Command Interpreter and Programming Language
A Large Collection of Useful Utilities
Interprocess Communication
System Administration
Additional Features of Linux
GUIs: Graphical User Interfaces
(Inter)Networking Utilities
Software Development
Chapter Summary
Exercises
Part I: The Linux and MacOS Operating Systems
2: Getting Started
Conventions Used in This Book
Logging In from a Terminal (Emulator)
Working from the Command Line
Which Shell Are You Running?
Correcting Mistakes
Repeating/Editing Command Lines
su/sudo: Curbing Your Power (root Privileges)
Where to Find Documentation
man: Displays the System Manual
apropos: Searches for a Keyword
info: Displays Information About Utilities
The ––help Option
The bash help Command
Getting Help
More About Logging In and Passwords
What to Do If You Cannot Log In
Logging In Remotely: Terminal Emulators, ssh, and Dial-Up Connections
Using Virtual Consoles
Logging Out
Changing Your Password
Chapter Summary
Exercises
Advanced Exercises
3: The Utilities
Special Characters
Basic Utilities
ls: Lists the Names of Files
cat: Displays a Text File
rm: Deletes a File
less Is more: Display a Text File One Screen at a Time
hostname: Displays the System Name
Working with Files
cp: Copies a File
mv: Changes the Name of a File
lpr: Prints a File
grep: Searches for a String
head: Displays the Beginning of a File
tail: Displays the End of a File
sort: Displays a File in Order
uniq: Removes Duplicate Lines from a File
diff: Compares Two Files
file: Identifies the Contents of a File
|(Pipeline): Communicates Between Processes
Four More Utilities
echo: Displays Text
date: Displays the Time and Date
script: Records a Shell Session
unix2dos: Converts Linux Files to Windows and macOS Format
Compressing and Archiving Files
bzip2: Compresses a File
bzcat and bunzip2: Decompress a File
gzip: Compresses a File
tar: Packs and Unpacks Archives
Locating Utilities
which and whereis: Locate a Utility
locate: Searches for a File
Displaying User and System Information
who: Lists Users on the System
finger: Lists Users on the System
uptime: Displays System Load and Duration Information
w: Lists Users on the System
free: Displays Memory Usage Information
Communicating with Other Users
write: Sends a Message
mesg: Denies or Accepts Messages
Email
Chapter Summary
Exercises
Advanced Exercises
4: The Filesystem
The Hierarchical Filesystem
Directory Files and Ordinary Files
Filenames
The Working Directory
Your Home Directory
Pathnames
Absolute Pathnames
Relative Pathnames
Working with Directories
mkdir: Creates a Directory
cd: Changes to Another Working Directory
rmdir: Deletes a Directory
Using Pathnames
mv, cp: Move or Copy Files
mv: Moves a Directory
Important Standard Directories and Files
Access Permissions
ls –l: Displays Permissions
chmod: Changes Access Permissions
Setuid and Setgid Permissions
Directory Access Permissions
ACLs: Access Control Lists
Enabling ACLs
Working with Access Rules
Setting Default Rules for a Directory
Links
Hard Links
Symbolic Links
rm: Removes a Link
Dereferencing Symbolic Links
Chapter Summary
Exercises
Advanced Exercises
5: The Shell
Special Characters
Ordinary Files and Directory Files
The Working Directory
Your Home Directory
The Command Line
A Simple Command
Syntax
Simple Commands
Processing the Command Line
Executing a Command
Editing the Command Line
Standard Input and Standard Output
The Screen as a File
The Keyboard and Screen as Standard Input and Standard Output
Redirection
Pipelines
Lists
Running a Command in the Background
Filename Generation/Pathname Expansion
The? Special Character
The * Special Character
The [] Special Characters
Builtins
Chapter Summary
Utilities and Builtins Introduced in This Chapter
Exercises
Advanced Exercises
Part II: The Editors
6: The vim Editor
History
Tutorial: Using vim to Create and Edit a File
Starting vim
Command and Input Modes
Entering Text
Getting Help
Ending the Editing Session
The compatible Parameter
Introduction to vim Features
Online Help
Terminology
Modes of Operation
The Display
Correcting Text as You Insert It
Work Buffer
Line Length and File Size
Windows
File Locks
Abnormal Termination of an Editing Session
Recovering Text After a Crash
Command Mode: Moving the Cursor
Moving the Cursor by Characters
Moving the Cursor to a Specific Character
Moving the Cursor by Words
Moving the Cursor by Lines
Moving the Cursor by Sentences and Paragraphs
Moving the Cursor Within the Screen
Viewing Different Parts of the Work Buffer
Input Mode
Inserting Text
Appending Text
Opening a Line for Text
Replacing Text
Quoting Special Characters in Input Mode
Command Mode: Deleting and Changing Text
Undoing Changes
Deleting Characters
Deleting Text
Changing Text
Replacing Text
Changing Case
Searching and Substituting
Searching for a Character
Searching for a String
Substituting One String for Another
Miscellaneous Commands
Join
Status
. (Period)
Copying, Moving, and Deleting Text
The General-Purpose Buffer
Named Buffers
Numbered Buffers
Reading and Writing Files
Reading Files
Writing Files
Identifying the Current File
Setting Parameters
Setting Parameters from Within vim
Setting Parameters in a Startup File
The .vimrc Startup File
Parameters
Advanced Editing Techniques
Using Markers
Editing Other Files
Macros and Shortcuts
Executing Shell Commands from Within vim
Units of Measure
Character
Word
Blank-Delimited Word
Line
Sentence
Paragraph
Screen (Window)
Repeat Factor
Chapter Summary
Exercises
Advanced Exercises
7: The emacs Editor
History
Evolution
emacs Versus vim
Tutorial: Getting Started with emacs
Starting emacs
Exiting
Inserting Text
Deleting Characters
Moving the Cursor
Editing at the Cursor Position
Saving and Retrieving the Buffer
Basic Editing Commands
Keys: Notation and Use
Key Sequences and Commands
META-x: Running a Command Without a Key Binding
Numeric Arguments
Point and the Cursor
Scrolling Through a Buffer
Erasing Text
Searching for Text
Using the Menubar from the Keyboard
Online Help
Advanced Editing
Undoing Changes
Point, Mark, and Region
Cut and Paste: Yanking Killed Text
Inserting Special Characters
Global Buffer Commands
Visiting and Saving Files
Buffers
Windows
Foreground Shell Commands
Background Shell Commands
Major Modes: Language-Sensitive Editing
Selecting a Major Mode
Human-Language Modes
C Mode
Customizing Indention
Comments
Special-Purpose Modes
Customizing emacs
The .emacs Startup File
Remapping Keys
A Sample .emacs File
More Information
Access to emacs
Chapter Summary
Exercises
Advanced Exercises
Part III: The Shells
8: The Bourne Again Shell (bash)
Background
Startup Files
Login Shells
Interactive Nonlogin Shells
Noninteractive Shells
Setting Up Startup Files
. (Dot) or source: Runs a Startup File in the Current Shell
Commands That Are Symbols
Redirecting Standard Error
Writing and Executing a Simple Shell Script
chmod: Makes a File Executable
#! Specifies a Shell
# Begins a Comment
Executing a Shell Script
Control Operators: Separate and Group Commands
; and NEWLINE Separate Commands
| and & Separate Commands and Do Something Else
&& and || Boolean Control Operators
() Groups Commands
\ Continues a Command
Job Control
jobs: Lists Jobs
fg: Brings a Job to the Foreground
Suspending a Job
bg: Sends a Job to the Background
Manipulating the Directory Stack
dirs: Displays the Stack
pushd: Pushes a Directory on the Stack
popd: Pops a Directory Off the Stack
Parameters and Variables
User-Created Variables
Variable Attributes
Keyword Variables
Special Characters
Locale
Time
Processes
Process Structure
Process Identification
Executing a Command
History
Variables That Control History
Reexecuting and Editing Commands
The Readline Library
Aliases
Single Versus Double Quotation Marks in Aliases
Examples of Aliases
Functions
Controlling bash: Features and Options
bash Command-Line Options
Shell Features
Processing the Command Line
History Expansion
Alias Substitution
Parsing and Scanning the Command Line
Command-Line Expansion
Chapter Summary
Exercises
Advanced Exercises
9: The TC Shell (tcsh)
Shell Scripts
Entering and Leaving the TC Shell
Startup Files
Features Common to the Bourne Again and TC Shells
Command-Line Expansion (Substitution)
Job Control
Filename Substitution
Manipulating the Directory Stack
Command Substitution
Redirecting Standard Error
Working with the Command Line
Word Completion
Editing the Command Line
Correcting Spelling
Variables
Variable Substitution
String Variables
Arrays of String Variables
Numeric Variables
Braces
Special Variable Forms
tcsh Variables
Control Structures
if
goto
Interrupt Handling
if...then...else
foreach
while
break and continue
switch
Builtins
Chapter Summary
Exercises
Advanced Exercises
Part IV: Programming Tools
10: Programming the Bourne Again Shell (bash)
Control Structures
if...then
if...then...else
if...then...elif
for...in
for
while
until
break and continue
case
select
Here Document
File Descriptors
Opening a File Descriptor
Duplicating a File Descriptor
File Descriptor Examples
Determining Whether a File Descriptor Is Associated with the Terminal
Parameters
Positional Parameters
Special Parameters
Variables
Shell Variables
Environment, Environment Variables, and Inheritance
Expanding Null and Unset Variables
Array Variables
Variables in Functions
Builtin Commands
type: Displays Information About a Command
read: Accepts User Input
exec: Executes a Command or Redirects File Descriptors
trap: Catches a Signal
kill: Aborts a Process
eval: Scans, Evaluates, and Executes a Command Line
getopts: Parses Options
A Partial List of Builtins
Expressions
Arithmetic Evaluation
Logical Evaluation (Conditional Expressions)
String Pattern Matching
Arithmetic Operators
Implicit Command-Line Continuation
Shell Programs
A Recursive Shell Script
The quiz Shell Script
Chapter Summary
Exercises
Advanced Exercises
11: The Perl Scripting Language
Introduction to Perl
More Information
Help
perldoc
Terminology
Running a Perl Program
Syntax
Variables
Scalar Variables
Array Variables
Hash Variables
Control Structures
if/unless
if...else
if...elsif...else
foreach/for
last and next
while/until
Working with Files
Sort
Subroutines
Regular Expressions
Syntax and the =~ Operator
CPAN Modules
Examples
Chapter Summary
Exercises
Advanced Exercises
12: The Python Programming Language
Introduction
Invoking Python
More Information
Writing to Standard Output and Reading from Standard Input
Functions and Methods
Scalar Variables, Lists, and Dictionaries
Scalar Variables
Lists
Dictionaries
Control Structures
if
if...else
if...elif...else
while
for
Reading from and Writing to Files
File Input and Output
Exception Handling
Pickle
Regular Expressions
Defining a Function
Using Libraries
Standard Library
Nonstandard Libraries
SciPy and NumPy Libraries
Namespace
Importing a Module
Example of Importing a Function
Lambda Functions
List Comprehensions
Chapter Summary
Exercises
Advanced Exercises
13: The MariaDB SQL Database Management System
History
Notes
Syntax and Conventions
More Information
Installing a MariaDB Server and Client
Fedora/RHEL (Red Hat Enterprise Linux)
Debian/Ubuntu/Mint
openSUSE
macOS
Client Options
Setting Up MariaDB
Assigning a Password to the MariaDB User Named root
Removing Anonymous Users
Running the Secure Installation Script
~/.my.cnf: Configures a MariaDB Client
~/.mysql_history: Stores Your MariaDB History
Creating a Database
Adding a User
Examples
Logging In
Creating a Table
Adding Data
Retrieving Data
Backing Up a Database
Modifying Data
Creating a Second Table
Joins
Chapter Summary
Exercises
14: The AWK Pattern Processing Language
Syntax
Arguments
Options
Notes
Language Basics
Patterns
Actions
Comments
Variables
Functions
Arithmetic Operators
Associative Arrays
printf
Control Structures
Examples
Advanced gawk Programming
getline: Controlling Input
Coprocess: Two-Way I/O
Getting Input from a Network
Chapter Summary
Exercises
Advanced Exercises
15: The sed Editor
Syntax
Arguments
Options
Editor Basics
Addresses
Instructions
Control Structures
The Hold Space
Examples
Chapter Summary
Exercises
Part V: Secure Network Utilities
16: The rsync Secure Copy Utility
Syntax
Arguments
Options
Notes
More Information
Examples
Using a Trailing Slash (/) on source-file
Removing Files
Copying Files to and from a Remote System
Mirroring a Directory
Making Backups
Restoring a File
Chapter Summary
Exercises
17: The OpenSSH Secure Communication Utilities
Introduction to OpenSSH
Files
More Information
Running the ssh, scp, and sftp OpenSSH Clients
Prerequisites
JumpStart I: Using ssh and scp to Connect to an OpenSSH Server
Configuring OpenSSH Clients
ssh: Logs in or Executes Commands on a Remote System
scp: Copies Files to and from a Remote System
sftp: A Secure FTP Client
~/.ssh/config and /etc/ssh/ssh_config Configuration Files
Setting Up an OpenSSH Server (sshd)
Prerequisites
Note
JumpStart II: Starting an OpenSSH Server
Authorized Keys: Automatic Login
Randomart Image
ssh-agent: Holds Your Private Keys
Command-Line Options
/etc/ssh/sshd_config Configuration File
Troubleshooting
Tunneling/Port Forwarding
Forwarding X11
Port Forwarding
Chapter Summary
Exercises
Advanced Exercises
Part VI: Command Reference
Utilities That Display and Manipulate Files
Network Utilities
Utilities That Display and Alter Status
Utilities That Are Programming Tools
Miscellaneous Utilities
Standard Multiplicative Suffixes
Common Options
The Sample Utility
sample: Brief description of what the utility does macOS
aspell: Checks a file for spelling errors
at: Executes commands at a specified time
busybox: Implements many standard utilities
bzip2: Compresses or decompresses files
cal: Displays a calendar
cat: Joins and displays files
cd: Changes to another working directory
chgrp: Changes the group associated with a file
chmod: Changes the access mode (permissions) of a file
chown: Changes the owner of a file and/or the group the file is associated with
cmp: Compares two files
comm: Compares sorted files
configure: Configures source code automatically
cp: Copies files
cpio: Creates an archive, restores files from an archive, or copies a directory hierarchy
crontab: Maintains crontab files
cut: Selects characters or fields from input lines
date: Displays or sets the system time and date
dd: Converts and copies a file
df: Displays disk space usage
diff: Displays the differences between two text files
diskutil: Checks, modifies, and repairs local volumes macOS
ditto: Copies files and creates and unpacks archives macOS
dmesg: Displays kernel messages
dscl: Displays and manages Directory Service information macOS
du: Displays information on disk usage by directory hierarchy and/or file
echo: Displays a message
expand/unexpand: Converts TABs to SPACEs and SPACEs to TABs
expr: Evaluates an expression
file: Displays the classification of a file
find: Finds files based on criteria
finger: Displays information about users
fmt: Formats text very simply
fsck: Checks and repairs a filesystem
ftp: Transfers files over a network
gawk: Searches for and processes patterns in a file
gcc: Compiles C and C++ programs
GetFileInfo: Displays file attributes macOS
grep: Searches for a pattern in files
gzip: Compresses or decompresses files
head: Displays the beginning of a file
join: Joins lines from two files based on a common field
kill: Terminates a process by PID
killall: Terminates a process by name
launchctl: Controls the launchd daemon macOS
less: Displays text files, one screen at a time
ln: Makes a link to a file
lpr: Sends files to printers
ls: Displays information about one or more files
make: Keeps a set of programs current
man: Displays documentation for utilities
mc: Manages files in a textual environment (aka Midnight Commander)
mkdir: Creates a directory
mkfs: Creates a filesystem on a device
mv: Renames or moves a file
nice: Changes the priority of a command
nl: Numbers lines from a file
nohup: Runs a command that keeps running after you log out
od: Dumps the contents of a file
open: Opens files, directories, and URLs macOS
otool: Displays object, library, and executable files macOS
paste: Joins corresponding lines from files
pax: Creates an archive, restores files from an archive, or copies a directory hierarchy
plutil: Manipulates property list files macOS
pr: Paginates files for printing
printf: Formats string and numeric data
ps: Displays process status
renice: Changes the priority of a process
rm: Removes a file (deletes a link)
rmdir: Removes directories
rsync: Securely copies files and directory hierarchies over a network
scp: Securely copies one or more files to or from a remote system
screen: Manages several textual windows
sed: Edits a file noninteractively
SetFile: Sets file attributes macOS
sleep: Creates: a process that sleeps for a specified interval
sort: Sorts and/or merges files
split: Divides a file into sections
ssh: Securely runs a program or opens a shell on a remote system
sshfs/curlftpfs: Mounts a directory on an OpenSSH or FTP server as a local directory
stat: Displays information about files
strings: Displays strings of printable characters from files
stty: Displays or sets terminal parameters
sysctl: Displays and alters kernel variables at runtime
tail: Displays the last part (tail) of a file
tar: Stores or retrieves files to/from an archive file
tee: Copies standard input to standard output and one or more files
telnet: Connects to a remote computer over a network
test: Evaluates an expression
top: Dynamically displays process status
touch: Creates a file or changes a file's access and/or modification time
tr: Replaces specified characters
tty: Displays the terminal pathname
tune2fs: Changes parameters on an ext2, ext3, or ext4 filesystem
umask: Specifies the file-creation permissions mask
uniq: Displays unique lines from a file
w: Displays information about local system users
wc: Displays the number of lines, words, and bytes in one or more files
which: Shows where in PATH a utility is located
who: Displays information about logged-in users
xargs: Converts standard input to command lines
Part VII: Appendixes
Appendix A: Regular Expressions
Characters
Delimiters
Simple Strings
Special Characters
Periods
Brackets
Asterisks
Carets and Dollar Signs
Quoting Special Characters
Rules
Longest Match Possible
Empty Regular Expressions
Bracketing Expressions
The Replacement String
Ampersand
Quoted Digit
Extended Regular Expressions
Appendix Summary
Appendix B: Help
Solving a Problem
Finding Linux and macOS Related Information
Mailing Lists
Specifying a Terminal
Appendix C: Keeping the System Up-To-Date
Using dnf
Using dnf to Install, Remove, and Update Packages
Other dnf Commands
dnf Groups
Downloading rpm Package Files Using dnf download
Configuring dnf
Using apt-get
Using apt-get to Install, Remove, and Update Packages
Using apt-get to Upgrade the System
Other apt-get Commands
Using apt Commands
Repositories
sources.list: Specifies Repositories for apt-get to Search
BitTorrent
Appendix D: MacOS Notes
Open Directory
Filesystems
Nondisk Filesystems
Case Sensitivity
/Volumes
Extended Attributes
File Forks
File Attributes
ACLs
Activating the Terminal META Key
Startup Files
Remote Logins
Many Utilities Do Not Respect Apple Human Interface Guidelines
Installing Xcode and MacPorts
macOS Implementation of Linux Features
Glossary
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
File Tree Index
Utility Index
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
R
S
T
U
V
W
X
Y
Z
Main Index
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z