bash

Inverse Case Insensitive Multiple Matching for grep

The apropos command is used to show man pages that exist on Linux, Mac, and Unix. It’s useful to, let’s say, look for a command that includes a given word in the description. It can also be used to show all commands that have man pages (and some that don’t). To see all commands for a machine, simply run apropos with a wildcard (*):

apropos *

This will take a while. Not all commands can actually be used. Constrain output (which takes no less runtime) by piping the output to a grep command. The -i option can be used to make grep parsing case insensitive. This means that if we search for Daemon or daemon, they’ll both match. The -v option can be used to do an inverse search. The default behavior of grep is to show only items that match a given search pattern. If we use -v then we instead match the opposite, or the display omits those objects that match a pattern from the results. The -E option instructs grep to act as egrep, which allows us to have multiple matching patterns separated by pipes that are wrapped in single-quotes. This keeps us from having to pipe output between a bunch of grep entries.

Let’s put all of this in a single command. The below does a search in apropos for all commands and then parses out those that can’t be invoked directly, like daemons, APIs, frameworks, services, servers, or managers.

apropos * | grep -ivE 'daemon|API|framework|Service|server|manager'

The runtime on a command like this is consider and the output is a long list of commands. We might remove server and manager from the list as they could skip over certain commands, but this should be a fairly exhaustive list of most commands available to call on a *nix computer. Some are perl scripts that happen to have a man page; others will show “nothing appropriate” meaning there’s a command without a man page. The point of the article isn’t as much to show a sure-fire way to find commands via apropos but instead to show how to do inverse grep, with case insensitivity, and multiple matches.

There are limitations and dragons here and there. Keep short of 300 characters (these grep strings tend to grow as we find more and more matches or false matches). Also empty lines can cause the parse to stop, er, parsing. But by and large this is a good start to more extended grep use. Some of the commands are actually just cruft of old man pages that haven’t been removed (e.g. some of the postfix from earlier versions of the Mac have plenty of these). It would be possible to search for each, and list them with a path, but now we’re getting into much more complicated loops based on the output of grep and it’s time to drive home… That might be easier to output to a text file and then “while read -r line do” but I have to drive home… OK fine let’s step through it… Nope, gotta’ pick up the kid…