First Things First

Welcome to Linux! If you’re a new user, this book can serve as a quick introduction, as well as a guide to common and practical commands. If you have Linux experience, feel free to skip the introductory material.

What’s in This Book?

This book is a short guide, not a comprehensive reference. I cover important, useful aspects of Linux so you can work productively. I do not, however, present every single command and every last option (my apologies if your favorite was omitted), nor delve into detail about operating system internals. Short, sweet, and essential—that’s our motto.

I focus on commands, those pesky little words you type on a command line to tell a Linux system what to do. Here’s an example command that counts lines of text in a file, myfile:

wc -l myfile

This book covers important Linux commands for most users, such as ls (list files), grep (search for text), mplayer (play audio and video files), and df (measure free disk space). I touch only briefly on graphical environments like GNOME and KDE Plasma, each of which could fill a Pocket Guide by itself.

I’ve organized the material by function to provide a concise learning path. For example, to help you view the contents of a file, I introduce many file-viewing commands together: cat for short text files, less for longer ones, od for binary files, and so on. Then I explain each command in turn, briefly presenting its common uses and options.

I assume you have access to a Linux system and can log in. If not, it’s easy to try out Linux on most computers. Just download and install a “live” Linux distribution onto a USB thumb drive and boot it. Examples are Ubuntu, Fedora, and KNOPPIX.

What’s New in the Fourth Edition?

New commands

I’ve added 50 new commands to this edition, such as git and svn for version control, split and column for text manipulation, pandoc and ffmpeg for file conversion, snap and flatpak for package management, mdadm, lvcreate, and zfs for fancy storage management, gpg for encryption, and many others.

Clearer organization

I’ve reorganized the book into chapters on concepts, files, basic system administration, networking, and other topics.

Goodbye, ancient commands

Some commands from previous editions of this book are mostly obsolete today, such as write and finger, or deprecated, such as ftp. I’ve replaced them with more relevant commands for modern Linux systems.

Conventions Used in This Book

Each command I present in this book begins with a standard heading. Figure P-1 shows the heading for ls, a command that lists the names and attributes of files. The heading demonstrates the command’s general usage in a simple format:

ls [options] [files]

which means you’d type “ls” followed, if you choose, by options and then filenames. Don’t type the square brackets “[” and “]”—they just indicate their contents are optional. Words in italics mean you have to fill in your own values, like names of actual files. If you see a vertical bar between options or arguments, perhaps grouped by parentheses:

(file | directory)

this indicates choice: you may supply either a filename or directory name as an argument.

Standard command heading
Figure P-1. Standard command heading

The standard heading shown in Figure P-1 also includes six properties of the command, printed in black (supported) or gray (unsupported):

stdin

The command reads by default from standard input (i.e., your keyboard). See “Input, Output, and Redirection”.

stdout

The command writes by default to standard output (i.e., your display). See “Input, Output, and Redirection”.

- file

A single-dash argument (-), when provided as an input filename, tells the command to read from standard input rather than a disk file. Likewise, if the dash is supplied as an output filename, the command writes to standard output. For example, the following wc command line reads the files myfile and myfile2, then standard input, then myfile3:

wc myfile myfile2 - myfile3
-- opt

A double-dash option (--) means “end of options”: any strings appearing later on the command line are not treated as options. A double dash is sometimes necessary to work with a filename that begins with a dash, which otherwise would be (mistakenly) treated as an option. For example, if you have a file named -dashfile, the command wc -dashfile fails because the string -dashfile is treated as an (invalid) option. Run wc -- -dashfile to indicate -dashfile is a filename. If a command does not support “--”, you can still work around the problem by prepending the current directory path “./” to the filename so the dash is no longer the first character:

wc ./-dashfile
--help

The option --help makes the command print a help message explaining proper usage, then exit.

--version

The option --version makes the command print its version information and exit.

Commands, Prompts, and Output

The Linux command line, or shell, prints a special symbol, called a prompt, when it’s waiting for a command. In this book, the prompt is a right-facing arrow:

Prompts come in all shapes and sizes, depending on how your shell is configured. Your prompt might be a dollar sign ($), a combination of your computer name, username, and various symbols (myhost:~smith$), or something else. Every prompt means the same thing: the shell is ready for your next command.

When I show a command line in this book, some parts are meant to be typed by the user, and other parts are not (like the prompt and the command’s output). I use boldface to identify the parts to type. Sometimes I add italic comments to explain what’s going on:

wc -l myfile      The command to type at the prompt
18 myfile           The output it produces

Your Friend, the echo Command

In many of my examples, I print information to the screen with the echo command, which I formally describe in “Screen Output”. echo is one of the simplest commands—it merely prints its arguments on standard output, once those arguments have been processed by the shell:

echo My dog has fleas
My dog has fleas
→ echo My name is $USER     The shell variable USER
My name is smith

Long Command Lines

Sometimes, a command is longer than the width of a page, so I split it onto multiple lines. A final backslash character means “continued on the next line”:

echo This is a long command that does not fit on \
  one line
This is a long command that does not fit on one line

If you enter one of my multiline commands in a running shell, feel free to break it up with backslashes as I did, or just type the whole command on one line without backslashes.

Keystrokes

I use certain symbols for keystrokes. The caret (^) means “hold down the Control key,” usually labeled Ctrl. For example, ^D (Ctrl D) means “hold down the Ctrl key and type D.” I also write ESC to mean “press and release the Escape key.” Keys like Enter and the space bar should be self-explanatory.

Downloading the Practice Files

I’ve created a collection of files to help you practice with Linux. Download and install them on any Linux machine, and you can run most of the example commands in this book verbatim. To download them for the first time, run the following commands.1 (Note that -O contains a capital O, not a zero.)

cdcurl -O https://linuxpocketguide.com/LPG4.tar.gztar -xf LPG4.tar.gz

The preceding commands create a directory named linuxpocketguide in your home directory. Visit this directory:

cd ~/linuxpocketguide

and run commands as you read the book. The output should match the book’s except for local details like dates and usernames.

To re-download and install the practice files (say, if you’ve modified them), simply run the provided reset-lpg script:

cd ~/linuxpocketguidebash reset-lpg

If you’ve placed the practice files in a different directory, supply it to reset-lpg. The following command creates or refreshes the directory /tmp/practice/linuxpocketguide:

bash reset-lpg /tmp/practice

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic

Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.

Constant width bold

Shows commands or other text that should be typed literally by the user.

Constant width italic

Shows text that should be replaced with user-supplied values or by values determined by context.

Tip

This element signifies a tip or suggestion.

Note

This element signifies a general note.

Warning

This element indicates a warning or caution.

O’Reilly Online Learning

Note

For more than 40 years, O’Reilly Media has provided technology and business training, knowledge, and insight to help companies succeed.

Our unique network of experts and innovators share their knowledge and expertise through books, articles, and our online learning platform. O’Reilly’s online learning platform gives you on-demand access to live training courses, in-depth learning paths, interactive coding environments, and a vast collection of text and video from O’Reilly and 200+ other publishers. For more information, visit https://oreilly.com.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at https://oreil.ly/linux-pocket-guide-4e.

For news and information about our books and courses, visit https://oreilly.com.

Find us on LinkedIn: https://linkedin.com/company/oreilly-media.

Watch us on YouTube: https://youtube.com/oreillymedia.

Acknowledgments

I am so grateful to the many readers who purchased the first three editions of this book over the past 20(!) years, making the fourth edition possible. My heartfelt thanks also go to my editor Virginia Wilson, acquisitions editor John Devins, the O’Reilly production team, my awesome technical reviewers (Abhishek Prakash, Dan Ritter, Doron Beit-Halahmi, Ethan Schwartz, and Jess Males), Maggie Johnson at Google, and Kerry and Lesley Minnear at Alucard Music. And all my love to my wonderful family, Lisa, Sophia, Kay, and Luna.

1 Or, if you are experienced with git and GitHub, download the files and skip the rest of my instructions. If you clone the repository and want to restore the files to their original state, don’t run the reset-lpg script; run git reset --hard instead.

Get Linux Pocket Guide, 4th Edition, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.