Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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).

share|improve this question
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 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 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 at 11:31
    
Just manpages or files in general? – muru Feb 1 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 – juggernaut1996 Feb 1 at 12:22
up vote 10 down vote accepted

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.

share|improve this answer

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...

share|improve this answer

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' 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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