When looking for the usage of a Linux/Unix command, you actually don't have to Google or search in the discussion forums, wiki pages, blogs or any offline/online resources. Everything is documented in the manual pages (shortly man pages) of the command. You just need to learn to use man pages efficiently in-order to get the details of a command or its options/flags easily and quickly. Today, I will share a lesser-known man page trick i.e. how to view a specific section in man pages in Linux and Unix.
Man page default structure
A man page consists several different sections, organized with headings for each section, such as NAME, SYNOPSIS, CONFIGURATION, DESCRIPTION, OPTIONS, EXIT STATUS, RETURN VALUE, ERRORS, ENVIRONMENT, FILES, VERSIONS, CONFORMING TO, NOTES, BUGS, EXAMPLE, AUTHORS, and SEE ALSO. Each man page has its command name followed by the section number in parenthesis.
Here is the list of section numbers of the manual followed by the types of pages they contain:
1 - Executable programs or shell commands. 2 - System calls (functions provided by the kernel). 3 - Library calls (functions within program libraries). 4 - Special files (usually found in /dev). 5 - File formats and conventions e.g. /etc/passwd. 6 - Games. 7 - Miscellaneous (including macro packages and conventions), e.g. man(7). 8 - System administration commands (usually only for root). 9 - Kernel routines [Non standard].
Some pages have all of these sections and some does not have. Now let us see how to view a specific section of a command's man page.
View A Specific Section In Man Pages In Linux
Let us open a man page of a command, for example uname.
$ man uname
Scroll down the end of the man page and you will see references to other commands.
[...] SEE ALSO arch(1), uname(2) [...]
Do you see the all those numbers trailing the commands at the end? You will see one or more reference commands with numbers in parenthesis at the end of almost all commands' man pages. These numbers indicates the specific section of the command's man page you want to look at. In other words, the number corresponds to what section of the manual that page is from.
To tell man explicitly to open the section 2 of the uname command, simply run:
$ man 2 uname
$ man uname.2
In the following screenshot, the left side Terminal window shows the output of "man uname" command. The right side of the Terminal window shows the output of "man 2 uname" (i.e. section 2 of uname) command.
Please note that when we run the man command without any numbers, it is usually equivalent of:
$ man 1 <command>
Some man pages doesn't have a specific section. In that cases, you don't get anything back when trying to view the man page with section number. For example, I run the following command to see the 5th section of uname command:
$ man 5 uname
Since uname command doesn't has section 5, I get the following output:
No manual entry for uname in section 5 See 'man 7 undocumented' for help when manual pages are not available.
Show all sections in a row
Like I already said, a specific command's man page has many sections. To view every matching page in a row, use -a flag like below.
$ man -a uname
Press q to quit one section and move to the next section.
Show all commands matching to a string with section numbers
We can search for man pages containing a search string using -k flag. So if you want to retrieve all section numbers of specific command in the man page, use -k option like below.
$ man -k uname
arch (1) - print machine hardware name (same as uname -m) oldolduname (2) - get name and information about current kernel olduname (2) - get name and information about current kernel uname (1) - print system information uname (1posix) - return system name uname (2) - get name and information about current kernel
As you see in the above output, we listed all commands(i.e. substrings) that matches to the given keyword string. To strictly limit the search within the matched string, use:
$ man -k '^uname'
uname (1) - print system information uname (1posix) - return system name uname (2) - get name and information about current kernel
You can alternatively use -f flag.
$ man -f uname
uname (1) - print system information uname (2) - get name and information about current kernel uname (1posix) - return system name
For more details, please look at the man pages of "man".
$ man man
Also, see other help sections for man pages.
$ man -k man
$ info man
- Good Alternatives To Man Pages Every Linux User Needs To Know
- How To Find Longest Man Page In Linux
- How To Easily Recall Forgotten Linux Commands
- How To Create And Maintain Your Own Man Pages
- How To Install Missing Man Pages Of Commands On Ubuntu
- Pinfo – A CLI Program To Read Info And Man Pages In Color
Hope this helps.