Shell Scripting / File Handling in Shell Scripts
Using Globbing to Match File Patterns
This tutorial, 'Using Globbing to Match File Patterns', explains how to select multiple files based on a pattern using globbing in shell scripts. It will cover how to use wildcard…
Section overview
5 resourcesFocuses on reading, writing, and manipulating files within shell scripts.
Using Globbing to Match File Patterns
1. Introduction
In this tutorial, we are going to learn how to use globbing, a feature available in Unix/Linux systems, to select multiple files based on a pattern. Globbing makes use of wildcard characters to match file patterns in shell scripts.
By the end of this tutorial, you will learn:
- What globbing is and how it works
- How to use wildcard characters to match file patterns
- Write shell scripts using globbing
Prerequisites:
- Basic knowledge of Linux/Unix shell commands
- Familiarity with basic programming concepts
2. Step-by-Step Guide
Globbing is a method used in Unix/Linux to select filenames based on patterns containing wildcard characters. The wildcard characters used in globbing are *, ?, and [].
*matches any number of any characters including none.?matches any single character.[]matches any single character specified within the brackets.
Let's dive into examples to understand these wildcard characters.
3. Code Examples
Example 1: Using * Wildcard
In the code snippet below, we are using the * wildcard to match all files with .txt extension.
# List all .txt files in the current directory
ls *.txt
Here, * matches any number of any characters, and .txt specifies that we are only interested in files that end with .txt.
Example 2: Using ? Wildcard
In this example, we use the ? wildcard to match files of a specific pattern.
# List all files in the current directory with a single character name
ls ?
Here, ? matches any single character. So, this command will list files such as a, 1, _, etc.
Example 3: Using [] Wildcard
Here, we use the [] wildcard to match files starting with either 'a', 'b', or 'c'.
# List all files in the current directory starting with a, b, or c
ls [abc]*
In this case, [abc] matches any single character specified within the brackets, i.e., 'a', 'b', or 'c'; and * matches any number of any characters.
Please note that the actual output of these commands will depend on the files present in your directory.
4. Summary
In this tutorial, we learned about globbing, a method used in Unix/Linux to select filenames based on patterns containing wildcard characters. We also learned how to use the wildcard characters *, ?, and [] to match file patterns.
To further enhance your understanding of globbing, try to write more complex file patterns and use different wildcard characters together.
5. Practice Exercises
- Write a command to list all .jpg or .png files in the current directory.
- Write a command to list all files starting with a number.
- Write a command to list all files with exactly 2 characters in their name.
Solutions:
ls *.{jpg,png}
This command uses{jpg,png}to match either .jpg or .png files.ls [0-9]*
Here,[0-9]matches any single digit, i.e., any number from 0 to 9.ls ??
Here,??matches any two characters.
Remember, the best way to learn is by doing. So, keep practicing until you are comfortable with using globbing to match file patterns.
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Random Password Generator
Create secure, complex passwords with custom length and character options.
Use toolLatest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article