10 of the best ways to get help on Linux

There are many ways to get help while working on the Linux command line. Here are details on some of the most useful.

One large glowing question mark surrounded by many small question marks.
CarlosCastilla / Getty Images

Just because Linux appeals to the nerdiest of nerds doesn't mean that it can't be extremely helpful for those who don't want to spend a lot of time delving into the technical details of how to use various commands. In fact, Linux provides a series of tools that can help anyone master the command line or just get the task at hand done more quickly and efficiently. This post covers 10 of the best options.

man pages

You can always go to the man pages to answer usage and syntax questions you might have on a Linux command. Just type "man" followed by the name of the command (e.g., man ps), and you'll get a lot of descriptive information. 

On the other hand, if you really just want to see some examples of how to use a particular command, the content of a man page might be a lot more than you want to comb through. In the remainder of this post, I'll explain some other options for finding the command that you need and learning how to use it.

apropos

One of the easiest ways to find the command you need is to use the apropos command. It will fetch the primary description line from the man page of every command that includes the word that you are asking about. For example, if you're looking for a tool that allows you to select command output based on some pattern, you might do this:

shs@firefly:~$ apropos pattern
apt-patterns (7)     - Syntax and semantics of apt search patterns
awk (1)              - pattern scanning and text processing language
dh_installtex (1)    - register Type 1 fonts, hyphenation patterns, or formats with TeX
egrep (1)            - print lines that match patterns
fc-pattern (1)       - parse and show pattern
fgrep (1)            - print lines that match patterns
gpg-check-pattern (1) - Check a passphrase on stdin against the patternfile
grep (1)             - print lines that match patterns
magic (5)            - file command's magic pattern file
mawk (1)             - pattern scanning and text processing language
nawk (1)             - pattern scanning and text processing language
patgen (1)           - generate patterns for TeX hyphenation
pcrepattern (3)      - Perl-compatible regular expressions
ptargrep (1)         - Apply pattern matching to the contents of files in a tar archive
rgrep (1)            - print lines that match patterns
Text::Glob (3pm)     - match globbing patterns against text
zipgrep (1)          - search files in a ZIP archive for lines matching a pattern

The output clearly shows a lot of commands – likely some that you've never used before and others unrelated to what you are hoping to do. Based on the descriptions shown, though, you should be able to find one that does just what you need.

man -k

As it turns out, the man -k command will provide the same output as apropos. You can use whichever you prefer or make your search even easier by setting up an alias like this:

$ alias ?="apropos"

With this alias, you would be able to type "? pattern" to get the output shown above and you won't have to spell "apropos" or remember the -k option with the man command.

command -h and command --help

Some Linux commands provide a brief usage summary when you use the -h option. Some will generate a usage statement because they don't offer this option and so assume you don't quite know what you're doing when you use it, but even usage statements can be helpful. Others simply recommend using the --help option instead.

shs@firefly:~$ pwd -h
-bash: pwd: -h: invalid option
pwd: usage: pwd [-LP]                      <== usage statement
shs@firefly:~$ mkdir -h
mkdir: invalid option -- 'h'
Try 'mkdir --help' for more information.   <== suggestion

whatis

The whatis command can be used to provide a very brief description of whatever command you provide as an argument, sometimes confirming the command is what you were expecting.

shs@firefly:~$ whatis grep
grep (1) - print lines that match patterns
shs@firefly:~$ whatis useradd
useradd (8) - create a new user or update default new user information

help

The help command sounds very useful and it is, but it only provides help on bash built-ins. If you ask about other things, you're likely to get a response like this:

$ help mkdir
-bash: help: no help topics match `mkdir'.  Try `help help' or `man -k mkdir' or `info mkdir'.

At least it points you in the right direction. For bash builtins, however, the output will likely be far more useful!

shs@firefly:~$ help while
while: while COMMANDS; do COMMANDS; done          <== syntax
    Execute commands as long as a test succeeds.

    Expand and execute COMMANDS as long as the final command in the
    `while' COMMANDS has an exit status of zero.

    Exit Status:
    Returns the status of the last command executed.

info

The info command is different from the other help commands described. It displays a lot of information – often more than the man pages – on commands that you ask about. The contents are also navigable. That is, you can click on an underlined "link" in the displayed text and move into a different section within the information provided.

The info content is derived from files in the /usr/share/info directory. The files are unzipped as you use the info command. If this directory does not include a file relevant to what you are asking about, info will show you the man page instead.

aliases

Aliases will not provide information on how to use commands, but can be a great boon to remembering them – especially those that are complex or require a string of options to do what you want. Here are some examples that I use to avoid command complexity:

alias dirsBySize='du -kx | egrep -v "\./.+/" | sort -n'
alias myip='hostname -I | awk '\''{print }'\'''
alias oct2dec='f(){ echo "obase=10; ibase=8; $1" | bc; unset -f f; }; f'
alias recent='ls -ltr | tail -5'
alias rel='lsb_release -r'
alias side-by-side='pr -mt '

cheat

There's a very useful snap called "cheat" that can be used to print a cheat sheet for a particular command, It will contain a lot of useful examples of how to use the command. You do, however, have to be using a system that supports snaps (distribution-neutral packages) and install cheat.

Here's a truncated example of what you might see:

shs@firefly:~$ cheat grep
# To search a file for a pattern:
grep <pattern> <file>

# To perform a case-insensitive search (with line numbers):
grep -in <pattern> <file>

# To recursively grep for string <pattern> in <dir>:
grep -R <pattern> <dir>

# Read search patterns from a file (one per line):
grep -f <pattern-file> <<file>

# Find lines NOT containing pattern:
grep -v <pattern> <file>

# To grep with regular expressions:
grep "^00" <file>                                               # Match lines starting with 00
grep -E "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" <file> # Find IP add
…

cheat sheets

You can also locate and use a prepared Linux cheat sheet, whether you print it and keep it on your desktop or download a PDF that you can open when needed. It's hard to know all of the commands available on Linux or all of the options available with any particular command. Good cheat sheets can save you a lot of trouble by providing common usage examples.

Join the Network World communities on Facebook and LinkedIn to comment on topics that are top of mind.
Related:

Copyright © 2021 IDG Communications, Inc.

IT Salary Survey 2021: The results are in