File Globbing In Linux

Table of Contents

File Globbing in Linux

By Derek Taylor at February 26, 2020

File globbing refers to “global” patterns that specify sets of filenames with wildcard characters. Globbing is the * and ? and some other pattern matchers you may be familiar with. An example would be doing something like the following in the Bash shell:

cp *.txt /Documents/text/

The above command moves all files that end in “.txt” from the current directory to the directory “/Documents/text/“. The * is a wildcard that stands for “any string of characters”. *.txt is a glob pattern. When the shell sees a glob, it will perform pathname expansion and replace the glob with matching filenames when it invokes the program.

Globbing Syntax

WILDCARD DESCRIPTION
* matches any number of any characters including none
? matches any single character
[abc] matches one character given in the bracket
[a-z] matches one character from the range given
[!abc] matches one character not given in the bracket
[!a-z] matches one character not from the range given
Example Matches
Bar* Bar, Bars, Barter
?at Bat, bat, Cat, cat
[CB]at Cat or Bat
File[0-9] File1, File2, etc.
[!B]at bat, cat, Cat
File[!4-9] File1, File2, File3
Example Does Not Match
Bar* FooBar, Ba, ar
?at at
[CB]at cat or bat
File[0-9] File, Files, File 10
[!B]at Bat
File[!4-9] File4, File5, Filexx

Globbing in SQL

SQL has the equivalent of * and ?. In SQL % equates to * and _ equates to ?. There is no equivalent to […].

Globs Are Not Regular Expressions

Though they rememble regex, glob patterns are not regular expressions. Unlike regular expressions, globbing is specifically for pattern matching filenames. Regex has much more syntax to it and is much more complicated. Also, the syntax that is shared with globbing is not the same thing in regex.

Footer

Copyright © 2020-2021 Derek Taylor (DistroTube)

This page is licensed under a Creative Commons Attribution-NoDerivatives 4.0 International License (CC-BY-ND 4.0).

The source code for distro.tube can be found on GitLab. User-submitted contributions to the site are welcome, as long as the contributor agrees to license their submission with the CC-BY-ND 4.0 license.

Author: dt

Created: 2022-02-20 Sun 10:05