Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Linux File Globbing
3.
*(Asterisk)
3.1.
Syntax
3.2.
Example
4.
? (Question Mark)
4.1.
Syntax
4.2.
Example
5.
[] (Square Brackets)
5.1.
Syntax
5.2.
Example
6.
^ (Caret)
6.1.
Syntax
6.2.
Example
7.
[a-z] and [0-9] (Ranges)
7.1.
Syntax
7.2.
Example
8.
! (Exclamation Mark)
8.1.
Syntax
8.2.
Example
9.
{} (Curly Bracket )
9.1.
Syntax
9.2.
Example
10.
Globbing Prevention
10.1.
Syntax
10.2.
Example
11.
Frequently Asked Questions
11.1.
How can we stop Linux from globbing?
11.2.
What are globbing characters?
11.3.
What is a glob in coding?
11.4.
What is bash?
11.5.
What are the two types of Linux user modes?
12.
Conclusion
Last Updated: Mar 27, 2024
Easy

Linux File Globbing

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

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. 

Linux Globbing

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

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.

What is 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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

*(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

asterisk working

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

? question mark working

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

square bracket working

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

codingninja file

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.

caret ss

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

ranges working

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

exclamation mark

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

curly braces working

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

globbing prevention

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-

Click here, Amazon Hirepro

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Happy Coding!

Previous article
Linux Screen Command
Next article
Linux - Decision Making
Live masterclass