When we need to search for anything using shell commands then we need to define a pattern for searching. Wildcard characters are used to define the pattern for searching or matching text on string data in the bash shell. Another common use of wildcard characters is to create regular expressions. How you can use different types of wildcard characters for searching files is shown in this tutorial.

Wildcard Characters

The three main wildcard characters are,

  • Star or Asterisk (*)
  • Question mark (?)
  • Square brackets ([])

Asterisk (*) is used to search for a particular character(s) for zero or more times. Question mark (?) is used to search for a fixed number of characters where each question mark (?) indicates each character. Square brackets are used to match with the characters of a defined range or a group of characters. The uses of these characters are shown in the next part of this tutorial.

Use of Asterisk (*)

Asterisk (*) can be used in various ways with shell commands for searching files. Different use of the asterisk (*) are shown in the following examples.

Example – 1: Searching Specific File with Filename and ‘*’

‘ls’ command is used to find out the list of files and folders of the current directory. ‘ls a*’ command will search and print all filenames of the current directory that starts with the character, ‘a’.

Output:

According to the following output, 12 files exist in the current directory that starts with the character, ‘a’.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Bash-Wildcard-1.png" data-lazy- height="623" src="data:image/svg xml,” width=”1177″>

Example – 2: Searching File with Particular Extension and ‘*’

You can search any file by using the asterisk (*) and the file extension. If you want to search all files with the ‘.txt’ extension from the current directory then run the following command from the terminal. Here, the filename can be any character(s) and any number of characters.

You can also search files of different extensions by using the asterisk (*). The following command will search any files with extension ‘.sh’ or ‘.txt’

Output:

The output shows the list of all existing files of the current directory with the extension ‘.sh’ and ‘.txt.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Bash-Wildcard-2.png" data-lazy- height="220" src="data:image/svg xml,” width=”1162″>

Example – 3: Removing File by Partial Match and ‘*’

You can use the asterisk (*) for matching any filename partially. The following command will remove the file which contains the ‘test’ word in any part of the filename.

Output:

The following output shows that two files exist in the current directory that contains the word ‘test’ and these files have been removed after executing the ‘rm’ command.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Bash-Wildcard-3.png" data-lazy- height="244" src="data:image/svg xml,” width=”1170″>

Use of Question Mark (?)

When you know the exact numbers of characters that you want to search then the question mark (?) wildcard can be used. The following examples show the different use of question mark (?) wildcard.

Example – 1: Searching File with Filename and ‘?’

Suppose, the file extension, the total number of characters of a file, and some characters of the file are known, then you can use this wildcard to search the file. The first command will search those files which have the extension ‘.txt’. The second command will search those files which are four characters long, the last character is ‘t’ and the extension of the file is ‘.txt’.

Output:

The output shows that 7 files exist in the current directory with the ‘.txt’ extension and one text file exists that is 4 characters long and the last character is ‘t’.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Bash-Wildcard-4.png" data-lazy- height="222" src="data:image/svg xml,” width=”1182″>

Example -2: Searching File with Extension and ‘?’

Suppose, you know the filename and the total number of characters of the file extension then you can use the question mark (?) wildcard to search the file. The following command will search the file with filename ‘hello’ and the extension is three characters long.

Output:

According to the output, two files exist in the current directory that has the name, ‘hello’ and the extension is three characters long.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Bash-Wildcard-5.png" data-lazy- height="218" src="data:image/svg xml,” width=”1176″>

Use of Square Brackets ([])

Different ranges of characters or group of characters can be used within square brackets ([]) for searching files based on the range.

Example -1: Search Files of Any Extension with Two Ranges Values

The following command will search any file whose name contains any character within ‘a-p’ and any digit within ‘0-5’ and the file extension can be any character.

Output:

The following output shows the list of all files that match the pattern used in the command.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Bash-Wildcard-6.png" data-lazy- height="220" src="data:image/svg xml,” width=”1169″>

Example-2: Search File Starts with a Particular Character and is Followed by Another Character

In this example, the second command will search the filenames starting with any of these characters [afgh] followed by the character ‘o’ with any extension.

Output:

The following output shows the list of all files that match the pattern used in the command.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Bash-Wildcard-7.png" data-lazy- height="209" src="data:image/svg xml,” width=”1174″>

Example-3: Search Filenames with the Prefix Value

The following command will match those files whose name starts with ‘fn’ and is followed by a number within 0 to 5.

The following command will match those files that contain the name, ‘hello’ and the extension contains any character from t to z.

Output:

The following output shows the list of all files that match with the pattern used in the commands.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Bash-Wildcard-8.png" data-lazy- height="273" src="data:image/svg xml,” width=”1177″>

The character class can be used in the third brackets to match the filename or the particular string in the script. Different classes that can be used in the pattern has described below.

Class name Purpose
[:alpha:] It is used to match with any uppercase and lowercase letter and equivalent to [a-zA-Z].
[:digit:] It is used to match with any digits and is equivalent to [0-9].
[:alnum:] It is used to match any alphabet and digit. It is equivalent to [a-zA-Z].
[:upper:] It is used to match with uppercase latter only and equivalent to [A-Z]
[:lower:] It is used to match with uppercase latter only and equivalent to [a-z]
[:blank:] It is used to match with space and tab.
[:print:] It is used to match with printable characters.
[:cntrl:] It is used to match with non-printable characters.
[:space:] It is used to match with while-space.
[:xdigit:] It is used to match with hexadecimal digits.
[:ascii:] It is used to match with ASCII characters.
[:word:] It is used to alphanumeric characters including underscore(_).

The uses of four mostly used classes have explained in the following four examples.

Example-1: Use of [:alpha:] in Bash Script

The following script will check that the input taken from the user contains alphabetic characters only.

#!/bin/bash

echo -n “What is your name? “

# Assign input value to the variable

read name

# Check the input value contains the alphabet only

if [[ $name  =~ ^[[:alpha:]] ]]; then


    echo “Your name is $name.”

else


    echo “Enter alphabetic character only.”

fi

Output:


The following output shows the message for invalid and valid input.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Bash-Wildcard-9.png" data-lazy- height="271" src="data:image/svg xml,” width=”1178″>

Example-2: Use of [:digit:] in Bash Script

The following script will check that the input taken from the user contains numeric characters only.

#!/bin/bash

echo -n “Enter your ID: “

# Assign input value to the variable

read id

# Set the ID value

ID=“ID-$id

# Set the pattern using [[:digit:]] class

regex=“ID-[[:digit:]] “

# Check the input value contains the number only

if [[ $ID  =~ $regex ]]; then


    echo $ID is valid.”

else


    echo $ID is invalid.”

fi

Output:

The following output shows the message for invalid and valid input.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Bash-Wildcard-10.png" data-lazy- height="266" src="data:image/svg xml,” width=”1178″>

Example-3: Use of [:alnum:] in Bash Script

The following script will check that the input taken from the user contains alphanumeric characters only.

#!/bin/bash

echo -n “Enter your address: “

# Assign input value to the variable

read addr

# Check the input value contains the alphabet and number only

if [[ $addr  =~ ^[[:alnum:]] ]]; then


    echo “Address is valid.”

else


    echo “Address is invalid.”

fi

Output:

The following output shows the message for invalid and valid input.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Bash-Wildcard-11.png" data-lazy- height="270" src="data:image/svg xml,” width=”1175″>

Example-4: Use of [:upper:] in Bash Script

The following script will check that the input taken from the user contains capital letters only.

#!/bin/bash

echo -n “Enter your code in uppercase letter: “

# Assign input value to the variable

read code

# Check the input value contains all uppercase latter

if [[ $code  =~ ^[[:upper:]] ]]; then


    echo “Code is correct.”

else


    echo “Code can contain uppercase only.”

fi

Output:

The following output shows the message for invalid and valid input.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Bash-Wildcard-12.png" data-lazy- height="265" src="data:image/svg xml,” width=”1169″>

Conclusion

The basic uses of wildcard characters for searching files in the terminal and validating data using bash script have been shown in this tutorial. I hope the examples shown here will help the Linux users to write regular expression patterns properly to match any content for different purposes.