11

I want to learn how to search for a file by giving the content within it as a parameter. I can then apply the solution to search for commands contributed by Richard Stallman (through man pages).

6
  • 1
    The answer below is perfect for what you're asking in the title, but mind that searching compressed man pages like is not the same as searching text / binary files (for those you'll want to use the regular grep).
    – kos
    Jan 31, 2016 at 11:14
  • @kos good point. I tried to edit the question to add "man", but it falls below the limit, so I let it go ;)
    – philsf
    Jan 31, 2016 at 11:22
  • @philsf Judging from the inconsistency in the question apparently they assumed that either way it would be the same; you could still add "remove the z for non-gzip-compressed files" to your answer though ;)
    – kos
    Jan 31, 2016 at 11:31
  • 1
    Just manpages or files in general?
    – muru
    Feb 1, 2016 at 1:43
  • actually both... i wanted to know all the commands contributed by Stallman i can obviously use the solution for general files as well Feb 1, 2016 at 12:22

3 Answers 3

18

From man man:

-K, --global-apropos
      Search for text in all manual  pages.   This  is  a  brute-force
      search,  and is likely to take some time; if you can, you should
      specify a section to reduce the number of pages that need to  be
      searched.   Search terms may be simple strings (the default), or
      regular expressions if the --regex option is used.

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

Combined:

man -wK 'Richard M Stllman'

Though manpages typically have just Richard Stallman, with a variable amount of space between the two words, so a regex might be appropriate:

--regex
      Show all pages with any part of  either  their  names  or  their
      descriptions   matching   each   page   argument  as  a  regular
      expression, as with  apropos(1).   Since  there  is  usually  no
      reasonable  way  to  pick  a  "best"  page  when searching for a
      regular expression, this option implies -a.

So:

man --regex -wK 'Richard *Stallman' 
1
  • This solution uses only the intended tool (man) so it is much more elegant than mine. This answer should be the accepted one.
    – philsf
    Sep 16, 2018 at 15:02
12

This command will show you the filenames of man files that contain the keyword Stallman:

zgrep -l Stallman /usr/share/man/man?/*

Output in my 15.10 begins with:

/usr/share/man/man1/cat.1.gz
/usr/share/man/man1/comm.1.gz
/usr/share/man/man1/ctags.1.gz
/usr/share/man/man1/ctags.emacs24.1.gz

Then, you can browse as usual, with man cat, man comm, etc.

4

This method does not search the entire manpages for a keyword, but only each manpage's title and short description. It will not be enough in your case, but useful to quickly look up something. If it does not return the desired results, you have to use @philsf's answer.

You can use the apropos command to quickly search all installed manpages' title and description for a keyword:

$ apropos chat
chat (8)             - Automated conversational script with a modem
chattr (1)           - change file attributes on a Linux file system
empathy (1)          - GNOME multi-protocol chat and call client

You can display a known manpage's description using whatis:

$ whatis empathy
empathy (1)          - GNOME multi-protocol chat and call client

As I said, this method will not search the entire manpage body, therefore apropos Stallman returns nothing...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .