File globbing in Linux - GeeksforGeeks

30 Jan, 2021
Before we move to file globbing, let’s understand what are wildcard patterns, these are the patterns containing strings like ‘?’, ‘*’ 
File globbing is the operation that recognizes these patterns and does the job of file path expansion. 
See the below example for clear understanding, 
 
path expansion for ‘*’
If you observe the image above, I created several directories whose starting characters are HELLO and hello, and then tried to delete these directories. 
When I used rm -rf hello*, it deleted the directories hello1, hello2, hello3, the ‘*’ symbol used after ‘hello’ recognizes the first characters as ‘hello’ and then zero or more occurrences of any other characters. 
Examples using other wildcard characters : 
1) asterisk (*) * is used to match any number of characters(zero or more), to understand more you can refer to the example taken above. 
2) question mark(?) ? is used to match exactly one character.  
In the above image, you can observe that ‘?’ can match exactly one character and is used at the end of the line. So using ‘hello?’ will match all files or directories whose starting characters are ‘hello’ and it will recognize one more character. 
3) Square Brackets [] Square brackets are used to match the characters inside [], refer below image,  
[] can be used to match exact characters or you can also specify a range, like in the above example, using ‘hello[1-5]’ will display all files and directories starting with ‘hello’, then the next character can be a number from 1 to 5. 
4) exclamation mark (!) ! is used to exclude characters from the list that is specified within the square brackets. 
For example:  
ls hello[!3]

It will display the directories starting with hello, ending with any character but not 3
5) Named character classes ([[:named:]])
It is used to print named values. Their interpretation depends on the LC_CTYPE locale; Some of them are listed below.
‘[:alnum:]’ : Prints all those files having alphabets and digits. Both lower and uppercases are considered.
‘[:alpha:]’ : Prints all those files having alphabets only. Both lower and uppercases are considered.
‘[:digit:]’ : Prints all those files having digits.
‘[:lower:]’: Prints all those files having lower-case letters.
‘[:punct:]’ : Prints all those files having punctuation characters.  Will search for ! ” # $ % & ‘ ( ) * + , – . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~.
‘[:space:]’: Prints all those files having space characters.
‘[:upper:]’: Prints all those files having lower-case letters.
Note: They can be used with asterisk (*) ,question mark(?) or square brackets([]) to generate useful outputs. 
For example: 
ls [[:alpha:]]* : Will display the directories starting with a alphabet (either in lower or uppercase) and ending 
          with any characters.
ls *[[:alnum:]]*.* : Will display the files (of any type) containing a alphabet or a digit but may start 
             or end with any length of characters.
ls *[[:digit:]] : Will display the directories which may start with any length of characters but ending with a digit.
ls ?[[:lower:]] : Will display the directories starting with exactly one character and ending with a lowercase 
          character.
ls *[[:upper:]]* : Will display the directories containing a alphabet or digit which may start or end with any 
          length of characters.
Note: Any combinations of *, ? Can be used with any named class.
yashbeersingh42
Articles
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Const keyword in C++
What's the difference between Scripting and Programming Languages?
Service-Oriented Architecture
Amazon’s most frequently asked interview questions | Set 2
Must Do Questions for Companies like TCS, CTS, HCL, IBM ...
TCP Server-Client implementation in C
ZIP command in Linux with examples
tar command in Linux with examples
UDP Server-Client implementation in C
curl command in Linux with Examples