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'
3
  • 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? Commented Nov 17, 2016 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. Commented Nov 17, 2016 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, explainshell.com/explain?cmd=find+-type+f to see just what the -type option does. Commented Nov 18, 2016 at 0:10

2 Answers 2

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:

8
  • 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. Commented Nov 18, 2016 at 0: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. Commented Nov 18, 2016 at 0: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 Commented Nov 18, 2016 at 2:44
  • 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. Commented Nov 18, 2016 at 2:52
  • 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. :) Commented Nov 18, 2016 at 3:07
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.)

1
  • Pipe it through col -bx first. Commented Nov 18, 2016 at 3:08

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.