4

I want to view a list of the headers in a man page without reading all of the man page. For example, in the bash man page (man bash.1) there are many headers: NAME, SYNOPSIS, COPYRIGHT, DESCRIPTION, etc. In essence, I want a list like the one presented at the top of this HTML man-page. Is there a way to get that locally in the command line?

This can be useful in case you want to search for a section by a different name than they provide ("syntax" instead of "grammar") or sometimes you don't even know what you're looking for.

AdminBee
  • 22,803
Noah May
  • 189
  • 1
    man-pages → Troff → https://www.gnu.org/software/groff/manual/groff.html ....... man-pages source code http://ftp.iij.ad.jp/pub/linux/kernel/linux/docs/man-pages/Archive/ ... → man-pages-5.12.tar.xz . – Knud Larsen Jul 28 '21 at 10:29
  • 1
    Been there. Your example page also uses subsections .SS which none of the answers address, because those are formatted with a few leading blanks (before the prevailing left-margin of text). – Thomas Dickey Jul 28 '21 at 19:32

3 Answers3

4

Edit:

I realized man page headers are the only text that is left-justified (for the most part). You can simply search /^\w after you open the man page and scroll up/down with N/n.

If you want to find the headers in a script, my original answer below might help or one of the others'.

Original:

Luckily, I found a solution based on this answer (go give them an upvote if this helped you).

zcat $(man -w bash.1) | grep -i '^\.sh'

man -w gets the location of the man page source (e.g. /usr/share/man/man1/bash.1.gz). zcat uncompresses the source on the fly and sends it to stdout. And the grep call searches for lines starting with .sh (case-insensitive), which mark "section headers."

This ignores subsection headers (.ss), but you can remedy that by changing the filter to '^\.s[sh]' (Thanks Thomas).

Noah May
  • 189
4

On systems using mandoc, you could ask for markdown-formatted output and then simply grep for lines starting with #:

$ man -T markdown man | grep '^#'
# NAME
# SYNOPSIS
# DESCRIPTION
# ENVIRONMENT
# FILES
# EXIT STATUS
# EXAMPLES
# SEE ALSO
# STANDARDS
# HISTORY

For manuals that this does not work for, you may instead go via HTML:

$ man -T html git | xmlstarlet sel -t -v '//h1/@id' -nl
NAME
SYNOPSIS
DESCRIPTION
OPTIONS
GIT_COMMANDS
HIGH
LOW
GUIDES
CONFIGURATION_MECHANISM
IDENTIFIER_TERMINOLOGY
SYMBOLIC_IDENTIFIERS
FILE/DIRECTORY_STRUCTURE
TERMINOLOGY
ENVIRONMENT_VARIABLES
DISCUSSION
FURTHER_DOCUMENTATION
AUTHORS
REPORTING_BUGS
SEE_ALSO
GIT
NOTES
Kusalananda
  • 333,661
  • Yeah. Unfortunately, this doesn't work by default on Ubuntu 20 (unless I'm doing something wrong). For example, man -T markdown man | head prints No manual entry for markdown trying to find a man page for markdown, and then outputs the raw source for man(1). Similar result with -T html. – Noah May Jul 29 '21 at 00:51
  • 1
    @Noah with Ubuntu (well, mandb man), you can use man --html=cat instead (it generates HTML and uses the cat command to show it instead of your browser) – muru Jul 29 '21 at 01:16
2

Shorter, simpler alternative is to grep lines that start with something other than whitespace:

$ man git | grep '^\S'
GIT(1)                            Git Manual                            GIT(1)
NAME
SYNOPSIS
DESCRIPTION
OPTIONS
GIT COMMANDS
HIGH-LEVEL COMMANDS (PORCELAIN)
LOW-LEVEL COMMANDS (PLUMBING)
GUIDES
CONFIGURATION MECHANISM
IDENTIFIER TERMINOLOGY
SYMBOLIC IDENTIFIERS
FILE/DIRECTORY STRUCTURE
TERMINOLOGY
ENVIRONMENT VARIABLES
DISCUSSION
FURTHER DOCUMENTATION
AUTHORS
REPORTING BUGS
SEE ALSO
GIT
NOTES
Git 2.32.0                        06/06/2021                            GIT(1)

Those extra lines can also be removed:

$ man git | grep '^\S' | sed '1d;$d'
NAME
SYNOPSIS
DESCRIPTION
OPTIONS
GIT COMMANDS
HIGH-LEVEL COMMANDS (PORCELAIN)
LOW-LEVEL COMMANDS (PLUMBING)
GUIDES
CONFIGURATION MECHANISM
IDENTIFIER TERMINOLOGY
SYMBOLIC IDENTIFIERS
FILE/DIRECTORY STRUCTURE
TERMINOLOGY
ENVIRONMENT VARIABLES
DISCUSSION
FURTHER DOCUMENTATION
AUTHORS
REPORTING BUGS
SEE ALSO
GIT
NOTES

If you're already viewing it through less (i.e. what happens when you just man git), you can use & to show just matching lines. IOW, you can type &^\S and less will show you just the headers (and first and last line). To go back to showing the whole manpage, you can type & then Enter.

JoL
  • 4,735