grep the man page of a command for hyphenated options

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'

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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:

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments