Difference between ? and * in globbing?

Joined
Apr 16, 2023
Messages
149
Reaction score
16
Credits
1,460
?
It represents a single character
*

It represents zero or more characters


The difference:
say there are these files in system.
file1
file2
file3
file4
file10

You do rm -f file?
It will not delete file10, meanwhile if you do rm -f file*, it'll delete all files.

What I'm trying to understand is its other use cases that I don't yet understand but you guys know about.
 


Unix-like shell, the characters "?" and "*" serve different purposes:

  1. "?" (Question Mark):
    • The "?" (question mark) is a wildcard character that matches any single character. It is used to represent a single character of any type at a specific position in the filename.
    • For example, if you have files named "file1.txt" and "file2.txt," you can use the pattern "file?.txt" to match both of them. The "?" matches any single character in the position where it appears in the pattern.
  2. "*" (Asterisk):
    • The "*" (asterisk) is a wildcard character that matches zero or more characters, including no characters. It is used to represent any sequence of characters in a filename.
    • For example, if you have files named "file.txt," "file123.txt," and "fileX.txt," you can use the pattern "file*.txt" to match all of them. The "*" matches zero or more characters in the position where it appears in the pattern.
Here are a few more examples to illustrate the difference:

  • "fil?.txt" would match "file.txt" but not "files.txt" because it expects a single character in place of "?".
  • "file*.txt" would match "file.txt," "file123.txt," and "fileX.txt" because it allows for zero or more characters in place of "*".
In summary, "?" matches exactly one character, while "*" matches zero or more characters in globbing patterns. These wildcard characters are handy for specifying flexible patterns when searching for files or directories in the command line.
 

Members online


Top