Shell 中的 Globbing:原理、使用方法与实现解析(中英双语)

Shell 中的 Globbing:原理、使用方法与实现解析

在 Unix Shell(如 Bash、Zsh)中,globbing 是指 文件名模式匹配(filename pattern matching),它允许用户使用特殊的通配符(wildcards)来匹配多个文件,而不需要手动列出所有文件名。这一功能是 Shell 解析命令时的一个重要步骤,极大地提高了命令行操作的灵活性。


1. 什么是 Globbing?

Globbing 是 Shell 解释用户输入的命令时,将包含通配符的模式 扩展为符合匹配规则的文件名列表 的过程。例如:

ls *.txt

在执行 ls *.txt 时,Shell 不会将 *.txt 直接传递给 ls,而是会 先解析 *.txt 并匹配当前目录下的所有 .txt 文件,然后把结果传递给 ls 命令。例如,当前目录下有:

file1.txt file2.txt file3.log

则:

ls *.txt

实际上等价于:

ls file1.txt file2.txt

其中 file3.log 因不符合 *.txt 的模式匹配规则而被排除。


2. Globbing 的常见通配符

Shell 的 globbing 机制支持多种 通配符(wildcards),常见的有以下几种:

通配符作用示例匹配内容
*匹配 任意数量 的字符(包括 0 个字符)*.txta.txtb.txttest.txt
?匹配 任意单个字符file?.txtfile1.txtfile2.txt,但不匹配 file10.txt
[abc]匹配 [] 内的 任意一个字符file[12].txtfile1.txtfile2.txt
[a-z]匹配 某个范围内的字符file[a-z].txtfilea.txtfileb.txt
{a,b,c}匹配 逗号分隔的任意一个模式file{1,2,3}.txtfile1.txtfile2.txtfile3.txt
** (仅 Bash 4.0+)递归匹配所有子目录**/*.txt匹配当前目录及所有子目录中的 .txt 文件

3. Globbing 的执行流程

在 Shell 处理用户输入的命令时,globbing 是命令解析(parsing)过程中的一个步骤。执行流程如下:

  1. 用户输入命令(例如 ls *.txt)。
  2. Shell 解析命令
    • 如果命令行包含通配符(如 *),Shell 进入 globbing 处理
    • Shell 读取当前目录下的文件列表,并 匹配符合规则的文件名
  3. Shell 替换通配符模式
    • *.txt 被替换为所有匹配的文件名,如 file1.txt file2.txt
  4. Shell 执行最终命令
    • ls file1.txt file2.txt

注意:如果没有任何文件匹配通配符模式,大多数 Shell 会直接返回原始模式,如:

echo *.xyz

如果 *.xyz 没有匹配的文件,Bash 会直接输出 *.xyz,而不会报错。


4. Globbing 与 正则表达式 的区别

Globbing 并不是正则表达式,它们有以下主要区别:

特性Globbing正则表达式
作用文件名匹配处理文本模式匹配
* 含义匹配 任意字符(包括空字符)匹配 前一个字符 0 次或多次
? 含义匹配 任意单个字符匹配 前一个字符 0 或 1 次
. 含义作为普通字符匹配代表 任意单个字符

示例:

ls *.txt    # 使用 globbing,匹配所有 .txt 结尾的文件
grep 'a.*b' file.txt   # 使用正则表达式,匹配 'a' 到 'b' 之间的任何字符

5. 禁用 Globbing

有时候,我们希望 Shell 不要 自动展开通配符,而是让命令接收到原始的 *? 等字符。可以使用以下方法:

  1. 使用单引号 ''
    echo '*.txt'  # 输出 *.txt,而不是匹配的文件列表
    
  2. 使用 set -f 关闭 globbing
    set -f
    echo *.txt   # 直接输出 *.txt
    set +f       # 重新开启 globbing
    

6. Shell 中的 Globbing 实现

Globbing 在 Shell 内部是如何实现的呢?主要分为以下几步:

  1. 读取命令:Shell 读取用户输入的命令字符串。
  2. 解析通配符
    • 遍历当前目录文件列表。
    • 依次对文件名进行 模式匹配(Pattern Matching)。
    • 使用 字符串匹配算法 进行匹配,如:
      • * 进行贪心匹配(Greedy Match)。
      • ? 进行单字符匹配。
      • [a-z] 进行范围匹配。
  3. 替换匹配项
    • 匹配的文件列表替换原始通配符字符串。
  4. 执行命令:Shell 执行替换后的命令。

Shell 通常使用 系统调用 opendir()readdir() 访问目录并进行匹配。


7. 编写 C 代码实现简单的 Globbing

下面是一个使用 fnmatch() 函数进行 Shell 风格模式匹配的 C 代码示例:

具体原理和代码解析请看笔者的另一篇博客: 深入解析 fnmatch():C 语言中的模式匹配函数(中英双语)

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <fnmatch.h>

void list_matching_files(const char *pattern) {
    struct dirent *entry;
    DIR *dir = opendir(".");
    
    if (!dir) {
        perror("opendir");
        return;
    }

    while ((entry = readdir(dir)) != NULL) {
        if (fnmatch(pattern, entry->d_name, 0) == 0) {
            printf("%s\n", entry->d_name);
        }
    }
    
    closedir(dir);
}

int main() {
    list_matching_files("*.c");  // 匹配当前目录下的所有 .c 文件
    return 0;
}

运行示例

$ gcc globbing.c -o globbing
$ ./globbing
main.c
utils.c
shell.c

8. 结论

Globbing 是 Shell 解析命令的重要步骤,主要用于 文件名匹配,其实现涉及 字符串匹配算法、系统目录读取 等技术。用户可以利用 通配符 灵活地批量操作文件,但要注意它与 正则表达式 的区别。此外,用户可以通过 单引号set -f 禁用 Globbing,使 Shell 直接传递原始字符串。理解 Globbing 的原理可以帮助我们更高效地使用 Shell 命令,提高自动化任务的执行效率。


这篇博客详细介绍了 Globbing 的概念、用法、实现原理、C 代码示例,希望能帮助你更深入地理解这个 Shell 机制! 🚀

Shell Globbing: Principles, Usage, and Implementation

Globbing in Unix Shell (such as Bash, Zsh) refers to filename pattern matching, where users can use special wildcard characters to match multiple files without manually listing their names. This feature enhances the flexibility of command-line operations.


1. What is Globbing?

Globbing is the process where the Shell expands wildcard patterns into matching filenames before executing a command.

For example, consider the command:

ls *.txt

Instead of passing *.txt as an argument to ls, the Shell first expands the pattern by searching for files in the current directory that match the pattern. If the directory contains:

file1.txt file2.txt file3.log

then the command:

ls *.txt

is actually executed as:

ls file1.txt file2.txt

while file3.log is ignored because it does not match *.txt.


2. Common Wildcards in Globbing

Shell globbing supports several wildcards for flexible pattern matching:

WildcardDescriptionExampleMatches
*Matches any number of characters (including none)*.txta.txt, b.txt, test.txt
?Matches any single characterfile?.txtfile1.txt, file2.txt (not file10.txt)
[abc]Matches any single character in bracketsfile[12].txtfile1.txt, file2.txt
[a-z]Matches any character in the rangefile[a-z].txtfilea.txt, fileb.txt
{a,b,c}Matches any of the comma-separated patternsfile{1,2,3}.txtfile1.txt, file2.txt, file3.txt
** (Bash 4.0+)Recursively matches files in subdirectories**/*.txtAll .txt files in the directory tree

3. How Globbing Works Internally

When a user enters a command, Shell processing follows these steps:

  1. User inputs a command (e.g., ls *.txt).
  2. Shell scans for wildcards:
    • If a command argument contains a wildcard (*, ?, etc.), Shell performs globbing.
    • It retrieves the list of files in the current directory.
    • It matches filenames against the pattern.
  3. Shell replaces the pattern with matching filenames:
    • If *.txt matches file1.txt file2.txt, the final command becomes:
      ls file1.txt file2.txt
      
  4. Shell executes the command.

Note: If no files match, some shells return the original pattern (e.g., echo *.xyz outputs *.xyz).


4. Difference Between Globbing and Regular Expressions

Globbing is not the same as regular expressions. Key differences:

FeatureGlobbingRegular Expressions
PurposeMatches filenamesMatches text patterns
*Matches any charactersMatches previous character zero or more times
?Matches one characterMatches previous character zero or one time
.Treated as a normal characterMatches any character

Example:

ls *.txt         # Uses globbing to list all .txt files
grep 'a.*b' file.txt  # Uses regex to match 'a' followed by 'b'

5. Disabling Globbing

To prevent Shell from expanding wildcards, use:

  1. Single quotes (''):
    echo '*.txt'  # Outputs *.txt without expansion
    
  2. Disable globbing with set -f:
    set -f
    echo *.txt   # Outputs *.txt
    set +f       # Re-enables globbing
    

6. How Globbing is Implemented in Shell

Internally, globbing is handled in these steps:

  1. Shell reads the command string.
  2. Pattern matching against directory contents:
    • It retrieves files using system calls like opendir() and readdir().
    • It applies pattern matching algorithms.
  3. Matches are substituted before executing the command.

7. Implementing Globbing in C

The following C program demonstrates pattern matching using fnmatch(), which applies glob-style matching:

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <fnmatch.h>

void list_matching_files(const char *pattern) {
    struct dirent *entry;
    DIR *dir = opendir(".");
    
    if (!dir) {
        perror("opendir");
        return;
    }

    while ((entry = readdir(dir)) != NULL) {
        if (fnmatch(pattern, entry->d_name, 0) == 0) {
            printf("%s\n", entry->d_name);
        }
    }
    
    closedir(dir);
}

int main() {
    list_matching_files("*.c");  // Matches all `.c` files in the directory
    return 0;
}

Example Output

If the directory contains main.c utils.c shell.c, running:

$ gcc globbing.c -o globbing
$ ./globbing
main.c
utils.c
shell.c

8. Conclusion

Globbing is a key feature of Shell parsing, allowing users to efficiently match filenames using wildcards. It differs from regular expressions and is processed before executing commands. Understanding globbing helps users write more efficient command-line operations and scripts. It is implemented at the Shell level using directory scanning and pattern matching algorithms.

Mastering globbing enables more effective batch file operations, automation, and scripting in Unix-based systems! 🚀

后记

2025年2月4日于山东日照。在GPT4o大模型辅助下完成。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/964493.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

idea隐藏无关文件

idea隐藏无关文件 如果你想隐藏某些特定类型的文件&#xff08;例如 .log 文件或 .tmp 文件&#xff09;&#xff0c;可以通过以下步骤设置&#xff1a; 打开设置 在菜单栏中选择 File > Settings&#xff08;Windows/Linux&#xff09;或 IntelliJ IDEA > Preference…

备考蓝桥杯嵌入式4:使用LCD显示我们捕捉的PWM波

上一篇博客我们提到了定时器产生PWM波&#xff0c;现在&#xff0c;我们尝试的想要捕获我们的PWM波&#xff0c;测量它的频率&#xff0c;我们应该怎么做呢&#xff1f;答案还是回到我们的定时器上。 我们知道&#xff0c;定时器是一个高级的秒表&#xff08;参考笔者的比喻&a…

ChatGPT-4o和ChatGPT-4o mini的差异点

在人工智能领域&#xff0c;OpenAI再次引领创新潮流&#xff0c;近日正式发布了其最新模型——ChatGPT-4o及其经济实惠的小型版本ChatGPT-4o Mini。这两款模型虽同属于ChatGPT系列&#xff0c;但在性能、应用场景及成本上展现出显著的差异。本文将通过图文并茂的方式&#xff0…

Codeforces Round 1002 (Div. 2)(部分题解)

补题链接 A. Milya and Two Arrays 思路&#xff1a;题意还是比较好理解&#xff0c;分析的话我加了一点猜的成分&#xff0c;对a&#xff0c;b数组的种类和相加小于4就不行&#xff0c;蒋老师的乘完后小于等于2也合理。 AC代码&#xff1a; #include <bits/stdc.h> u…

机器学习中的关键概念:通过SKlearn的MNIST实验深入理解

欢迎来到我的主页&#xff1a;【Echo-Nie】 本篇文章收录于专栏【机器学习】 1 sklearn相关介绍 Scikit-learn 是一个广泛使用的开源机器学习库&#xff0c;提供了简单而高效的数据挖掘和数据分析工具。它建立在 NumPy、SciPy 和 matplotlib 等科学计算库之上&#xff0c;支持…

【Linux系统】信号:信号保存 / 信号处理、内核态 / 用户态、操作系统运行原理(中断)

理解Linux系统内进程信号的整个流程可分为&#xff1a; 信号产生 信号保存 信号处理 上篇文章重点讲解了 信号的产生&#xff0c;本文会讲解信号的保存和信号处理相关的概念和操作&#xff1a; 两种信号默认处理 1、信号处理之忽略 ::signal(2, SIG_IGN); // ignore: 忽略#…

OpenAI新商标申请曝光:AI硬件、机器人、量子计算全线布局?

每周跟踪AI热点新闻动向和震撼发展 想要探索生成式人工智能的前沿进展吗&#xff1f;订阅我们的简报&#xff0c;深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同&#xff0c;从行业内部的深度分析和实用指南中受益。不要错过这个机会&#xff0c;成为AI领…

python学opencv|读取图像(五十六)使用cv2.GaussianBlur()函数实现图像像素高斯滤波处理

【1】引言 前序学习了均值滤波和中值滤波&#xff0c;对图像的滤波处理有了基础认知&#xff0c;相关文章链接为&#xff1a; python学opencv|读取图像&#xff08;五十四&#xff09;使用cv2.blur()函数实现图像像素均值处理-CSDN博客 python学opencv|读取图像&#xff08;…

【C语言深入探索】:指针高级应用与极致技巧(二)

目录 一、指针与数组 1.1. 数组指针 1.2. 指向多维数组的指针 1.2.1. 指向多维数组元素的指针 1.2.2. 指向多维数组行的指针 1.3. 动态分配多维数组 1.4. 小结 二、指针与字符串 2.1. 字符串表示 2.2. 字符串处理函数 2.3. 代码示例 2.4. 注意事项 三、指针与文件…

吴恩达深度学习——有效运作神经网络

内容来自https://www.bilibili.com/video/BV1FT4y1E74V&#xff0c;仅为本人学习所用。 文章目录 训练集、验证集、测试集偏差、方差正则化正则化参数为什么正则化可以减少过拟合Dropout正则化Inverted Dropout其他的正则化方法数据增广Early stopping 归一化梯度消失与梯度爆…

蓝桥杯刷题 DAY4:小根堆 区间合并+二分

import os import sys import heapq# 请在此输入您的代码if __name__"__main__":x,n map(int,input().split())l[]a[0]*nb[0]*nc[0]*nq[]for i in range(n):l.append(list( map( int ,input().split()) ))l.sort(keylambda pair:-pair[1])total0j0for i in range(x,0…

K8S学习笔记-------1.安装部署K8S集群环境

1.修改为root权限 #sudo su 2.修改主机名 #hostnamectl set-hostname k8s-master01 3.查看网络地址 sudo nano /etc/netplan/01-netcfg.yaml4.使网络配置修改生效 sudo netplan apply5.修改UUID&#xff08;某些虚拟机系统&#xff0c;需要设置才能生成UUID&#xff09;#…

大语言模型深度研究功能:人类认知与创新的新范式

在人工智能迅猛发展的今天&#xff0c;大语言模型&#xff08;LLM&#xff09;的深度研究功能正在成为重塑人类认知方式的关键力量。这一突破性技术不仅带来了工具层面的革新&#xff0c;更深刻地触及了人类认知能力的本质。本文将从认知科学的角度出发&#xff0c;探讨LLM如何…

【Redis】Redis 经典面试题解析:深入理解 Redis 的核心概念与应用

文章目录 1. Redis 是什么&#xff1f;它的主要特点是什么&#xff1f;答案&#xff1a;主要特点&#xff1a; 2. Redis 的数据结构有哪些&#xff1f;分别适用于什么场景&#xff1f;答案&#xff1a;keys *命令返回的键顺序 3. Redis 的持久化机制有哪些&#xff1f;它们的优…

基于SpringBoot的物资管理系统

作者&#xff1a;计算机学姐 开发技术&#xff1a;SpringBoot、SSM、Vue、MySQL、JSP、ElementUI、Python、小程序等&#xff0c;“文末源码”。 专栏推荐&#xff1a;前后端分离项目源码、SpringBoot项目源码、Vue项目源码、SSM项目源码、微信小程序源码 精品专栏&#xff1a;…

【力扣】53.最大子数组和

AC截图 题目 思路 这道题主要考虑的就是要排除负数带来的负面影响。如果遍历数组&#xff0c;那么应该有如下关系式&#xff1a; currentAns max(prenums[i],nums[i]) pre是之前记录的最大和&#xff0c;如果prenums[i]小于nums[i]&#xff0c;就要考虑舍弃pre&#xff0c;从…

本地部署DeepSeek教程(Mac版本)

第一步、下载 Ollama 官网地址&#xff1a;Ollama 点击 Download 下载 我这里是 macOS 环境 以 macOS 环境为主 下载完成后是一个压缩包&#xff0c;双击解压之后移到应用程序&#xff1a; 打开后会提示你到命令行中运行一下命令&#xff0c;附上截图&#xff1a; 若遇…

代码随想录算法【Day36】

Day36 1049. 最后一块石头的重量 II 思路 把石头尽可能分成两堆&#xff0c;这两堆重量如果相似&#xff0c;相撞后所剩的值就是最小值 若石头的总质量为sum&#xff0c;可以将问题转化为0-1背包问题&#xff0c;即给一个容量为sum/2的容器&#xff0c;如何尽量去凑满这个容…

【Numpy核心编程攻略:Python数据处理、分析详解与科学计算】2.28 NumPy+Matplotlib:科学可视化的核心引擎

2.28 NumPyMatplotlib&#xff1a;科学可视化的核心引擎 目录 #mermaid-svg-KTB8Uqiv5DLVJx7r {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-KTB8Uqiv5DLVJx7r .error-icon{fill:#552222;}#mermaid-svg-KTB8Uqiv5…

基序和纯度分数的计算

以下对这两个概念的详细解释&#xff1a; 基序 纯度分数 PWM矩阵的来源 为什么会有PWM矩阵&#xff1f; 一个特定的转录因子&#xff08;TF&#xff09;的结合位点的基序&#xff08;motif&#xff09;并不是唯一的。实际上&#xff0c;TF结合位点通常具有一定的序列变异性&a…