Open In App

File globbing in Linux

Improve
Improve
Like Article
Like
Save
Share
Report

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.


Last Updated : 31 Oct, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads