Chapter 2. Linux Key Commands

Wildcard

Wildcard
Tag:

Wildcard is a symbol (or a set of symbols) representing other characters. It is generally used for substituting any string or character.

Wildcard vs. Regular Expression

Wildcard and regular expression look similar but they are different things. The definition of wildcard characters can differ by software while the definition of meta-characters in the regular expression is typically the same across different software.

* (asterisk) and ? (question mark)

* (asterisk) and ? (question mark) are the most commonly used wildcards. * represents any single or multiple characters while ? represents any single character.

For example,

  • ca* : "cat", "can", "caterpillar", "capital", etc.
  • c?t : "cat", "cut", "cot", etc.

Wildcard Use Cases on Linux

Shorten file or directory names in the command execution

You can use the wildcard to specify a file or directory with a long name. For example, when there is a file named wildcard_regular_expression_differences.txt and there are no other files starting with “w” in the same directory, you can use w* to specify the file. Below is an example of a command that specifies the file name starting with w*.

Command Line - INPUT
cat w*

Find command

The wildcard can be useful when you forget the entire name of a file and want to locate it.

For example, to search the documents whose name starts with "sample", run the following command.

Command Line - INPUT
find ~ -name sample*

If you run the command in the same directory structure as was used in the previous section, the results will be like the one below.

Command Line - RESPONSE
/home/ubuntu/test/sample_1.txt
/home/ubuntu/test/test_sub/sample_2.txt

Wildcard is a symbol (or a set of symbols) representing other characters. It is generally used for substituting any string or character.

Wildcard vs. Regular Expression

Wildcard and regular expression look similar but they are different things. The definition of wildcard characters can differ by software while the definition of meta-characters in the regular expression is typically the same across different software.

* (asterisk) and ? (question mark)

* (asterisk) and ? (question mark) are the most commonly used wildcards. * represents any single or multiple characters while ? represents any single character.

For example,

  • ca* : "cat", "can", "caterpillar", "capital", etc.
  • c?t : "cat", "cut", "cot", etc.

Wildcard Use Cases on Linux

Shorten file or directory names in the command execution

You can use the wildcard to specify a file or directory with a long name. For example, when there is a file named wildcard_regular_expression_differences.txt and there are no other files starting with “w” in the same directory, you can use w* to specify the file. Below is an example of a command that specifies the file name starting with w*.

Command Line - INPUT
cat w*

Find command

The wildcard can be useful when you forget the entire name of a file and want to locate it.

For example, to search the documents whose name starts with "sample", run the following command.

Command Line - INPUT
find ~ -name sample*

If you run the command in the same directory structure as was used in the previous section, the results will be like the one below.

Command Line - RESPONSE
/home/ubuntu/test/sample_1.txt
/home/ubuntu/test/test_sub/sample_2.txt
Tag: