Linux Command Line · Lesson 4 of 43
Getting help with man, --help and type
Learn to get help for any Linux command using man pages, the --help flag, type, which, apropos and info, and how to read a manual page.
UNDERSTAND IT. THEN TRY IT.
What you can do after this lesson
No one remembers every option of every command, and nobody is expected to. Experienced users look things up many times a day. In this lesson you learn the fastest ways to do that, so that you can always teach yourself the next command.
--help: the quick answer
Most commands print a short summary when you add --help. It is fast and works inside the same window:
mkdir --help | head -n 6Usage: mkdir [OPTION]... DIRECTORY...
Create the DIRECTORY(ies), if they do not already exist.
Mandatory arguments to long options are mandatory for short options too.
-m, --mode=MODE set file mode (as in chmod), not a=rwx - umask
-p, --parents no error if existing, make parent directories as neededWe added | head -n 6 to show only the first lines. The pipe | is explained in a later lesson; for now, it limits the output so it fits on screen. On macOS, many commands print usage after you give them a wrong option instead of accepting --help.
man: the full manual
man (manual) shows the complete documentation of a command in a pager, a viewer that lets you scroll:
man lsInside the page:
- Space or f scrolls down a page, b scrolls up.
- /word searches forward; n goes to the next match, N to the previous.
- q quits and returns to the prompt.
Pages are divided into sections with the same names every time. Read these first:
- NAME: the command and a one-line description.
- SYNOPSIS: the shapes the command line can take.
- DESCRIPTION and OPTIONS: the details.
- EXAMPLES, when present, and SEE ALSO, which points to related commands.
Servers and containers sometimes do not install man pages to save space. In that case rely on --help, or read the manual online.
Reading a synopsis
The synopsis is a compact grammar. For example, the synopsis of cp includes lines like this:
cp [OPTION]... SOURCE... DIRECTORYRead it as: the command cp, then zero or more options (square brackets mean optional, the three dots mean repeatable), then one or more sources, then a directory. A vertical bar means "one or the other". Words in capitals are placeholders that you replace with real values.
type and which: what is this name?
When you type a name, the shell must decide what it refers to. type tells you:
type cd
type ls
type pwdcd is a shell builtin
ls is /usr/bin/ls
pwd is a shell builtinA builtin is part of the shell itself. A file result means a separate program found on disk, and the path shows where. An alias is a shortcut defined in your shell; you will create some later.
Use -a to list every match, and which to print only the path of a program:
which ls/usr/bin/lsThe exact path differs between distributions, and some systems show /bin/ls and others /usr/bin/ls. On many desktop and server installs ls and grep are also aliases that add colour in an interactive shell, so type ls may print a line such as ls is aliased to `ls --color=auto' instead. Both answers are normal; type -a ls shows every match. Builtins have their own help: help cd prints it.
apropos: finding a command by what it does
When you do not know the command name, search the one-line descriptions with apropos, or equivalently man -k:
apropos "copy files"This lists manual pages whose summary matches. It needs the manual database to be installed, so it may print nothing on minimal systems.
info and other sources
Some GNU tools have longer documentation in info, a hyperlinked format. Many projects also ship examples in /usr/share/doc. Online, the best sources are the project's own documentation, your distribution's wiki and community answers; always check that the command matches your distribution and version.
Common mistakes
Searching the web before reading the man page. Man pages match your installed version exactly, which online answers might not.
Getting stuck in a man page. Press q. Ctrl+C does not always work in a pager.
Assuming macOS and Linux options are identical. sed -i, date -d and ls --color behave differently on macOS. The manual of your own system is the truth.
Running a command you do not understand. Read what each option does first, especially with rm, chmod, dd and anything run with sudo.
Exercise
Run type on cd, echo and grep and note which are builtins. Run grep --help | head -n 15. If man pages are installed, open man ls, search for human-readable using /, then quit. Finally, use man -k or apropos to find a command that "sorts" something.
KNOWLEDGE CHECK
A quick check. You’ve got this.
Try each question. Every answer comes with an explanation.
Ready for the next small win?
Mark the lesson complete when you are ready.