Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search Linux man pages (e.g. with grep)

I'm looking for information on the -X option of curl. However, the documentation is quite lengthy and I have to scroll down for quite a long time to get to this option. Instead, I'd like to do something like

man curl | grep -X

to get the line containing "-X". (I would also do this in conjunction with the option -A 10 to get the 10 lines after the match). However, if I try this I get

grep: option requires an argument -- 'X'
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.

Any ideas on how to use grep together with man, or more generally on how to search man pages for specific lines?

like image 618
Kurt Peek Avatar asked Jan 05 '17 16:01

Kurt Peek


People also ask

How do you search man pages in Linux?

To search a specific man page section, use the -s option with the man command and the -k or -K option. Note - Keywords are contained within double quotation marks.

How do I search for a grep file in Linux?

The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in. The output is the three lines in the file that contain the letters 'not'.

How do I search man pages in bash?

Type man bash or the word man followed by the name of any command that you'd be interested in reading about. Once you're inside of the man browser, type / followed by whichever word you'd like to find the next instance of. You can then push the enter or return key to search for it.

What is grep command in Linux with examples?

The grep filter searches a file for a particular pattern of characters, and displays all lines that contain that pattern. The pattern that is searched in the file is referred to as the regular expression (grep stands for global search for regular expression and print out).


1 Answers

You have to tell grep that -X is not an option, but the pattern to look for:

man curl | grep -- '-X'

-- indicates the end of options. Without it, grep thinks that -X is an option.

Alternatively, you can use -e to indicate that what follows is a pattern:

man curl | grep -e '-X'

If you want to see the complete man page but skip directly to the first occurrence of -X, you can use a command line option to less:

man curl | less +/-X

Typing N repeatedly then takes you to the following occurrences.

like image 132
Benjamin W. Avatar answered Sep 28 '22 09:09

Benjamin W.