> /dev/null

Interesting hacks, snippets, and shortcuts.

Apr 12

Search for commands with wildcards

I often run into issues where I can't remember the exact name for a program. After spending half an hour looking for nm-applet, I decided it was time to be able to search by more than just prefix. Here is a snippet of code you can put in your .bashrc to allow you to search for programs more easily.

function find_command
{
    i=1;
    while [ $(echo $PATH | cut -d : -f "$i") ]; do
        PATHITEM=$(echo $PATH | cut -d : -f "$i")
        find $PATHITEM -name "$1"
        let i="$i+1"
    done
}

Now you can search with find_command "*applet*" to find all programs containing "applet".

Notes

  • Remember the to quote your search string if it has * in it. Otherwise bash will to to expand it.
  • If you have non-existent directories on your path you will get a warning for each one saying " No such file or directory". I know it sounds unlikely but it turns out both of the people who tested this ran into that issue.