Get yourself globstar, bash 4 for your Mac terminal

So how many times have you wanted a simple, easy-to-remember command that will do a pattern search for files with a certain name in a directory while using Mac? Even Spotlight is annoying to do something like that. I do this all the time, as I want to find any file that has a .txt extension or maybe a .pages extension. I’m going to show you how to do this with some of the blackest magic you can find.

First of all, though, you really don’t have to do this. There is always the trusty find command that’s been available in bash since forever. However, with the advent of bash 4, there is this really-great-oh-my-god feature that is so worth having, it’s down as globstar. To show you how cool this is, we’re going to count line numbers of every python file in the current directory using two command, the swiss-army knife find command, and using the globstar. See which one is easier on the eyes (and head):

find . -type f -name "*.py" -print0 | xargs -0 cat | wc -l

You have got to be kidding me, right. Here’s the equivalent command with our freaking-awesome globstar:

wc **/*.py -l

Two stars next to each other = globstar…

Okay, well it’s still pretty esoteric. But the second one is so much clearer. Both utilize the “wc” command (word count) with “option l” which means count the lines. (Why isn’t there a “lc” command for this, oh well, easy enough to make an alias for that…) The second one works simply because two globs next to each other makes it recursive, a feature found in bash 4. Here’s how to get it installed on the Mac.

Enable globstar on Mac

First you have to install bash 4, which you do thusly:

brew install bash

This doesn’t actually override your current bash, it installs into a completely different location. You can check where by using this:

brew ls bash

Look for the line that ends with /bin/bash, you’ll want to copy that line, and insert it at the end of the file /etc/shells, which provides the operating system with its available choices for shells. Now tell the operating system that you want to use that shiny-new bash as the default:

chsh -s /usr/local/bin/bash YOUR_USER_NAME

Confirm with:


Now you have to turn on the globstar, which you do with

shopt -s globstar

To make that permanent, do this:

echo "shopt -s globstar" >> ~/.bash_profile

There are loads of other features for the command-line savvy, which you can read about here.

Comments are closed.