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
         --author
    

    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.
           --help
           -H, --halt
           -P, --poweroff
               Power-off the machine (the default).
           -r, --reboot
           -h
               Equivalent to --poweroff, unless --halt is specified.
           -k
               Do not halt, power-off, reboot, just write wall message.
           --no-wall
               Do not send wall message before halt, power-off, reboot.
           -c
           On success, 0 is returned, a non-zero failure code otherwise.
    

    Edit:

    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:]]*-'
           --help
           -H, --halt
           -P, --poweroff
           -r, --reboot
           -h
           -k
    

    One could obviously also use apropos in Linux.

    [+] External Links:

    Full-text search for man pages - At Unix SE