Mastering the Linux Terminal / Command-Line
Tags: linux, commandline, tutorial, terminal
Published: 2024-04-08 14:37:35

Using the terminal is something any developer will eventually need. It is part of my daily routine to perform almost all of my development activities in a terminal, mastering this skill is something that will certainly boost your productivity and make you learn how to use any command-line interface (CLI) faster.

# How to read a Manual

When you wish to read the manual or help section of any command-line, it is pretty straightforward, just run: `man <command>` or `<command> --help` and it should give you some instructions on how to do it, right? But do you know how to read these manuals properly?

For instance, by running:

```
man git
```

It will give you this man page.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l8nvnasqjvhxc9jlq7po.png)

You can navigate it by using the arrow keys and quit using `q`

Do you understand what this section means with its symbols and conventions?

```
git [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]
    [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
    [-p|--paginate|-P|--no-pager] [--no-replace-objects] [--bare]
    [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
    [--super-prefix=<path>] [--config-env=<name>=<envvar>]
    <command> [<args>]
```

## Alternatives

Let's analyze the part:

```
git [-v | --version]
```

The pipe (`|`) character means that you can use one option or the other. This can mean that both option would either be mutually exclusive (or would not make sense)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j8cda3n6sbmmmxcwv1nk.png)

## Optional and Required Arguments

The brackets inside the brackets (`[]`) means the argument is optional, so you are not required to insert any of them unless you want to explicitly use them.

```
git [-v | --version] [...] <command> [<args>]
      │                │                │
      │                │                │
      └────────────────┴────────────────┴─ All Optional
```

Arguments inside lesser and greater than symbols `<>` are always required.

```
git [-v | --version] [...] <command> [<args>]
                               │
                               │
                            Required
```

This shows us that the only mandatory argument when using the `git` command is an argument named `<command>` that should be positioned before all of these optional arguments, and after this, there are some optional arguments that can be set after it.

```bash
# Simplified documentation
git <command> [<args>]
```

## Conventions

There are some argument conventions that most command-line utilities use and can make your life easier when understanding command-line usage.

### Short Options

Several command-line offer short options for common used arguments, for instance the `ls` command:

```
-a, --all
       do not ignore entries starting with.
-l     use a long listing format
-h, --human-readable
       with -l and -s, print sizes like 1K 234M 2G etc.
```

The short options are usually prefixed by a single hyphen (`-`) (e.g.: `-a`), long options are usually prefixed by double hyphen `--` (e.g.: `--all`).

### Combining arguments

Some command-line software support combining short options by simply putting everything together, for instance, these two commands produce the same output:

```
ls -lah
ls -l -a -h
```

### Arguments with Parameters

Some command-line software support arguments with parameters, one common convention is to be able to use both of them indicated by space or by `=` sign.

```
# These are the same
git commit -a --author="Name <email@email.com>"
git commit -a --author "Name <email@email.com>"
```

Sometimes a parameter can appear immediately after the option:

```
grep "Text to Find" * -R -n2
                          │
                          └─ Shows two lines before and after the found text
```

## Navigating the man pages

When navigating the manual or help pages or a particular command, it is common that they are presented in the `less` command, in which you can navigate the manual using the arrow up and down keyboard keys.

But you can also navigate it using:

```
Page Up -> Navigate one page (screen size) up
Page Down -> Navigate one page (screen size) down
Home -> Go to the beginning
Down -> Go to the end
```

You can also perform search with: `/<pattern>`, and it will search for the pattern you've typed.

After searching you can navigate by using:

```
n -> Go to the next result
N -> Go to the previous result
```

If you wish to learn more about `less`, you can check:

```
man less
```

Also, now you can finally RTFM