14

When I grep the man page of the find command for matches to type it returns a lot of search results that I don't want. Instead I want to use a command that returns only the search results for -type.

The command man find | grep -type doesn't work. It returns:

grep: invalid option -- 't'
karel
  • 2,030
  • 1
    Do you want just the individual lines of the formatted man page that contain the string -type, or do you want, say, the entire paragraph or two that describes what -type does? – Mark Plotnick Nov 17 '16 at 23:34
  • I want to know how to do it both ways, grepping for the individual lines that contain the string -type would be enough for the way I usually search the man pages, however returning the entire paragraph or two that describes what -type does would be very useful to do at least one time. – karel Nov 17 '16 at 23:51
  • BTW, if viewing a web page is an alternative, Idan Kamara at explainshell.com has done a great job of extracting the portions of man pages that describe command options. See, for example, http://explainshell.com/explain?cmd=find+-type+f to see just what the -type option does. – Mark Plotnick Nov 18 '16 at 00:10

2 Answers2

15

If you want to grep for a pattern beginning with a hyphen, use -- before the pattern you specify.

man find | grep -- -type

If you want more info, for example the entire section describing an option, you could try using Sed:

$ man find | sed -n '/-mindepth/,/^$/p'
   -mindepth levels
          Do  not apply any tests or actions at levels less than levels (a
          non-negative integer).  -mindepth  1  means  process  all  files
          except the command line arguments.

However, this won't work for every option you might search for. For example:

$ man find | sed -n '/^[[:space:]]*-type/,/^$/p'
   -type c
          File is of type c:

Not very helpful. Worse, for some options you could be misled into thinking you'd read the whole text about the option when you really hadn't. For example, searching -delete omits the very important WARNING contained as a second paragraph under that heading.


My recommendation is to use a standard call to man with the LESS environment variable set. I use it quite commonly in my answers on this site.

LESS='+/^[[:space:]]*-type' man find

To learn more about how this works, see:

LESS='+/^[[:space:]]*LESS ' man less
LESS='+/\+cmd' man less
LESS='+/\/' man less

If you just want to find the option quickly and interactively in the man page, learn to use less's search capabilities. And also see:

Wildcard
  • 36,499
  • Thank you for posting. The commands in your answer returned the results that I was looking for. I will accept an answer after a day or two, so please be patient. – karel Nov 18 '16 at 00:02
  • @karel, no problem, I'm very patient. :) A little puzzled, though: I know you can't start a bounty on a question for two days, but I believe the time limit before you can accept an answer is only 15 minutes or so. – Wildcard Nov 18 '16 at 00:06
  • 1
    man find | sed -n '/-type/,/^$/p' gives lot more than what you posted as it will match -type anywhere in the line... am working on small script myself to search man or help (for builtin) and currently using awk which still has few quirks to solve.. awk -v RS= -v rx="^\\\s*$arg\\\>" '$0 ~ rx' "$file" where arg would be -type in this case – Sundeep Nov 18 '16 at 02:44
  • @Sundeep, true. I updated with the actual command used. I didn't think it crucial because my point is that's not a good way to get the data, but it's better this way; thanks. – Wildcard Nov 18 '16 at 02:50
  • 2
    BTW, @Sundeep, you might want to try parsing the underlying troff files containing the original man page info with format information, instead of the text output of the man command. – Wildcard Nov 18 '16 at 02:52
  • @Wildcard will check it out, thanks... irregular spacing between different man pages makes it lot tougher and require patches.. my script so far works for most of my use cases though, needed a script to work a bit like explainshell, for ex: ch ls -latr -h – Sundeep Nov 18 '16 at 02:57
  • 1
    @Sundeep, here's the starting point for you. (Be sure to follow the link in that answer.) There's a LOT to know about troff. Ping me in chat when you finish diving down the rabbit hole. :) – Wildcard Nov 18 '16 at 03:07
  • @Wildcard, here's my current version of script .. as you've pointed out in this answer, it has its drawbacks but works well for many cases... will check out the groff rabbit hole and get back to you :) – Sundeep Nov 22 '16 at 06:13
2

Or pipe to less and feed that a search term:

man 1 find | less -p ' -type'

(This may fail depending on exactly what less is feed, e.g. if -type has been bolded up with backspaces.)

thrig
  • 34,938