Searching from Command Line in Linux

For those of us learning command line for the first time, it can be a pretty intimidating thing. A terminal in the hands of a person with a black belt in shell commands can be far more powerful than a GUI (graphical user interface). However, becoming a so-called black belt takes a lot of time and practice. Unlike a GUI, you have to memorize the commands you need…mostly.

I say mostly because there is a command in Linux that is probably one of the singly most used commands out there. That command is known as the man command. In interviews in response to a question you don’t know, "I would just read the man pages" is the equivalent of "Jesus is the answer to everything" in church. The great thing is both actually work (not to put an obvious religious statement in my blog here).

Man is short for manual. It’s like reading your car manual, but for a command in your shell. For instance, if you run

man mkdir

You see something an explanation of what the command does, how to use the command, and the various advanced features you can do with it.

But what ifyou don’t know what command to use?

Thankfully, there is a relatively simple solution to this. All you really know is how to describe what you want to do in a simplistic way. Ladies and gentlemen, that command is the man -k command. I hope you all didn’t pay full price for your seats because you’ll only be using the edge.

man -k <command>

The man -k command/switch searches all of the commands that have man pages for what you typed in to search for. It then returns the command name with a short explanation of what it does. Let’s get some practice in.

Say you want to search for how to create a directory. We’re going to run

man -k "make directories"

And it will return

mkdir  (1)              make directories

Cool, huh? Now, there is a complication to this. If you want to search for something and the exact text you type isn’t in the manual exactly as you typed it, it will not be returned. For instance…

man -k "create directory"

…will return nothing becuase the manual for mkdir has "make directories" in it, not "create directory". How do we get around this?

Wild cards and very simple one word searches.

Wild Cards

Now, let’s say you’re not sure if the manual you’re looking for has the word directories, directory, or just dir in it. We need a way to search for multiple forms of a word. We do this with what is called a wild card character. Run the following command: <pre>man -k dir*</pre>

This will search the manuals for any words that start with dir and end with anything (including spaces or other words).

Once you’ve found the command you want, you can simply type <pre>man <command></pre> and you can read the manual until you’re heart is content, or even until it stops!  :)

On a similar note, to get out of reading a manual, press the letter q. I can’t tell you how long it took me to figure that out when I first was learning about the man pages. I guess now I should be ashamed of myself.