Practical Uses For Bash Globbing
Publish Date2014-01-14

The first half of this article discusses What Globbing is, how to Enable Globbing, and How it works. If you are more experienced feel free to skip to The Tips.

What Globbing Is

Globbing lets you be more ambiguous with the way you specify input to your computer. To glob an input use the * operator in place of more specific text.

Make sure you have globbing enabled in your shell ( See Next Section ) before delving into Seeing globbing in Action .

Enable Globbing

By default your bash instance should have an option called globbing enabled. To check run the following command:

$ set -o | grep glob
  noglob      off

The examples in this article rely on that option to be set, so make sure globbing is enabled ( noglob off ). If it says noglob on you will need to enable globbing. There are two ways to do this.

## Temporary Method
$  set -o noglob
$  set -o | grep glob
  noglob      off

A more permanent solution is to add this line to your bashrc:

shopt -s extglob

See Globbing In Action

To get a feel for how globbing works, just give it a try.

As an example lets use some globbing in my Blog Posts folder:

$  echo * 2013-12-28-4-stereotypical-technology-users-(starwars-edition).md 2014-vim-tips.txt vimstuff.txt c.txt

In the previous example the * is expanded to every file in the folder.

$ echo 2014* 2014-vim-tips.txt

Notice how the glob worked in the previous command. It told the echo command to print all the files that start with 2014.

$ echo *.txt
2014-vim-tips.txt vimstuff.txt c.txt

Putting the * first switches the way the glob works. Instead of matching the beginning, the command grabs all the files that end with the text after the *.

$ echo *vim* 2014-vim-tips.txt vimstuff.txt

Using a * around some text will look for it anywhere in the input. Notice that the * will match vimstuff.txt, this shows that the star can match nothing as well as something.

$ echo 20*vim* 2014-vim-tips.txt

Finally, you can use as many *'s as you want to refine your globs.

The Tips

Here are some of my favorite ways to use globbing. You will find your own unique ways as well, but this will be a good handful to get your feet wet with.

Perform Filetype Specific Move / Copy

$ mv *.txt ~/Documents/Notes
$ mv *.css stylesheets/
$ cp ~/SomeProject/*.html* .
$ ls *.html*

Grab All Folders

$  echo */

Hone in on a file quickly

This tip is my absolute favorite, and the reason I wrote this article.

$ echo * 2013-12-28-4-stereotypical-technology-users-(starwars-edition).md 2014-vim-tips.txt vimstuff.txt c.txt
$ echo *master*
$ mv *master*
$ vim *master*
$ rm *master*
$ ls
$ vim *jquery*


This tip even works for commands like cd.

$ cd *ox*
$ pwd

That is all I am going to offer up now, but feel free to post your own favorites / discoveries in the comments.