# 2.2 Using the Command Line to Get Help

### **2.2 Using the Command Line to Get Help**

**Weight:** 2

**Description:** Running help commands and navigation of the various help systems.

**Key Knowledge Areas:**

* Man pages
* Info pages

**The following is a partial list of the used files, terms and utilities:**

* man
* info
* /usr/share/doc/
* locate

Linux commands are essential for working with the operating system, but some can be difficult to remember, especially for beginners. This lesson will teach you how to use helpful tools and documentation to find information about Linux commands, making it easier to use and understand them.

### Ways to get help

Learning how to access help resources can enhance your problem-solving skills, benefiting your future learning and endeavors.

| Man Pages   | info               | less              | -h  or --help       |
| ----------- | ------------------ | ----------------- | ------------------- |
| Built in    | Less common        | for viewing files | Doesn't always work |
| Informative | Newer than **man** | /usr/share/doc    |                     |

## man

The `man` command in Linux is used to display the manual pages for other commands and utilities. It provides detailed documentation about the usage, options, and functionality of commands, making it an essential tool for both beginners and experienced users. Each manual page includes sections such as NAME, SYNOPSIS, DESCRIPTION, OPTIONS, and EXAMPLES, which help users understand and effectively use the command.

The `man` command is essentially the Linux manual reader. When you type `man` followed by a command name, it retrieves and displays the manual page for that command, offering comprehensive details on how to use it, including all available options and flags. This command is crucial for learning about the tools and commands available in the Linux operating system.

The basic syntax of the "man" command is as follows:

```
man [option] [command]
```

Here,

"**option**" refers to additional flags that modify the behavior of the "man" command,

"**command**" is the Linux command or utility for which you want to access the manual.

#### Man Command Options <a href="#exploring-command-options" id="exploring-command-options"></a>

The "man" command itself has options that enhance its functionality:

| **Option**     | **Description**                                              |
| -------------- | ------------------------------------------------------------ |
| man \[command] | Display the manual page for the specified command.           |
| -f, --whatis   | Display a concise one-line description of the command.       |
| -k, --apropos  | Search for commands related to a given keyword.              |
| -a, --all      | Display all matching manual pages for the specified command. |
| Spacebar       | Move forward one page in the manual.                         |
| Enter          | Move forward one line in the manual.                         |
| B              | Move backward one page in the manual.                        |
| Q              | Quit the manual viewer.                                      |

#### Accessing Manual Pages <a href="#accessing-manual-pages" id="accessing-manual-pages"></a>

to access the manual page for a specific command, simply type:

```
man [command]
```

**For example ,** To view the manual for the "**ls**" command execute the following command:

```
man ls
```

This will display detailed information about the "ls" command, including its usage, options, and examples.

```
LS(1)                           User Commands                           LS(1)

NAME
       ls - list directory contents

SYNOPSIS
       ls [OPTION]... [FILE]...

DESCRIPTION
       List  information  about the FILEs (the current directory by default).
       Sort entries alphabetically if none of -cftuvSUX nor --sort is  speci‐
       fied.

       Mandatory  arguments  to  long options are mandatory for short options
       too.

       -a, --all
              do not ignore entries starting with .

       -A, --almost-all
              do not list implied . and ..

       --author
              with -l, print the author of each file

       -b, --escape
              print C-style escapes for nongraphic characters

       --block-size=SIZE
              with  -l,  scale  sizes  by  SIZE  when  printing  them;  e.g.,
              '--block-size=M'; see SIZE format below

       -B, --ignore-backups
              do not list implied entries ending with ~

       -c     with  -lt:  sort by, and show, ctime (time of last modification
              of file status information); with -l: show ctime  and  sort  by
              name; otherwise: sort by ctime, newest first

       -C     list entries by columns

       --color[=WHEN]
              colorize the output; WHEN can be 'always' (default if omitted),
              'auto', or 'never'; more info below

       -d, --directory
              list directories themselves, not their contents

       -D, --dired
              generate output designed for Emacs' dired mode

       -f     do not sort, enable -aU, disable -ls --color

       -F, --classify
              append indicator (one of */=>@|) to entries

       --file-type
 Manual page ls(1) line 1 (press h for help or q to quit)
```

### Navigating Manual Pages <a href="#navigating-manual-pages" id="navigating-manual-pages"></a>

Once you've accessed a manual page, you can navigate through it using various commands. The common navigation keys include:

* **Spacebar:** Move forward one page.
* **Enter:** Move forward one line.
* **B:** Move backward one page.
* **Q:** Quit the manual viewer.

### Sections in Manual Pages <a href="#sections-in-manual-pages" id="sections-in-manual-pages"></a>

Manual pages are organized into different sections, each serving a specific purpose. The primary sections include:

* **NAME:** Provides the name and a brief description of the command.
* **SYNOPSIS:** Describes the syntax of the command.
* **DESCRIPTION:** Offers a detailed explanation of the command's functionality.
* **OPTIONS:** Lists the available command-line options and their descriptions.
* **EXAMPLES:** Provides practical examples demonstrating command usage.
* **SEE ALSO:** Suggests related commands or resources.

## info

**info** command reads documentation in the **info** format. It will give detailed information for a command when compared with the man page. The pages are made using the **Texinfo** **tools** which can link with other pages, create menus, and easy navigation.

syntax:

```
info [OPTION]... [MENU-ITEM...]
```

where,

* **'OPTIONS':** Modify the behavior of the command.
* **'MENU-ITEM':** Specify a particular section or node in the documentation to visit.

### **Key Options for the info command**

The **info command** supports several options that enhance its functionality. Below are the most useful ones:

| Option                        | Description                                     |
| ----------------------------- | ----------------------------------------------- |
| `-a, --all`                   | Use all matching manuals.                       |
| `-k, --apropos=STRING`        | Look up `STRING` in all indices of all manuals. |
| `-d, --directory=DIR`         | Add `DIR` to `INFOPATH`.                        |
| `-f, --file=MANUAL`           | Specify the Info manual to visit.               |
| `-h, --help`                  | Display help and exit.                          |
| `-n, --node=NODENAME`         | Specify nodes in the first visited Info file.   |
| `-o, --output=FILE`           | Output selected nodes to `FILE`.                |
| `-O, --show-options, --usage` | Go to the command-line options node.            |
| `-v, --variable VAR=VALUE`    | Assign `VALUE` to the Info variable `VAR`.      |
| `--version`                   | Display version information and exit.           |
| `-w, --where, --location`     | Print the physical location of the Info file.   |

example:

```
info ls
```

output:

```
Next: dir invocation,  Up: Directory listing

10.1 ‘ls’: List directory contents
==================================

The ‘ls’ program lists information about files (of any type, including
directories).  Options and file arguments can be intermixed arbitrarily,
as usual.

   For non-option command-line arguments that are directories, by
default ‘ls’ lists the contents of directories, not recursively, and
omitting files with names beginning with ‘.’.  For other non-option
arguments, by default ‘ls’ lists just the file name.  If no non-option
argument is specified, ‘ls’ operates on the current directory, acting as
if it had been invoked with a single argument of ‘.’.

   By default, the output is sorted alphabetically, according to the
locale settings in effect.(1)  If standard output is a terminal, the
output is in columns (sorted vertically) and control characters are
output as question marks; otherwise, the output is listed one per line
and control characters are output as-is.

   Because ‘ls’ is such a fundamental program, it has accumulated many
options over the years.  They are described in the subsections below;
within each section, options are listed alphabetically (ignoring case).
The division of options into the subsections is not absolute, since some
options affect more than one aspect of ‘ls’’s operation.

   Exit status:

     0 success
     1 minor problems  (e.g., failure to access a file or directory not
       specified as a command line argument.  This happens when listing a
       directory in which entries are actively being removed or renamed.)
     2 serious trouble (e.g., memory exhausted, invalid option, failure
       to access a file or directory specified as a command line argument
       or a directory loop)

   Also see *note Common options::.

* Menu:

* Which files are listed::
* What information is listed::
* Sorting the output::
* General output formatting::
* Formatting file timestamps::
* Formatting the file names::

   ---------- Footnotes ----------

   (1) If you use a non-POSIX locale (e.g., by setting ‘LC_ALL’ to
‘en_US’), then ‘ls’ may produce output that is sorted differently than
you’re accustomed to.  In that case, set the ‘LC_ALL’ environment
variable to ‘C’.

-----Info: (coreutils)ls invocation, 56 lines --All-----------------
```

## The /usr/share/doc directory

The <kbd>/usr/share/doc</kbd> directory is another excellent place to look for help in Linux. This directory has very intensive documentation; it doesn't just show you how to use a command; sometimes, it will even show the name and contact information of the authors who developed the command. Moreover, it may also include a <kbd>TODO</kbd> file that contains a list of unfinished tasks/features; contributors usually check the <kbd>TODO</kbd> files to help fix bugs and develop new features.

To demonstrate, let's go to the `grep` documentation directory:

```
[payam@earth ~]$ cd /usr/share/doc/
[payam@earth doc]$ cd grep
[payam@earth grep]$ ls
AUTHORS  NEWS  README  THANKS  TODO
```

How ever there are several ways to see the content of these files, let use `less` command.

## less

The less command in Linux is used to view the contents of a file one page at a time without opening it in an editor, making it ideal for reading large files efficiently.

* It allows you to scroll forward and backward through a file.
* It does not load the entire file into memory, making it faster for large files.
* You can search for specific text within the file using /pattern.
* It provides navigation shortcuts (e.g., Space for next page, b for previous page, q to quit).
* Commonly used to view log files, configuration files, or command outputs (cat filename | less or less filename)

**Syntax:**

```
less [options] filename
```

* Here, \`filename\` represents the name of the file we want to view using the \`less\` command.
* The less command provides several options that modify its behavior.&#x20;

### Commonly Used Options in\`less\`command <a href="#commonly-used-options-inlesscommand" id="commonly-used-options-inlesscommand"></a>

Here are the most commonly used and practical options of the `less` command in Linux:

| **Options** | **Description**                                                     |
| ----------- | ------------------------------------------------------------------- |
| -E          | Automatically exit when reaching the end of the file.               |
| -f          | Force non-regular files to be opened.                               |
| -F          | Exit if the entire file can be displayed on the first screen.       |
| -g          | Highlight the string that was found by the last search command.     |
| -G          | Suppress highlighting of search matches.                            |
| -i          | Ignore cases when searching.                                        |
| -n          | Suppress line numbers.                                              |
| -p pattern  | Start at the first occurrence of the specified pattern in the file. |
| -s          | Squeeze consecutive blank lines into a single line.                 |
| -q          | quit `less`                                                         |

### Navigation in less command:

| Command                            | Action                  |
| ---------------------------------- | ----------------------- |
| `Down arrow`, `Enter`, `e`, or `j` | Move forward one line.  |
| `Up arrow`,`y` or `k`              | Move backward one line. |
| `Space bar` or `f`                 | Move Forward one page.  |
| `b`                                | Move Backward one page. |

lets go back our example and see the content of grep command manusals:

```
[payam@earth grep]$ pwd
/usr/share/doc/grep
[payam@earth grep]$ ls
AUTHORS  NEWS  README  THANKS  TODO
[payam@earth grep]$ less README
```

output:

```
  Copyright (C) 1992, 1997-2002, 2004-2020 Free Software Foundation, Inc.

  Copying and distribution of this file, with or without modification,
  are permitted in any medium without royalty provided the copyright
  notice and this notice are preserved.

This is GNU grep, the "fastest grep in the west" (we hope).  All
bugs reported in previous releases have been fixed.  Many exciting new
bugs have probably been introduced in this revision.

GNU grep is provided "as is" with no warranty.  The exact terms
under which you may use and (re)distribute this program are detailed
in the GNU General Public License, in the file COPYING.

GNU grep is based on a fast lazy-state deterministic matcher (about
twice as fast as stock Unix egrep) hybridized with a Boyer-Moore-Gosper
search for a fixed string that eliminates impossible text from being
considered by the full regexp matcher without necessarily having to
look at every character.  The result is typically many times faster
than Unix grep or egrep.  (Regular expressions containing back-references
will run more slowly, however.)

See the files AUTHORS and THANKS for a list of authors and other contributors.

See the file INSTALL for compilation and installation instructions.
If there is no INSTALL file, this copy of the source code is intended
for expert hackers; please see the file README-hacking.

```

Cool! You can view the <kbd>AUTHORS</kbd> file to see the team of developers who contributed...

{% hint style="success" %}
the `less` command seems to magically detect a gzipped file and decompress it on the fly, so you can do:

```
less my_stuff.csv.gz
```

{% endhint %}

### Use the `--help` Option <a href="#use-the-help-option" id="use-the-help-option"></a>

Many Linux commands support a `--help` option that provides a quick overview of the command's usage. This is often the fastest way to get basic information about a command.

Try using the `--help` option with the which command:

```
which --help
```

Type this command and press Enter. You should see a summary of the `which` command's options and usage:

```
Usage: /usr/bin/which [options] [--] COMMAND [...]
Write the full path of COMMAND(s) to standard output.

  --version, -[vV] Print version and exit successfully.
  --help,          Print this help and exit successfully.
  --skip-dot       Skip directories in PATH that start with a dot.
  --skip-tilde     Skip directories in PATH that start with a tilde.
  --show-dot       Don't expand a dot to current directory in output.
  --show-tilde     Output a tilde for HOME directory for non-root.
  --tty-only       Stop processing options on the right if not on tty.
  --all, -a        Print all matches in PATH, not just the first
  --read-alias, -i Read list of aliases from stdin.
  --skip-alias     Ignore option --read-alias; don't read stdin.
  --read-functions Read shell functions from stdin.
  --skip-functions Ignore option --read-functions; don't read stdin.

Recommended use is to write the output of (alias; declare -f) to standard
input, so that which can show aliases and shell functions. See which(1) for
examples.

If the options --read-alias and/or --read-functions are specified then the
output can be a full alias or function definition, optionally followed by
the full path of each command used inside of those.

Report bugs to <which-bugs@gnu.org>.
```

{% hint style="success" %}

## command --help vs man command

Help is a built-in "usage" of the command, and not all commands implement it, or at least not the same way, however, `man` is a command by itself which is a pager program that reads manual. For more info issue the `man man` for the manual pages for the `man` command.

You should use man every time you need additional information about a command, as well with the `info` command, that reads info documents.
{% endhint %}

## locate

The locate command in Linux is a fast and efficient tool used to find files by their name. Unlike the find command, it searches through a pre-built database of file paths instead of scanning the entire filesystem, making searches much quicker.

* Used to search and locate files or directories by name.
* Works faster than the find command because it uses a pre-indexed database.
* The database is updated using the `updatedb` command.
* Newly created files do not appear until the database is refresh

Example : Search a File with Specific Name Using locate Command

```
locate myfile.txt
```

It will search for myfile.txt

That's all

.

.

.

***

source:\
<https://labex.io/tutorials/linux-get-help-on-linux-commands-18000>\
<https://www.geeksforgeeks.org/linux-unix/man-command-in-linux-with-examples/>\
<https://www.geeksforgeeks.org/linux-unix/info-command-in-linux-with-examples/>

<https://linuxize.com/post/less-command-in-linux/>

<https://subscription.packtpub.com/book/cloud-and-networking/9781800566002/5/ch05lvl1sec48/the-usr-share-doc-directory#:~:text=The%20%2Fusr%2Fshare%2Fdoc%20directory%20is%20another%20excellent%20place,authors%20who%20developed%20the%20command.>\
<https://www.geeksforgeeks.org/linux-unix/locate-command-in-linux-with-examples/><br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://borosan.gitbook.io/lpi-linux-essentials/2.2-using-the-command-line-to-get-help.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
