What is File Globbing in Linux (How to Use It with Examples)

Linux TLDR
Last Updated:
Reading time: 4 minutes

If you have ever used wildcard characters like (*) or (?) for global selection, then you are already interacting with file globbing, but let’s get a proper picture of it with its introduction.

What is Linux File Globbing?

File globbing is a built-in shell operation (or shell feature) that recognizes patterns like (*), (?), ([]), (^), etc. and interprets them to perform pathname expansion.

πŸ“
It may not work the same with all shells, so I assume you are using the most common Bash shell.

For example, the following command will list all the files that share the common β€œ.txt” extension using the (*) operator.

$ ls *.txt

Output:

Listing all the ".txt" files

In the above command, the (*) in (*.txt) is a wildcard character that matches all the files ending with the β€œ.txt” extension in the current working directory. This makes it easier for the user to select a group of files that share similarities using the wildcard characters.

Although, file globbing is not limited to searching for files in a directory, it can also be used to search for strings within a text file with the grep command.

πŸ“
Grep uses its own set of pattern matching rules; they look similar but might behave differently. So, don’t consider this article a Grep regular expression tutorial.

So, read this article until the end to learn about the different kinds of wildcard characters and how to use them.

Standard Wildcards (Globbing Patterns)

The following is a list of all the wildcard characters used for file globbing in Linux. starting with

Asterisk (*)Β 

As you saw earlier, this pattern can match any number of characters, either zero or more. Generally, it is used when you have less context about the file or want to select a group of files sharing some similarities.

1. The following command will search for all the files with the β€œ.php” extension.

$ ls *.php

Output:

Listing all PHP files

2. The following command will search for all the files with any extension, but the name should be β€œindexβ€œ.

$ ls index.*

Output:

Listing all the files with the name index

3. The following command will search for all the PHP files that start with the β€œpage” string.

$ ls page*.php

Output:

Listing all the PHP files that start with the "page" string

I think it would be enough of an example to show the usage of this wildcard character; let’s jump on to the next

Question Mark (?)

The (?) is used to match with one single character when declared once, or you can specify it multiple times like (??) to match multiple characters.

This wildcard character is mostly used when you have little context about the number of characters your files contain that you are interested in searching for.

1. The following command will search for all the PHP files whose names contain five characters.

$ ls ?????.php

Output:

Listing all the PHP files with names containing five characters

2. The following command will search for all the files that start with the β€œfood” string and after that contain four random characters.

$ ls food????.txt

Output:

Listing all the files with the last four unknown characters

3. The following command is identical to the previous one, but it will list all the files with any extension.

$ ls food????.*

Output:

Listing any files with last four unknown character

Square Bracket ([])

The ([]) is used to match a range of characters. If you specify β€œfile[1,2,3].txtβ€œ, it will select all the β€œfile1.txtβ€œ, β€œfile2.txtβ€œ, and β€œfile3.txt” files.

The following is a list of known range declaration examples:

  • β€œ[:upper:]” or a more specific β€œ[A-Z]” will select a range of uppercase alphabets.
  • β€œ[:lower:]” or a more specific β€œ[a-z]” will select a range of lowercase alphabets.
  • β€œ[:alpha:]” or a more specific β€œ[a-zA-Z]” will select a range of all the uppercase and lowercase alphabets.
  • β€œ[:digit:]” or a more specific β€œ[0-9]” will select a range of digits.
  • β€œ[:alnum:]” or a more specific β€œ[a-zA-Z0-9]” will select both uppercase and lowercase alphabets, including digits.
  • β€œ[:puncht:]” will select all the files with any of the β€œ! ” # $ % & β€˜ ( ) * + , – . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~” punctuation characters.

1. The following command will search for all the files that start with β€œi” or β€œe” characters.

$ ls [i,e]*

Output:

Listing all the files that include "i" or "e" characters

2. The following command will search for all the files that start with β€œfβ€œ, β€œgβ€œ, β€œhβ€œ, or β€œi” characters.

$ ls [f-i]*

Output:

Listing all the files that starts with assigned characters

3. The following command will search for all the files with any extension that start with the β€œpage” string and contain integers between the range of β€œ0” to β€œ9β€œ.

$ ls page[0-9].*

Output:

Listing the files with a "page" string and an integer between "0" to "9"

Exclamation Mark (!)

The (!) can be used to exclude a certain range of patterns when specified within the square brackets.

1. The following command will search for all the files except those with the name β€œ3β€œ.

$ ls [!3].txt

Output:

Listing all the files except for three

2. The following command will search for all the files that start with any name and extension but should not contain an integer in the range of β€œ0” to β€œ9β€œ.

$ ls *[!0-9].*

Output:

Excluding all the files with integer in name

3. The following command will search for all the lines that contain the β€œ2” in β€œfile.txtβ€œ.

$ grep [!2] file.txt

Output:

Listing all the lines that starts with integer two

Caret (^)

The (^) is pretty much the same as the previous wildcard character, but it changes behavior when specified inside or outside the square bracket.

For example, when it is specified outside the square bracket, then it will search for all the files with a given range of characters.

However, if it’s specified inside the square bracket, then it will highlight all the files with a given range of characters.

1. The following command will search for all the files except those that start with β€œ3β€œ.

$ ls [^3].txt

Output:

Listing all files with the exception of integer three

2. The following command will search for all the lines that start with β€œ2” in β€œfile.txtβ€œ.

$ grep ^[2] file.txt

Output:

Listing all of the lines that begin with the integer two

3. The following command will highlight all the lines that start with β€œ2” in β€œfile.txtβ€œ.

$ grep [^2] file.txt

Output:

Highlight all the lines that start with the integer two

Dollar Sign ($)

The ($) is used to specify the end characters, especially when you know the last character and want to search information based on the last character.

1. The following command will search for all the lines that end with β€œ0” in β€œfile.txtβ€œ.

$ grep 0$ file.txt

Output:

Listing all the years that end with zero

2. The following command will search for all the lines that end with β€œ20” in β€œfile.txtβ€œ.

$ grep 20$ file.txt

Output:

Listing all the years that end with twenty

Curly Bracket ({})

The ({}) is used to match filenames with more than one globbing pattern. Each pattern is separated with β€œ,” comma without any space.

1. The following command will search for all the text files (with four characters in the name) and PHP files.

$ ls {????.txt,*.php}

Output:

Listing all the text files (with five characters) and PHP files

2. The following command will search for all the text and PHP files.

$ ls {*.txt,*.php}

Output:

Listing all the text and PHP files

3. The following command will remove all the text and PHP files.

$ rm {*.txt,*.php}

Output:

Removing all the text and PHP files

Bonus Tip: Dot (.) and Multiple Dots (..)

This isn’t exactly what β€œfile globbing” means, but it’s still interesting to learn, so let’s start with a single β€œ.” dot, which stands for the current working directory.

If you specify this with the ls command, it will display the content of your current working directory.

$ ls .

Even if you do not specify the β€œ.” dot, you will get the same output, as shown.

Dot to list the directory content

If you specify the β€œ..” double dots, which stand for the parent directory, then it will list the content of one step backward in your directory structure.

$ ls ..

Output:

Double dots to list the directory content

Wrap Up

I hope this comes in handy for you in your Linux journey; also, don’t forget to check the Stdin, Stdout, and Stderr Streams in Linux.

If you have questions or queries related to this topic, then feel free to ask them in the comment section.

Till then, peace!

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.

3 thoughts on β€œWhat is File Globbing in Linux (How to Use It with Examples)”