grep the man page of a command for hyphenated options

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:

  • How do I use man pages to learn how to use commands?

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

Tags:

Grep

Options

Man