Question mark – (?)
‘?’ is used to match any single character. You can use ‘?’ for multiple times for matching multiple characters.
Example-1:
Suppose, you want to search those text filenames whose names are 4 characters long and extension is .txt. You can apply globbing pattern by using ‘?’ four times to do this task.
Find out the list of all files and folder of the current directory.
Run the following command search those files whose names are four characters long and unknown.
Example-2:
Suppose, you want to search those document files whose names are 8 characters long, first 4 characters are f, o, o and t and extension is doc. Run the following command with globbing pattern to search the files.
Example-3:
Suppose, you know the filename is ‘best’ and extension is 3 characters long, but don’t know the extension. Run the following command by using ‘?’ to search all files with the name ‘test’ having any extension of three characters long.
Asterisk – (*)
‘*’ is used to match zero or more characters. If you have less information to search any file or information then you can use ‘*’ in globbing pattern.
Example -1:
Suppose, you want to search all files of ‘pl’ extension. Run the following command using ‘*’ to do that task.
Example-2:
Suppose, you know the starting character of the filename only which is ‘a’. Run the following command using ‘*’ to search all files of the current directory whose names are started with ‘a’.
Example-3:
You can apply ‘*’ in bash script for various purposes without searching files. Create a bash file named ‘check.sh’ with the following script. Here, when the user will type ‘y’ or ‘Y’ or ‘yes’ or ‘Yes’ then ‘confirmed’ will print and when the type will type ‘n’ or ‘N’ or ‘no’ or ‘No’ then ‘Not confirmed’ will print.
echo "Do you want to confirm?"
read answer
case $answer in
[Yy]* ) echo "confirmed.";;
[Nn]* ) echo "Not confirmed.";;
*) echo "Try again.";;
esac
Run the script.
Square Bracket – ([])
‘[]’ is used to match the character from the range. Some of the mostly used range declarations are mentioned below.
All uppercase alphabets are defined by the range as, [:upper:] or [A-Z] .
All lowercase alphabets are defined by the range as, [:lower:] or [a-z].
All numeric digits are defined by the range as, [:digit:] or [0-9].
All uppercase and lower alphabets are defined by the range as, [:alpha:] or [a-zA-z].
All uppercase alphabets, lowercase alphabet and digits are defined by the range as, [:alnum:] or [a-zA-Z0-9]
Example -1:
Run the following command to search all files and folders whose name contains p or q or r or s.
Example-2:
Run the following command to search all files and folders whose name starts with any digit from 1 to 5.
Caret – (^)
You can use ‘^’ with square bracket to define globbing pattern more specifically. ‘^’ can be used inside or outside of square bracket. ‘^’ is used outside the square bracket to search those contents of the file that starts with a given range of characters. ‘^’ is used inside the square bracket to show all content of the file by highlighting the lines start with a given range of characters . You can use different types of globbing patterns for searching particular content from a file. ‘grep’ command is used for content searching in bash. Suppose, you have a text file named ‘list.txt’ with the following content. Test the following examples for that file.
4000
Banana
700
Orange
850
Pear
9000
Jackdruit
Example – 1:
Run the following command to search those lines from list.txt file that starts with P or Q or R.
Example – 2:
Run the following command to highlight those lines from list.txt file that starts with A or B or C.
Exclamatory Sign – (!)
You can use ‘!’ inside the range pattern. It works same as the use of ‘^’ symbol outside the range pattern. Some examples of using ‘!’ sign are given below.
Example – 1:
Run the following command to show those lines from list.txt file that starts with ‘P’ or Q or R.
Example – 2:
Run the following command to show those lines from list.txt file that starts with any digit from 4 to 8.
Dollar Sign – ($)
‘$’ is used to define the ending character. If you know want to search information based on last character then you can use ‘$’ in globbing pattern.
Example – 1:
Run the following command to search those lines from list.txt file that ends with ‘a’.
Example – 2:
Run the following command to search those lines from list.txt file that end with the number 50.
Curly bracket – ({})
‘{}’ can be used to match filenames with more than one globbing patterns. Each pattern is separated by ‘,’ in curly bracket without any space. Some examples are given below.
Example – 1:
Run the following command to search those files whose names are 5 characters long and the extension is ‘sh’ or the last two characters of the files are ‘st’ and the extension is ‘txt’.
Example – 2:
Run the following command to delete all files whose extensions are ‘doc’ or ‘docx’.
Pipe– ( | )
‘|’ sign is also used for applying more than one condition on globbing pattern. Each pattern is separated by ‘|’ symbol in the command.
Example – 1:
Run the following command to search those filenames which are starting with character ‘a’ and has the extension ‘bash’ or ‘sh’.
Example – 2:
Create a bash file named ‘menu.bash’ and add the following script. If the user type 1 or S then it will print “Searching text”. If the user type 2 or R then it will print “Replacing text”. If the user type 3 or D then it will print “Deleting text”. It will print “Try again” for any other input.
echo "Select any option from the menu:"
read answer
case $answer in
1 | S ) echo "Searching text";;
2 | R ) echo "Replacing text";;
3 | D ) echo "Deleting text";;
*) echo "Try again.";;
esac
Run the script.
CONCLUSION
Some of the most commonly used globbing patterns are explained in this tutorial by using very simple examples. I hope after practicing the above examples, the concept of globbing will be clear to you and you will be able to apply it in bash commands and scripts successfully.
For more info check this video: