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…

Tutorial 5 of 5 5 resources in this section

Section overview

5 resources

Focuses 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

  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.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Case Converter

Convert text to uppercase, lowercase, sentence case, or title case.

Use tool

PDF Splitter & Merger

Split, merge, or rearrange PDF files.

Use tool

EXIF Data Viewer/Remover

View and remove metadata from image files.

Use tool

WHOIS Lookup Tool

Get domain and IP details with WHOIS lookup.

Use tool

Random String Generator

Generate random alphanumeric strings for API keys or unique IDs.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI 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 article

AI 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 article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help