Shell Expansion & Globbing
Understand how the shell expands commands, including brace expansion, tilde expansion, and filename globbing patterns.
Shell Expansion & Globbing is a free DevOps Bootcamp lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is Shell Expansion?
In Linux, the shell is like a translator between you and the operating system. Before the shell runs your command, it often performs some 'magic' called expansion.
Shell expansion means the shell looks for special characters or patterns in your command and replaces them with something else. This happens before the command actually executes, saving you typing and making commands more powerful!
Your Home with Tilde `~`
The tilde (~) expansion is a super handy shortcut. It automatically expands to your current user's home directory.
Instead of typing /home/yourusername, you can just use ~. This is especially useful for navigating or referring to files in your home folder.
echo ~
echo ~/DocumentsTilde for Other Users
The tilde isn't just for your own home directory! You can also use it to refer to another user's home directory.
By typing ~username, the shell will expand this to the home directory of the specified 'username'. This is great for accessing shared files or configurations.
~rootwould expand to/root~guestwould expand to/home/guest
Brace Expansion `{}`
Brace expansion allows you to generate arbitrary strings. It's like a quick way to create multiple items from a single pattern without typing them all out.
The shell will expand the content within the braces {} into a list of separate words. This is very useful for creating files or directories with similar names, or for running commands on multiple items.
echo {apple,banana,cherry}Brace Ranges
You can also use brace expansion to generate sequences of numbers or letters. This is done using two items separated by .. within the braces.
The shell will generate all items in the sequence, making it easy to create numbered files or directories, or to iterate through a set of values.
echo file_{1..3}.txt
echo {a..c}_report.logIntro to Globbing
Globbing is the shell's way of matching filenames and pathnames using special wildcard characters. It's like using a 'search pattern' to find files.
When you type a command like ls *.txt, the shell doesn't send *.txt to ls. Instead, it expands *.txt to a list of all matching files in the current directory, and then passes that list to ls.
Match All with `*`
The asterisk (*) is the most common wildcard. It matches zero or more characters in a filename.
You can use it to match any sequence of characters, making it incredibly versatile for selecting files based on parts of their names.
mkdir -p glob_test
touch glob_test/report.txt glob_test/image.jpg glob_test/document.pdf
echo "Files ending in .txt:"
ls glob_test/*.txt
rm -rf glob_testMatch One with `?`
The question mark (?) wildcard matches exactly one character.
This is useful when you know the exact length of the part of the filename you want to match, but not the specific character itself.
mkdir -p glob_test
touch glob_test/file1.log glob_test/file2.log glob_test/fileA.log
echo "Files matching file?.log:"
ls glob_test/file?.log
rm -rf glob_testSpecific Characters `[]`
You can use square brackets ([]) to match any single character within a specified set of characters.
This allows for more precise matching than ?. You can list individual characters or specify a range (e.g., [a-z] for any lowercase letter, [0-9] for any digit).
mkdir -p glob_test
touch glob_test/photoA.png glob_test/photoB.png glob_test/photoC.png glob_test/photoD.png
echo "Files matching photo[A-C].png:"
ls glob_test/photo[A-C].png
rm -rf glob_testGlobbing Combinations
You can combine different globbing patterns and even brace expansion for powerful file selection.
The shell processes these expansions from left to right, creating complex patterns that match exactly what you need.
mkdir -p glob_test
touch glob_test/data_01.csv glob_test/data_02.txt glob_test/data_a.log glob_test/info_01.txt
echo "Files matching data_[0-9]*.{txt,csv}:"
ls glob_test/data_[0-9]*.{txt,csv}
rm -rf glob_testQuick Check
Which of the following commands will list files named report1.doc, report2.doc, and report3.doc, but NOT reportA.doc?
Recap & Next Steps
Great job! You've learned how the shell expands commands even before they run. This 'magic' makes your command-line work much more efficient.
- Tilde Expansion (
~): A shortcut for home directories. - Brace Expansion (
{}): Generates custom strings or sequences. - Globbing (
*,?,[]): Matches filenames using wildcards for powerful pattern-based selection.
Understanding these expansions is key to writing effective and concise commands. Keep practicing them!
Frequently asked questions
Is the “Shell Expansion & Globbing” lesson free?
Yes — the full text of “Shell Expansion & Globbing” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.
What will I learn in “Shell Expansion & Globbing”?
Understand how the shell expands commands, including brace expansion, tilde expansion, and filename globbing patterns. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Shell Expansion & Globbing” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Job Control (bg, fg, jobs)
- Shell Expansion & Globbing
- Aliases and Custom Prompts
- Shell Functions & Reusable Snippets