Sometimes, in the command-line interface, we want to see the documentation of a specific command.
In this quick tutorial, we’ll discuss how to get information for the commands in Linux using the man command.
2. man Page Organization and Structure
The man command is used to display the user manual of a command.
Usually, the argument given to man is the name of a program, a utility, or a function. Thus, the manual page related to each of these arguments is then found and displayed.
2.1. Numbered Sections
In general, the collection of manual pages for the system is divided into numbered sections. These section numbers and corresponding page types are:
- Executable programs or commands
- System calls (functions given by the kernel)
- Library calls (functions given by the library)
- Special files (usually found in /dev)
- File Formats and conventions (configuration files such as etc/passwd)
- Games (such as character-based applications)
- Miscellaneous (e.g. man (7))
- System administration commands (usually only for root)
- Kernel routines (non-standard)
2.2. Named Sections Within Each man Page
Within each manual page, there are many named sections. By default, these include NAME, SYNOPSIS, CONFIGURATION, DESCRIPTION, OPTIONS, EXIT STATUS, RETURN VALUE, and ERRORS.
In addition, man pages can include sections named ENVIRONMENT, FILES, VERSIONS, CONFORMING TO, NOTES, BUGS, EXAMPLE, AUTHORS, and SEE ALSO.
Now, let’s take a look at the basic syntax of the man command:
$ man [option].. [command name]..
For example, to get information about the passwd command, we can use:
$ man passwd PASSWD(1) User utilities PASSWD(1) NAME passwd - update user's authentication tokens SYNOPSIS passwd [-k] [-l] [-u [-f]] [-d] [-e] [-n mindays] [-x maxdays] [-w warndays] [-i inactivedays] [-S] [--stdin] [username] DESCRIPTION The passwd utility is used to update user's authentication token(s). This task is achieved through calls to the Linux-PAM and Libuser API. Essentially, it initializes itself as a "passwd" service with Linux-PAM and utilizes configured password modules to authenticate and then update a user's password.
3.1. Search by Keyword in Manual Pages
We can get the results matching a keyword or regular expression in all the manual pages using the -k option.
Now, let’s see what we can find with the -k option:
$ man -k printf asprintf (3) - print to allocated string dprintf (3) - formatted output conversion dprintf (3p) - print formatted output fprintf (3) - formatted output conversion
Here, we see a list of all man pages returned that match printf as a regular expression.
3.2. Display All Sections for a Command Sequentially
In default mode, the man command only shows the most relevant man page. We can ask the man to display all the matching man pages sequentially by using the -a option. Let’s see how to use this option to list all the matching man pages:
$ man -a intro --Man-- next: intro(2) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ] --Man-- next: intro(3) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ] --Man-- next: intro(4) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ] --Man-- next: intro(5) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ] --Man-- next: intro(6) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ] --Man-- next: intro(7) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ] --Man-- next: intro(8) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]
As a result, all the relevant man pages for the intro command are displayed in succession.
3.3. Display a Specific Numbered Section
As we know, the manual is divided into multiple sections. We can pass the section number as an argument to get to a specific section of the manual for a given command. Let’s see an example — we’ll look up section 2 of the intro command:
$ man 2 intro INTRO(2) Linux Programmer's Manual INTRO(2) NAME intro - introduction to system calls DESCRIPTION Section 2 of the manual describes the Linux system calls. A system call is an entry point into the Linux kernel. Usually, system calls are not invoked directly:instead, most system calls have corresponding C library wrapper functions which perform the steps required (e.g., trapping to kernel mode) in order to invoke the system call. Thus, making a system call looks the same as invoking a normal library function.
3.4. Find the Filesystem Location of a Command’s Manual Page
Furthermore, we can locate a man page in the filesystem using the -w option:
$ man -w ping /usr/share/man/man8/ping.8.gz
As shown above, we can see the filesystem location of the man page for the ping command.
Finally, we can read about the man command using man itself:
$ man man
In this quick tutorial, we’ve seen how to use the man command.
First, we covered what the man command does, then we discussed manual sections, page types, and command syntax. After that, we touched upon several topics such as moving into specific sections, filtering by keyword, and finding the filesystem location of a manual.
Finally, we’ve explored how to use the man command effectively while working on Linux.