Shell – How to Output a List of All Man Pages in a Particular Section

manshell

A man page for fork, for example, is in the System Calls section that has number 2:

man 2 fork

How do you see what else is section 2 without resorting to Google?

Best Answer

This command lists the sorted names of all the entries in the given section:

man -aWS 1 \* | xargs basename | sed 's/\.[^.]*$//' | sort -u

If you want to see the pathnames, use:

man -aWS 1 \* | sed 's/\.[^.]*$//' | sort

This tells man to search a section for all commands using the wildcard pattern * (backslash-quoted so the shell doesn't interpret it). -a finds all matches, -W prints the pathnames instead of displaying the pages, and -S 1 specifies section one. Change the 1 to whatever section you want to search.

The sed command strips the filename extensions; remove it if you want to see the complete filenames. sort sorts the results (-u removes duplicates).

For convenient reuse, this defines a Bash shell function:

function mansect { man -aWS ${1?man section not provided} \* | xargs basename | sed 's/\.[^.]*$//' | sort -u; }

For example, you can invoke it as mansect 3 to see the entries in section three.

[Tested on macOS.]

Related Question