Before we can start teaching you how to get help using the various forms of documentation available for Linux commands, we first have to learn how to read the default command syntax documentation. Most of the provided standard shell commands in Linux follow a uniform format describing their usage. Afterward, we will show you how to get help.
When working with the Linux command line, getting help and looking up information and documentation is very important because the command line can be very complex and nobody knows and can remember everything. On every Linux system, there are several ways available to get help, depending on the kind of level of information you need to know. In this section, we will tap into the different sources of documentation.
In a previous section, you already learned the general structure of Bash shell commands and everything you need to know about command options and parameters in general, but oftentimes this is not enough. For a lot of shell commands, the specific structure of options and arguments are very complex. A post can be bound to a specific position, and some of them can be mandatory or optional. Also, options and arguments can be interdependent. In Linux, a description of a command's command-line format, including arguments and options, is called command usage or syntax of a command. Learning to read a command's usage is one of the most essential skills that a Linux beginner needs to learn when starting. The standard way to describe command usage in Linux is the command name, square brackets that contain text, dots, and text, for example, CommandName [XXX]... TEXT. Square brackets mean that the content within is optional. Three dots mean that the expression right before the dots can be repeated multiple times or only once. Any word without square brackets is mandatory.
Take, for example, the general syntax for the ls command, which you already know how to work with. From the official ls manual, it can be read as ls [OPTION]... [FILE]...; this means that the command to list files has the following usage. It starts with the ls command name, everything else is in brackets, so all the options and arguments are optional, which means you can also execute ls without providing any further information, just by pressing the Enter key. But you can also provide multiple options or only one. Also, we can see that the arguments are of the FILE type, which means a file or directory is needed here at this position. You can also provide multiple files or directories, or only one or zero, as shown in the following screenshot:
As another example, the copy command can be run by using the cp command name followed by zero or multiple options. The syntax of the cp command is cp [OPTION]... SOURCE... DEST_DIR. You can completely skip option, but at least one or more source directory and exactly one destination directory are mandatory and denoted by the three dots, and you cannot run the command without them. For example, running just cp without at least two arguments produces the following error. Correct usage would be with all options:
Now that we know how to read any standard command syntax or usage, how can we actually get help? As we said before, there are several ways available, which are command help options, man pages, and full program documentation. Normally, all of these three types of help are installed together with the command line or program, so it's a very good habit to first try to get help for shell commands locally on the same machine where the commands live. This is usually the most accurate, reliable, and up-to-date information for every command and should be favored before doing internet research or using documentation from another computer with a different Linux version or system.
Command parameters, options, and features can change over time depending on the version and implementation, and they can be very dangerous if applied incorrectly. There are hundreds of commands available on Linux and every one of them has a different syntax. No one can memorize everything, so first let's start with the easiest and fastest way to get quick help for any standard Linux program that you already know the name of. In fact, most programs do have a special command-line switch that prints out a quick summary of the usage of its options and arguments on screen, which in most cases is all you need to know. However, the help or usage flag is not standardized on Linux and some commands don't even have this flag at all, but most tool developers follow the rule to use the one-character flag -h, or the long option flag --help.
Now, if you need more help, you can check out the commands manual, which is often called man pages by Linux users. Most programs have such documentation. For the next few examples, you need to install some additional software using your root account's password, which you set up during installation. Man pages use lesser navigation, which we will talk about later when we learn how to view text files.
The following steps will help you to navigate the manual of any command in the Linux Terminal:
- Open the Terminal and type man cp for the copy command.
- Use the Page Up and Page Down keys to scroll the document up and down, and slash (/) can be used to search text; put any keywords after the slash to search for, and then press Enter. For example, /backup.
- Press the End key to search for the next entry in the man page.
- To quit the search option, use the Esc key.
- Using lowercase g, you can scroll to the top of the page, whereas uppercase G scrolls to the bottom of the page.
- You can press lowercase q to quit the man page.
When you go back to the top of the page, the man page of the cp command is divided into different topics and headings, as shown in the following screenshot:
Most standard Linux commands follow this type of structure. Also, you can see here that some commands can have different usage formats depending on the options and arguments given. Now, quit using the q key. The man command has a very useful option, type man -k and put any definition of interests as an argument afterward. This will search all man pages installed on your system for a certain keyword. For example, this is very useful if you forgot a specific command name or need general help with the topic or command to use or where to look first. If you type the man -k copy command, this will print out all the man pages for the commands that have something to do with copying:
While using the -k flag, you also see that the search result writes some numbers in the brackets after the man name; these are man page sections, which is another very useful concept we need to know. A Linux shell definition, such as printf, can describe more than only a command-line program, and man pages not only describe command-line tools. In our example, printf is not only a command-line tool that can be started by the shell user, but also the name for a library function in the programming language C, which is used by this system. man now defines a system of section numbers for the type a specific man name is from. Typing man man will display the manual documentation for the man command, and search for the keyboard sections, as shown here:
As we can see in the previous screenshot, the man page of the man command has nine sections. The first one is the most important one for us in this section, as we are most likely the shell command users. But, as you can see, the third section is a library call. Type man printf, which prints the usage of the printf command. On the other hand, if you type man 3 printf, it will print the Linux programmers' manual for the C language.
Let's jump to the eighth section, which is the manual for the xfs_copy command written for the system administrators. Besides manual pages, a lot of commands that can be installed on Linux or that come right out of the box with the system, do have additional and advanced documentation available in a specific folder location in the filesystem on your hard disk. For some programs, additional documentation can also be installed using a special installation package, as we will learn later in this section. Sometimes, this additional documentation contains precious usage examples on how to use the program; information about the internal algorithms or approaches used; change log and license information; author contact information; history; a list of errors or limitations; or sample configuration files, which we will talk about later.
If you get stuck with the manual or it is just not enough for you, try to check out if a documentation folder exists for your command of interest in your CentOS 7 standard documentation path. Type, for example, the postfix documentation folder lives in. This is a good example. If you go into the directory, you will find a lot of additional documentation in text file format. Refer to the following screenshot for more information:
Use the less program to read the files. Use the same keyboard shortcuts to navigate the files as with the man pages, for example, type q to exit.