Javatpoint Logo
Javatpoint Logo

Linux File Globbing

Globbing is also known as path name expansion. To learn about file globbing first we need to know about wildcards.

Wildcards pattern are the strings containing characters like '?', '[', '*'. It performs action on more than one file having same pattern or to find part of a phrase in a text file. Shell uses wildcards for file globbing.

Globbing is an operation that recognizes the wildcard pattern and expands it into its path name.


*asterisk

The asterisk is interpreted as the sign to generate matching file names. It is placed at the end of a line. It matches the combination by any number of characters.

Example:

Linux File Globbing1

Look at the above snapshot, * has displayed the matching file names in all the examples.


? question mark

You can also use question mark sign in place of asterisk to generate matching file names. It is placed at the end of a line. It matches the combination by exactly one character.

Example:

Linux File Globbing2

Look at the above snapshot, ? has displayed the exact matching file names in all the examples.


[] Square Brackets

Square brackets are also used to generate matching file names inside the brackets and the first subsequent. Order inside the square bracket doesn't matter. It matches the combination by exactly one character.

Example:

Linux File Globbing3

! exclamation mark

Exclamation mark excludes characters from the list within the square bracket. And you can use the combination of asterisk (*), question mark (?) and square bracket [].

Example:

Linux File Globbing4

Look at the above snapshot, we have used diffrent combinations with exclamation mark.


Ranges [a-z] and [0-9]

You can also specify ranges according to your need.

Example:

Linux File Globbing5

Globbing Prevention

The command echo * will print * when a directory is empty. But if not empty, them it will print the files. To prevent it, special characters can be used like backward slash (\), single quote (') and double quote (").

Syntax:

Linux File Globbing6

Look at the above snapshot, when directory 'Download' was empty, * is printed. But when it contains files then list is printed. This ic lrevented by using special characters.





Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


B.Tech / MCA