What is File Globbing in Linux?

Linux TLDR
Last Updated:
Reading time: 5 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.

Note: 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
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.

Note: 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.

Example 1:

The following command will search for all the files with the “.php” extension.

$ ls *.php

Output:

Listing all PHP files
Listing all PHP files

Example 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
Listing all the files with the name index

Example 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
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.

Example 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
Listing all the PHP files with names containing five characters

Example 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
Listing all the files with the last four unknown characters

Example 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
Listing any files with the last four unknown characters

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.

Example 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
Listing all the files that include “i” or “e” characters

Example 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
Listing all the files that start with the assigned characters

Example 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"
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.

Example 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
Listing all the files except for three

Example 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
Excluding all the files with integer in name

Example 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
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.

Example 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
Listing all files with the exception of integer three

Example 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
Listing all of the lines that begin with the integer two

Example 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
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.

Example 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
Listing all the years that end with zero

Example 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
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.

Example 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
Listing all the text files (with five characters) and PHP files

Example 2:

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

$ ls {*.txt,*.php}

Output:

Listing all the text and PHP files
Listing all the text and PHP files

Example 3:

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

$ rm {*.txt,*.php}

Output:

Removing all the text and PHP files
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
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
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.

2 thoughts on “What is File Globbing in Linux?”