Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
A long time ago, the /etc/glob program in UNIX V6 expanded wildcard patterns which quickly evolved into a shell built-in. So through this blog, we will understand the concept of Linux File Globbing, some of the most commonly used globbing patterns and Globbing Prevention.
The term "glob" refers to patterns for matching file and directory names based on wildcards. Globbing is defining one or more glob patterns and producing files from inclusive or exclusive matches.
So, are you ready to learn Linux File Globbing?
Linux File Globbing
Before we move to file globbing, let's understand what wildcard patterns are. Wildcard patterns contain strings like '?', '*,' '!', or '[].' It acts on more than one file having the same pattern or phrase in a present text file. Shell uses wildcards for file globbing.
Globbing, also known as path name expansion, is the process of expanding a wildcard pattern into a list of the pathnames that match the pattern. It is mainly required to match filenames or searching for content in a file.
See the below-discussed wildcard patterns for a clear understanding.
*(Asterisk)
The asterisk will match the combination by any number of characters(zero or more characters) and give the result accordingly. It is written at the end of a line.
Syntax
ls cod*
Example
In the above example we are creating some files using the ‘touch’ command. Now suppose, you want to search a file or a folder starting from ‘a’ or ‘cod’ filename. You can simply apply globbing pattern by using ‘*’ a single time at the end of the line.
? (Question Mark)
Instead of using an asterisk, we can also use the question mark sign(?) to generate matching file names. It can be written anywhere in the line depending upon the missing letter of the file. It matches the combination by exactly one character. Underscore(_) also works same as question mark.
Syntax
ls cod?
Example
In the above screenshot, we are creating some files using the ‘touch’ command. Now suppose, you want to search all the files or folders having only three letters such as ‘anu’ or ‘cod’, in that case you can simply type ‘???’. You can simply apply globbing pattern by using ‘?’ multiple times anywhere in the line and run the query as given in above example.
[] (Square Brackets)
Square brackets is used to match the combination by exactly one character having a particular range. They are used to generate matching file names inside the brackets and the first subsequent. Order inside the square bracket doesn't matter.
Syntax
ls codingninjas[123]
Example
In the above snapshot we have saved some files and now with the help of square brackets, we can search for all files and folders whose name starts or ends with any digit, capital or small alphabet.
^ (Caret)
Caret can be used with square brackets to define globbing wildcard patterns more specifically. It can be placed either inside or outside of the square bracket.
Syntax
grep ‘^[n]’ codingninja.txt
Example
In the above snapshot we have saved a file named codingninja.txt, inside which we have written some content. Now with the help of caret, we can search for all the characters inside the files.
In the above screenshot we searched for all the characters starting with ‘n’ or ‘c’, using caret symbol.
[a-z] and [0-9] (Ranges)
Using square brackets you can also specify ranges(either digits or alphabets) according to your need. Where you can specify your requirements as-
For all numeric digits, you can define- [:digit:] or [0-9].
For all lowercase alphabets, you can write- [:lower:] or [a-z].
For all uppercase alphabets you can write- [:upper:] or [A-Z] .
In the case of all uppercase and lowercase alphabets, the range can be defined as- [:alpha:] or [a-zA-z].
And for the combination of uppercase alphabets, lowercase alphabet and digits, the range can be specified as- [:alnum:] or [a-zA-Z0-9]
Syntax
ls CodingNinjas[A-Z]
Example
In the above snapshot, we have used different combinations of alphabets and digits inside the square brackets to search for the files or folders.
! (Exclamation Mark)
The exclamation mark excludes characters from the list within the square bracket. And you can use the combination of an asterisk (*), question mark (?), and square bracket []. ! is used to exclude characters from the list that is specified within the square brackets.
Syntax
ls anu[!2]
Example
In the above screenshot, we are creating some files using the ‘touch’ command. Now suppose, you want to to exclude characters from the list that is specified within the square brackets, in that case you can simply type use exclamation mark inside the bracket as given in the above example.
{} (Curly Bracket )
The curly brackets helps in matching the filenames with more than one globbing patterns. In curly brackets, each pattern is separated by ‘,’ without any space. Some examples are given below.
Syntax
ls {*.txt,*u,[c]*}
Example
In the above example we have used different combinations of globbing patterns.
Globbing Prevention
The command echo * will print * when a directory is empty. But if not empty, then it will print the files. To prevent it, special characters can be used, like backward slash (\), single quote ('), and double quote (").
Syntax
echo \*
echo '*'
echo "*"
Example
This was all about Linux File Globbing, now it’s time for some frequently asked questions on Linux File Globbing.
Frequently Asked Questions
How can we stop Linux from globbing?
Globbing can be prevented using quotes or by escaping the special characters, as shown in this screenshot.
What are globbing characters?
A glob is a string of literal and/or wildcard characters used to match file paths. Locating files on a filesystem using one or more globs is known as globbing.
What is a glob in coding?
In computer programming, glob (/ɡlɑːb/) patterns specify sets of filenames with wildcard characters.
What is bash?
BASH enables users to create commands and trigger events. It's an interpreted, non-compiled process that may run in a terminal window. BASH can read commands from shell scripts and execute them.
What are the two types of Linux user modes?
The two types of Linux User modes are Command Line and GUI.
Conclusion
In this article, we have discussed Linux File Globbing. Along with this, we have also discussed some of the most commonly used globbing wildcard patterns with examples. Along with this, we have also discussed Globbing Prevention. To enhance your knowledge of Linux you can refer to these articles-