Using wildcards in Linux

Reading Time: 2 minutes

Wildcards (also referred to as meta characters) are symbols or special characters that represent other characters. You can use them with any command such as ls command or rm command to list or remove files matching a given criteria, receptively.

There are three main wildcards in Linux:

  • An asterisk (*) – matches one or more occurrences of any character, including no character.
  • Question mark (?) – represents or matches a single occurrence of any character. so, it matches exactly one charecter
  • Bracketed characters ([ ]) – matches any occurrence of character enclosed in the square brackets. It is possible to use different types of characters (alphanumeric characters): numbers, letters, other special characters etc.

We use these wildcards typically in ls and rm commands for example to list all txt or html files in the folder:

ls *.txt *.html

to delete all txt files in the folder:

rm *.txt

Using first example we can get details of all files that start with a through c and have 3 digits followed by .txt

file [a-c][0-9][0-9][0-9].txt

In the second example, we delete 2020 files that can start with any letter or number and the second to fifth character as 2020

rm ?2020*.txt

In the last example, to list all files that start with a or c and have exactly 3 characters after them and the third of those is a numeric, use

ls [ac]??[0-9].txt

Leave a comment