Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Introduction to Bash Globbing on Linux
Bash globbing is the process of using wildcard characters to match multiple filenames or paths in the shell. Bash provides several special characters that can be used for globbing, such as *, ?, and []. This feature allows you to perform operations on multiple files without having to specify each filename individually.
The * character is a wildcard that can match zero or more characters in a filename or path. For example, the command ls * would list all files in the current directory, while the command ls *.txt would list all files with the .txt extension in the current directory.
The ? character is similar to the * character, but it only matches a single character. For example, the command ls ?.txt would match filenames like a.txt or b.txt, but not abc.txt.
The [] character is used to specify a character class, which can be used to match any single character that is a member of the class. For example, the command ls [abc]*.txt would match filenames like a.txt, b.txt or c.txt but not d.txt.
Matching Any String
The wildcard character * can be used to match any string of characters (including zero characters) in a filename or path.
ls * # List all files in current directory ls *file* # List files containing "file" anywhere in name ls *.txt # List all files with .txt extension ls doc*.pdf # List files starting with "doc" and ending with .pdf
The * character provides maximum flexibility for pattern matching, making it useful for batch operations on files with similar naming patterns.
Matching a Single Character
The wildcard character ? matches exactly one character, making it useful when you need to match a specific pattern that includes a single unknown character.
ls file?.txt # Match "filea.txt", "fileb.txt", not "fileab.txt" ls test? # Match "test1", "testA", not "test12" ls file?? # Match exactly two characters after "file" ls ???.log # Match 3-character filenames with .log extension
You can combine ? with other wildcards for more complex patterns, such as file?* to match files starting with "file" followed by at least one character.
Matching a Range of Characters
Square brackets [] allow you to specify a character class to match any single character that is a member of the class.
ls file[a-z].txt # Match "filea.txt" through "filez.txt" ls file[abc].txt # Match "filea.txt", "fileb.txt", "filec.txt" ls file[0-9].txt # Match "file0.txt" through "file9.txt" ls file[a-zA-Z].txt # Match any single letter (upper or lowercase)
You can negate a character class using ! or ^ inside the brackets:
ls file[!a-z].txt # Match any character NOT in a-z range ls file[^0-9].txt # Match any non-digit character
| Pattern | Description | Example Match |
|---|---|---|
| [a-z] | Lowercase letters | filea.txt |
| [A-Z] | Uppercase letters | fileB.txt |
| [0-9] | Digits | file3.txt |
| [!a-z] | Not lowercase | file1.txt |
Hidden Files
In Unix-based systems, files and directories that start with a dot . are considered hidden files. These files are not typically displayed by default when using commands like ls.
ls -a # Show all files including hidden ones ls -A # Show hidden files but exclude "." and ".." ls -la # Show detailed listing including hidden files ls .* # Match all hidden files using globbing ls .bash* # Match hidden files starting with ".bash"
Hidden files are commonly used for configuration files, such as .bashrc, .profile, and .gitignore. Understanding how to work with them using globbing patterns is essential for system administration.
Advanced Globbing Examples
ls backup_[0-9][0-9][0-9][0-9].tar # Match 4-digit backup files ls file[0-9]?.txt # Match file + digit + any char + .txt ls *.[ch] # Match files ending in .c or .h ls image[!0-9]* # Match "image" not followed by digit
Conclusion
Bash globbing provides powerful wildcard matching capabilities using *, ?, and [] characters. The asterisk matches any string, the question mark matches single characters, and brackets define character classes for precise pattern matching. Mastering these globbing patterns enables efficient file management and automation in Linux environments.
