3

This question already has an answer here:

I'd like to search for a certain string in all system man pages, but I only want to get a list of the man pages where this text appears. The first 2 pages of results Google returned do not provide the answer I'm after, if at all such an answer exists.

Let me explain:

I'd like to see all man pages where the word hairpin appears. I proceed to type man -K hairpin, and the contents that appear are akin to me having typed man nmcli. When I exit this man page via q, the following then appears on my screen:

--Man-- next: bridge(8) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]

From here, I can continuously type Ctrl+D and eventually return to the command prompt with the following displayed above the prompt:

--Man-- next: bridge(8) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]
--Man-- next: lldptool-evb22(8) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]
--Man-- next: systemd.network(5) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]
--Man-- next: nm-settings(5) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]
--Man-- next: nm-settings-ifcfg-rh(5) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]
--Man-- next: systemd.directives(7) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]

What I'm asking is, is it possible to get a listing of the man pages where hairpin appears such as below?:

man -<some-option> -K hairpin
nmcli(1)
bridge(8)
lldptool-evb22(8)
systemd.network(5)
nm-settings(5)
nm-settings-ifcfg-rh(5)
systemd.directives(7)

Edit:
Searching Google using a sentence doesn't always return the results where the specific query is addressed. This is obviously the case for this question, as pointed out by several that it is indeed a duplicate.

However, it would be best not to delete it as it shows the specific output returned on Ubuntu systems. A user on Ubuntu may benefit from this question.

Edit 2:
Searching for skip (Ctrl-D) now has this question appearing in the first page of Google results where previously nothing was returned!

marked as duplicate by muru, Jakuje, Stephen Kitt, Jeff Schaller, cuonglm Mar 28 '16 at 1:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    Is a terrible ugly hack like PAGER="head -1" man -K hairpin </dev/null good enough for you? – Celada Mar 27 '16 at 12:39
  • Awesome, Celada. Now I just need to type it a few times to train the grey matter! It would be better though if an option was added to man to allow for this. – AnthonyK Mar 27 '16 at 12:48
  • A better version of @Celada's clever trick could be PAGER='awk "NR==1{print $1}"' man -K hairpin </dev/null or even PAGER='perl -lane "print lc($F[0]) if $.==1"' man -K hairpin </dev/null. Celada, why not whip that into an answer? – terdon Mar 27 '16 at 12:59
  • @terdon, I don't think other answers will get votes being that the answer you've provided indicates that this option already exists - just that I wasn't aware - or rather, hadn't taken the time to read the manual thoroughly! – AnthonyK Mar 27 '16 at 13:09
6

You can use the -w option:

   -w, --where, --path, --location
          Don't actually display the manual pages, but do print the  loca‐
          tion(s) of the source nroff files that would be formatted.

That returns almost what you asked for:

$ man -wK hairpin
/usr/share/man/man1/nmcli.1.gz
/usr/share/man/man8/ip-link.8.gz
/usr/share/man/man8/bridge.8.gz
/usr/share/man/man5/systemd.network.5.gz
/usr/share/man/man5/nm-settings.5.gz
/usr/share/man/man5/nm-settings-ifcfg-rh.5.gz
/usr/share/man/man7/systemd.directives.7.gz

If that's not enough, you can parse it into shape:

$ man -wK hairpin | perl -pe 's#.*/([^/]+)\.(\d+)\.gz#$1($2)#'
nmcli(1)
ip-link(8)
bridge(8)
systemd.network(5)
nm-settings(5)
nm-settings-ifcfg-rh(5)
systemd.directives(7)

Finally, you could make that into a function. Add this line to your shell's initialization file (~/.bashrc, for example):

function sman(){ man -wK "$@" | perl -pe 's#.*/([^/]+)\.(\d+)\.gz#$1($2)#'; }

Then, you can run:

$ sman hairpin
nmcli(1)
ip-link(8)
bridge(8)
systemd.network(5)
nm-settings(5)
nm-settings-ifcfg-rh(5)
systemd.directives(7)

Not the answer you're looking for? Browse other questions tagged or ask your own question.