1. Overview

When working with files in bash, we often use wildcard characters to match file names. Bash interprets these characters and performs filename expansion, a process also known as globbing.

In this tutorial, we’ll introduce the concept of globbing by providing some examples that explain the wildcard characters and their use.

We should note that while globbing might look similar to regular expressions, they’re fundamentally different.

2. Matching Any String

Often, we use globbing along with ls. Let’s assume that we have a folder in our file system that contains a bunch of different kinds of files. Now, if we want to get a listing of all text files, we could cd to this folder and perform:

$ ls *.txt

In globbing, the * or asterisk character represents any string of any given length. The output of the command above might look something like this:

test-2.txt test-3.txt test.txt

3. Matching a Single Character

Where * matches character strings of any length, with ? we can match exactly one character. For example, we can use a single question mark to list all text files named ‘test-‘ followed by a single digit:

$ ls test-?.txt
test-2.txt test-3.txt

Or, we could list all text files with a name of exactly four characters:

$ ls ????.txt
test.txt

4. Matching a Range of Characters

We saw how to match any character by using * or ?. Suppose we have some more information about what we’re looking for. For example, we might know that we’re only interested in the files that contain some number in their filename. That’s where ranges come in.

Ranges are declared within square brackets. For example, the range of all numeric digits is represented by:

[0-9]

We use this pattern along with the asterisk to search for all files with a number in their name:

$ ls *[0-9]*
1234.dat    test-2.txt  test-3.txt  test-99.txt

Ranges can apply to letters as well as digits:

  • [a-z] = all lowercase characters of the alphabet
  • [A-Z] = all uppercase characters of the alphabet
  • [a-zA-Z] = all characters of the alphabet, irrespective of their case
  • [j-p] = lowercase characters j, k, l, m, n, o or p
  • [a-z3-6] = lowercase characters or the numbers 3, 4, 5 or 6

5. Hidden Files

By default, hidden files and folders don’t show up in the output of ls. To apply globbing to our hidden files and folders, we have to explicitly add a leading . (dot).

For example:

$ ls *

in our home directory might yield:

file.txt

whereas:

$ ls .*

might show:

.bashrc

6. Conclusion

In this article, we’ve been introduced to the process of filename expansion, also known as globbing. We learned how to apply the * and ? characters to match zero or more characters or exactly one character respectively. We also learned how to perform more advanced globbing by specifying ranges using brackets.

We should remember that, while the patterns seem similar, globbing doesn’t use regular expressions.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.