Using man Pages and Command Help
No one remembers every option of every command. What separates a confident Bash user from a beginner is knowing where to look. Linux and Bash include their own documentation, available offline in your terminal, and most questions can be answered in seconds without a web search. This lesson shows the four sources you will use most: help for Bash built-ins, --help for most other commands, man pages for the full manual, and type to find out what kind of command you are dealing with.
Understanding the Concept
Commands come from two places (the earlier lesson on built-in and external commands explained the difference), and each place documents itself differently:
| Kind of command | Where its documentation lives | How to read it |
|---|---|---|
Bash built-in, such as cd or pwd | Inside Bash itself | help cd |
External program, such as ls or grep | The program and its manual page | ls --help or man ls |
Shell keyword, such as if or for | Inside Bash itself | help if |
So the first question is always: what kind of command is this? The type command answers it.
Finding Out What a Command Is
The -t option of type prints one word describing the command:
type -t cd ls if
Output:
builtin
file
keyword
The word builtin means the command is part of Bash, file means it is a separate program on disk, and keyword means it is part of the shell's syntax. Without -t, type gives a longer answer:
type -a ls
Output (the paths depend on your system):
ls is /usr/bin/ls
ls is /bin/ls
The option -a lists every place where the name is found. A built-in command is documented by help, so when type -t says builtin or keyword, use help.
Help for Built-in Commands
The help command is itself a built-in. Given a command name, it prints that command's usage and a description. This is the beginning of the output for cd:
help cd
Output (first lines):
cd: cd [-L|[-P [-e]] [-@]] [dir]
Change the shell working directory.
Change the current directory to DIR. The default DIR is the value of the
HOME shell variable. If DIR is "-", it is converted to $OLDPWD.
The variable CDPATH defines the search path for the directory containing
DIR. Alternative directory names in CDPATH are separated by a colon (:).
The first line is the synopsis: the command followed by the options it accepts. In a synopsis, square brackets mean the part is optional, and a vertical bar separates alternatives. Here [-L|[-P [-e]] [-@]] says the options are all optional and that -L and -P are alternatives.
Three options of help give shorter or longer views:
help -d pwd
help -s pwd
help -m pwd
Output of the first two:
pwd - Print the name of the current working directory.
pwd: pwd [-LP]
The option -d prints a one-line description and -s prints only the synopsis. The option -m prints the full text in a manual-page layout, beginning with NAME, SYNOPSIS and DESCRIPTION sections. The command help help describes help itself, and plain help with no argument lists every built-in.
Shell keywords are documented the same way. Run help if or help for to see the syntax of those statements.
To see how many built-ins your Bash has, use compgen:
compgen -b | wc -l
Output (the count depends on the Bash version; Bash 5.2 printed this number):
61
Help for External Commands
Most external commands support a --help option that prints a short usage message. It is the quickest way to refresh your memory about an option name:
ls --help | head -6
Output:
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
Mandatory arguments to long options are mandatory for short options too.
-a, --all do not ignore entries starting with .
The | head -6 part keeps only the first six lines, because the full text is long. For longer help you can send the output to a pager: ls --help | less.
--help is a convention, not a rule. GNU tools such as ls and grep support it, and the same goes for many modern programs, but some commands use -h, -?, or help as a subcommand. Programs also commonly offer --version to print their version:
ls --version | head -1
Output:
ls (GNU coreutils) 9.4
If you give a command an option it does not know, it normally explains the mistake and points to its help:
ls --nosuchoption
echo "status=$?"
Output:
ls: unrecognized option '--nosuchoption'
Try 'ls --help' for more information.
status=2
The exit status 2 signals a usage error. The message is a useful hint: it tells you the exact command that will show the correct options.
Reading Manual Pages with man
A manual page, or man page, is the complete reference for a command. It is longer than --help and includes every option, explanations, examples, exit codes and related commands. Open one by giving man the name of the command:
man ls
The page opens in a pager, a program that shows a long text one screen at a time. On most systems the pager is less. The page does not print and exit; it stays open until you press q.
Note: some minimal systems, such as containers and cloud images, ship man without the manual pages themselves, and then man ls prints a message saying the system has been minimized. On a normal desktop or server install, the pages are present. If they are missing on yours, installing the package named man-db and the packages with the manual pages (on Ubuntu, manpages) restores them.
Moving Around a Page
Because the pager is less, you can use its keys:
| Key | Action |
|---|---|
Space or f | Scroll forward one screen |
b | Scroll back one screen |
↓ / ↑ | Scroll one line |
/text then Enter | Search forward for text |
n | Jump to the next search match |
N | Jump to the previous search match |
g | Go to the top |
G | Go to the bottom |
h | Show the pager's own help |
q | Quit |
Searching is the key skill. If you want to know how to sort ls output by size, open the page, type /size, press Enter, and press n until you reach the option you want.
How a Page Is Organized
Man pages follow a standard layout. You will almost always see these headings:
| Section | What it contains |
|---|---|
NAME | The command and a one-line description |
SYNOPSIS | The exact syntax, using the same bracket notation as help |
DESCRIPTION | What the command does, and usually the list of options |
OPTIONS | The options, if they are separate from the description |
EXIT STATUS | The meaning of each exit code |
EXAMPLES | Sample uses (not every page has this) |
SEE ALSO | Related commands and pages |
When reading a synopsis, remember the notation: text in square brackets is optional, an ellipsis (...) means the item can repeat, and a vertical bar separates choices. The synopsis ls [OPTION]... [FILE]... means that ls takes any number of options and any number of files, all optional.
Manual Sections
The manual is divided into numbered sections, and the same name can appear in more than one:
| Section | Contents |
|---|---|
| 1 | User commands and programs |
| 2 | System calls |
| 3 | Library functions |
| 5 | File formats and configuration files |
| 7 | Overviews and conventions |
| 8 | System administration commands |
The section number is written in parentheses after the name, as in ls(1). When two pages share a name, you choose one by putting the section first. For example, man 5 passwd opens the description of the /etc/passwd file, while man passwd opens the command that changes a password.
Searching When You Do Not Know the Name
Sometimes you know what you want to do but not which command does it. Two tools search the short descriptions of all man pages:
man -k "copy files"
apropos "copy files"
Both search the one-line descriptions and list the pages that match, each with its section number. The command whatis works the opposite way: given a command name, it prints its one-line description.
whatis ls
These tools depend on a prebuilt index that the system updates regularly. On a freshly installed system the index may be empty, and the search prints nothing until it is built.
Other Sources of Help
Some programs have richer documentation than a man page. The GNU project documents many tools in the info format, read with info coreutils. Bash also has a complete reference manual, which you can open with man bash. It is long, so use the search key. For example, open it and type /Parameter Expansion to find that section.
Many commands also ship examples in the directory /usr/share/doc. And the first step when a command gives an error is often the simplest one: read the message. As the ls example showed, many programs tell you exactly how to get more help.
Practical Applications
Checking what an unfamiliar command does before you run it:
whatis rsync
Finding the option that makes grep count matches, without leaving the terminal:
man grep
Then type /count, press Enter, and read the entry for -c.
Confirming that a command is available in a script before it is used:
type -t grep
If the command does not exist, type -t prints nothing and returns a non-zero exit status, so a script can test it. The later lessons on functions and error handling use this idea.
Picking the right help tool in a single line: use help when type -t says builtin or keyword, use --help for a quick reminder, and use man for the complete explanation.