Globbing in Bash

Bash globbing is a fundamental feature that allows you to match multiple filenames or paths using wildcard characters.
Globbing in Bash

On this page

Bash globbing is a fundamental feature that allows you to match multiple filenames or paths using wildcard characters. This feature is essential when working with files in a Linux environment, as it helps automate and manage tasks efficiently. In this guide, we'll dive into the details of globbing, covering its syntax, usage, and some practical examples to help you master this powerful concept.

What is Bash Globbing?

Globbing in Bash is the process of using wildcard characters to match filenames or paths. Unlike regular expressions, which are used in commands like sed or awk, globbing is specifically for filename expansion within the shell. The most commonly used wildcards in globbing are:

  • `` (Asterisk): Matches zero or more characters in a filename or path.
  • ? (Question Mark): Matches exactly one character.
  • [] (Square Brackets): Defines a character class to match any single character within the brackets.

These characters allow you to work with groups of files or directories without specifying each one individually.

Matching Any String with ``

The asterisk * is perhaps the most versatile wildcard in globbing. It can match any string of characters, including an empty string, which makes it incredibly useful for listing or manipulating files.

For example, to list all files in the current directory, you can use:

$ ls *

To narrow it down to files with a specific extension, like .txt files, you would use:

$ ls *.txt

This command will list all files ending with .txt in the current directory, regardless of what comes before the extension.

Matching a Single Character with ?

The question mark ? is used to match exactly one character in a filename. This is useful when you know the structure of a filename but need to match files with slight variations.

For example, to list files that start with "file" and have any single character before the .txt extension, you could use:

$ ls file?.txt

This command would match filenames like file1.txt or filea.txt, but not file12.txt or file.txt.

Matching Specific Characters with []

Square brackets [] allow you to specify a character class, matching any single character within the brackets. This is particularly powerful when you want to match files with specific patterns.

For example, to match files that start with "file" followed by any digit from 1 to 5, you would use:

$ ls file[1-5].txt

This command matches file1.txt, file2.txt, etc., but not file6.txt.

You can also combine ranges and individual characters within the brackets:

$ ls file[a-zA-Z]*.txt

This command matches files starting with "file" followed by any letter, regardless of case, and ending with .txt.

Matching Hidden Files

In Unix-like systems, files or directories that begin with a dot (.) are considered hidden. These files do not appear in the output of standard ls commands unless explicitly specified.

To list all hidden files and directories, you can use:

$ ls -a

To specifically match hidden files with globbing, include the dot in your pattern:

$ ls .*

This command lists all hidden files and directories in the current directory.

Conclusion

Bash globbing is a powerful feature that makes it easier to work with files and directories through the use of wildcards. By understanding and applying the wildcards *, ?, and [], you can perform complex filename matching and manage your file tasks more effectively. Whether you're working with large directories or simply trying to match specific patterns, globbing is an essential skill for any Bash user. Explore these patterns in your scripts to see how they can simplify your tasks and improve your command over file management.

Subscribe to sysxplore newsletter and stay updated.

Don't miss anything. Get all the latest posts delivered straight to your inbox. It's free!
Great! Check your inbox and click the link to confirm your subscription.
Error! Please enter a valid email address!