Bash Globbing Explained With Real-Life Use Cases


When I started working on Bash back in 2018, searching for files was challenging and often time-consuming. Then, I discovered Bash Globbing, a simple technique that automates tasks, reduces manual efforts, and simplifies the matching process. The best part? I don’t need to install any extra tools. Just a basic understanding of wildcards is enough to get started.

In this post, I’ll share some practical use cases of Bash Globbing that I personally use in my daily life to automate tasks.

What is Bash Globbing

Bash Globbing is a feature in Bash that helps you automatically match filenames and directories using special characters called wildcards. You can use patterns to match multiple files at once instead of typing each file name manually.

Understanding the Bash Wildcards

Wildcards are special symbols that enable us to search filenames based on a specific pattern. For example, we can search a file that contains a specific character, a substring, a particular pattern, or a range of numbers. The most frequently used Bash wildcards are discussed below:

Asterik (*)

Matches 0 or more characters.

Question Mark (?)

Matches exactly one character.

Square Brackets []

Matches a range of characters.

Exclamation Mark (!)

Excludes a specific character from the search.

Bash Globbing Real Life Usage

Let’s go through the following examples to understand how bash globbing assists us in automating our routine tasks:

Matching Multiple Characters in File Names

The asterisk (*) is the most used wildcard that lets us find n number of characters at a specific position in the given string/filename. For example, I execute the following command to find file names that start with anything but end with “.txt”:

Sometimes I come across a situation where I remember the starting character of the file and its extension, but I forgot the rest of the string. In that case, I create a search pattern with the * wildcard as follows:

This pattern retrieves all the files that start with a letter “e” and have a “.txt” extension:

Matching a Single Character or Number in File Names

Use the “?” wildcard to match a specific character at the start, end, or the specified index/position. For example, the following command searches for all those files that start with a string “example” followed by exactly one character:

I often need to search for files that start with any number of characters, then a specific substring, followed by exactly one character. In that case, I can combine “?” with “*” wildcard to create a search pattern to match filenames:

Another useful use case of the “?” wildcard is to find all the files of a specific length. For example, if I’ve to list all files of length 7, I use the “?” wildcard as follows:

This search pattern returns five files and a directory that contains 7 characters in their names:

Matching a Range of Characters in File Names

The square brackets let us perform file globbing within a specific range. For example, [0-9] matches all filenames that contain a digit, [a-z] and [A-Z] perform lowercase and uppercase pattern matching in the filenames. Similarly, we can match filenames case-insensitively using the pattern [a-zA-Z]. Use [a-z0-5] to match filenames between the range a to z and 0 to 5 digits.

In the following example, I use square brackets with the “*” wildcard to list all those files that contain a numeric value in their names:

We can use the “[]” wildcard to list all files whose name is “sample” followed by any number:

Matching Hidden Files

Hidden files are represented using a dot and are, by default, not included in the output of the ls command. To apply bash globbing to your hidden files and directories, you must specify a dot at the start of the filename.

For instance, this command lists all the hidden files and directories that start with “bash” and are followed by any number of characters:

Matching Filenames Based on Complementation

The “!” wildcard is used in globbing to complement the specified character from the filenames. For instance, the command below lists all the files and folders except the ones that begin with the string “example”:

Similarly, this command lists all the files starting with the string “sample” and ending with anything except 2:

Matching File Names Using Named Character Classes

We can use character classes in globbing to print named values. In bash globbing, character classes are used along with the wildcards to get the desired results. The table below illustrates some of the most frequently utilized character classes in globbing:

Character Class Description
[[:blank:]] Shows files and folders containing blank spaces and tabs.
[[:space:]] Prints file and folder names containing whitespaces.
[[:digit:]] Displays file and folder names containing numeric values in their names.
[[:alpha:]] Shows filenames containing alphabets(case-insensitive).
[[:alnum:]] Prints case-insensitive alphanumeric file names.
[[:upper:]] Prints only those filenames that contain uppercase letters.
[[:lower:]] Shows only those files and folder names that include lower-case letters only.
[[:punct:]] Prints filenames containing punctuation characters like “.”, “,”, “;”, “()”, “$”, etc.

Let’s use the [[:blank:]] class to list files and folders that contain a whitesapce or tab between their names:

In the following command, we use the [:digit:] character class to list all files and folders that contain a digit at the start of their names:

Creating a Shell Script for Bash Globbing

I often create shell scripts to automate my routine tasks. For example, let’s take a look at my Downloads directory, which contains videos, text files, PNG files, etc.

With time, it becomes messy, and finding and managing a specific file type becomes a headache for me. So, I often move these files to their respective folder to make things cleaner and smoother. However, moving files one by one is a time-consuming task. Therefore, I create a shell script, use globbing to filter files based on their types, and then move them to the desired folders.

Let’s create a bash script in any editor of your choice:

Make the script executable using the chmod command:

chmod u+x exampleScript.sh

Let’s specify the following code in the script to find all file types in the Downloads folder and move them to their respective directories:

#!/bin/bash

find ~/Downloads -name ‘*.txt’ -exec mv {} ~/Documents/ \;

find ~/Downloads -name ‘*.mp4’ -exec cp {} ~/Videos/ \;

find ~/Downloads -name ‘*.png’ -exec mv {} ~/Pictures/ \;

In this code, I use globbing with the find command to find the text, mp4, and png files. If the text file exists in the Downloads directory, I move it to the Documents folder. Similarly, I moved the MP4 and PNG files to the Videos and Pictures directories, respectively. Finally, run this shell script using the following command:

The output confirms that all file types have been successfully moved to their respective folders:

This way, we can create a bash script and use Globbing to automate our routine tasks.

Conclusion

Bash Globbing, or Filename expansion, is a technique that helps us in matching file names or paths using special characters called Wildcards. It automates the tasks, reduces manual efforts, avoids repetition, saves your time, and simplifies file operations. In this article, we explained what bash globbing is and how it works in real-life use cases to automate tasks.