Using Globbing to Match File Patterns

Tutorial 5 of 5

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

  1. Write a command to list all .jpg or .png files in the current directory.
  2. Write a command to list all files starting with a number.
  3. Write a command to list all files with exactly 2 characters in their name.

Solutions:

  1. ls *.{jpg,png}
    This command uses {jpg,png} to match either .jpg or .png files.
  2. ls [0-9]*
    Here, [0-9] matches any single digit, i.e., any number from 0 to 9.
  3. 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.