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.
For example, the following command will list all the files that share the common β.txt
β extension using the (*
) operator.
$ ls *.txt
Output:
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.
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:
2. The following command will search for all the files with any extension, but the name should be βindex
β.
$ ls index.*
Output:
3. The following command will search for all the PHP files that start with the βpage
β string.
$ ls page*.php
Output:
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:
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:
3. The following command is identical to the previous one, but it will list all the files with any extension.
$ ls food????.*
Output:
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:
2. The following command will search for all the files that start with βf
β, βg
β, βh
β, or βi
β characters.
$ ls [f-i]*
Output:
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:
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:
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:
3. The following command will search for all the lines that contain the β2
β in βfile.txt
β.
$ grep [!2] file.txt
Output:
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:
2. The following command will search for all the lines that start with β2
β in βfile.txt
β.
$ grep ^[2] file.txt
Output:
3. The following command will highlight all the lines that start with β2
β in βfile.txt
β.
$ grep [^2] file.txt
Output:
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:
2. The following command will search for all the lines that end with β20
β in βfile.txt
β.
$ grep 20$ file.txt
Output:
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:
2. The following command will search for all the text and PHP files.
$ ls {*.txt,*.php}
Output:
3. The following command will remove all the text and PHP files.
$ rm {*.txt,*.php}
Output:
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.
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:
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!
The following command will search for all the lines that start with β2β in βfile.txtβ.
$ grep [!2] file.txt
https://linuxtldr.com/file-globbing-linux/
I donβt think your statement is correct.
Thanks for noticing that, Tim. Article has been updated now.
grep [!2] searches for lines containing β!β or β2β. I donβt understand why this example is included in an article about globbing since grep uses regular expressions.