The Shell, the Terminal and the Kernel · about 30 min

Getting help: man, help and --help

Why this matters

Nobody memorizes the options of hundreds of commands. Experienced people are fast because they know where the answer is and how to read it in seconds. The documentation on a Linux system is complete, offline (useful in this sandbox, which has no network) and matches the exact version installed, which web answers often do not.

Three sources, one rule

There are three main places to look, and which one applies depends on the kind of command (remember type from lesson 2):

Kind of commandWhere its docs liveExample
bash builtininside bash: helphelp cd, help read
external programthe manual: manman ls, man tar
most GNU programsbuilt-in summary: --helpls --help, tail --help

The rule: when unsure, run type name first, then pick.

help: builtins

$ help cd
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. ...

help with no arguments lists every builtin, and help -d name gives a one-line description. You will use help heavily from module 7 on, for read, printf, declare, set, test and [[.

--help: the quick summary

Most GNU tools print a compact summary of their options with --help:

$ tail --help
Usage: tail [OPTION]... [FILE]...
Print the last 10 lines of each FILE to standard output.
...
  -n, --lines=[+]NUM       output the last NUM lines, instead of the last 10;
                             or use -n +NUM to skip NUM-1 lines at the start
...

It often scrolls off the screen. Send it through a pager: tail --help | less. (The | is a pipe; module 5 explains it. For now, read it as "feed the output into".)

man: the manual

man opens the manual page of a program in the pager less:

$ man tree

Inside less:

KeyAction
Space / bpage forward / back
Up / Down, j / kone line
/word then Entersearch forward for word; n next match, N previous
g / Gtop / bottom
hhelp for less
qquit

Searching is the real skill. Looking for how to sort by size in ls? Open man ls, type /size, press n until you find -S.

A man page has a standard layout:

Many pages are long; jump straight to EXAMPLES with /EXAMPLES if there is one.

Note: If man says No manual entry for ... in this sandbox, that page was not installed in the container image. Fall back to --help (for GNU tools) or help (for builtins).

SYNOPSIS notation

The synopsis uses a small grammar:

cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY

So the second line says: any number of options, then one or more sources, then exactly one directory at the end.

Sections

The manual is divided into numbered sections, because the same name can mean different things:

SectionContents
1user commands (ls, grep)
2system calls (fork, open): what programs ask the kernel
3C library functions (printf)
5file formats (passwd, crontab)
7overviews and conventions (hier, signal, regex, glob)
8administration commands (often need root)

man passwd shows section 1, the command. man 5 passwd shows the file format of /etc/passwd. Documentation writes this as passwd(5). Try man 7 hier now: it describes the directory layout you will explore in the next module. And for the curious, man 2 fork and man 2 execve describe the two system calls from lesson 2.

man -k word (the same as apropos word) searches the one-line descriptions of all pages, and whatis name prints just the NAME line. They rely on an index that may not be built in a minimal container, so do not worry if they find nothing here.

Reading documentation well

Common mistakes

Recap

Going further: read man 7 hier, then help help. Pick one command you used today and find its EXIT STATUS section.

Exercises

Exercise 1 · quiz

Where is the documentation?

You want the documentation for the cd command. cd is a bash builtin. Which command shows its documentation most directly?

Answer
  • help cd

Builtins are part of bash, so their documentation lives inside bash: help cd. On many systems man cd says there is no manual entry (or shows a generic POSIX page), and cd --help works in recent bash versions only because bash added it. For external programs it is the other way around: man ls or ls --help. Use type first when in doubt about which kind you have.

Exercise 2 · quiz

Reading a SYNOPSIS

A man page shows this synopsis:

cp [OPTION]... SOURCE... DIRECTORY

Which of these command lines fit it? Select all that apply.

Answer
  • cp a.txt b.txt backup/

  • cp -v a.txt backup/

  • cp -r -v src1 src2 src3 dest/

[OPTION]... means zero or more options; SOURCE... means one or more sources (no brackets, so at least one is required); DIRECTORY is exactly one and comes last. cp backup/ has no source at all, so it does not fit; cp would complain missing destination file operand.

Exercise 3 · quiz

Manual sections

/etc/passwd is both the name of a command (passwd, which changes a password) and of a file format. Which command shows the documentation of the file format of /etc/passwd? Type the whole command.

Answer

man 5 passwd. Section 5 holds file formats; plain man passwd finds section 1 (commands) first. When you see a name written like passwd(5) in documentation, the number in parentheses is the section. The sections you will use most: 1 commands, 5 file formats, 7 overviews and conventions (try man 7 hier for the filesystem layout), 8 administration commands.

Exercise 4 · task in a shell

Look an option up

The /work workspace contains a file numbers.txt with 50 lines. You want only its last 7 lines, and the tool for the end of a file is tail.

Use tail --help (or man tail) to find the option that sets the number of lines, then save the last 7 lines of numbers.txt into /work/last7.txt.

Hint 1

Run tail --help | less and look for an option about lines. Press q to quit less.

Hint 2

-n, --lines=[+]NUM: so tail -n 7 numbers.txt > last7.txt.

Reference solution
tail -n 7 numbers.txt > last7.txt