Ubuntu – How to use grep to search for options in a man page?

command linegrepmanpage

I want to search for specific options like -s, -f, -l in a manpage and display only the results that contain info on what those options do. I tried this command hoping that the single quotes would bypass grep from receiving options:

man --pager=cat some_man_page_here | grep '-option_here'

I also tried \ but grep gives me a syntax error:

Usage: grep [OPTION]... PATTERNS [FILE]...
Try 'grep --help' for more information.

I just wanna know if there's a way to use grep to search for options in manpages. I'm currently using the Find button on the terminal top bar but I'd like to be able to use grep for efficiency.

Best Answer

  • Would this work in your case?

    $ man ls | grep -- '--a'
         -a, --all
         -A, --almost-all

    A more detailed (hopefully clearer) example of the command:

    $ man shutdown | grep -- '-'
           shutdown - Halt, power-off or reboot the machine
           shutdown may be used to halt, power-off or reboot the machine.
           logged-in users before going down.
           -H, --halt
           -P, --poweroff
               Power-off the machine (the default).
           -r, --reboot
               Equivalent to --poweroff, unless --halt is specified.
               Do not halt, power-off, reboot, just write wall message.
               Do not send wall message before halt, power-off, reboot.
           On success, 0 is returned, a non-zero failure code otherwise.


    As glenn jackman commented below (very useful):

    And to narrow down the results to just lines starting with a hyphen:

    grep '^[[:space:]]*-' – 

    Test run:

    $ man shutdown | grep -- '-' | grep '^[[:space:]]*-'
           -H, --halt
           -P, --poweroff
           -r, --reboot

    One could obviously also use apropos in Linux.

    [+] External Links:

    Full-text search for man pages - At Unix SE