With the matching patterns, we can easily and efficiently find the files or directories we need.

Preparation

Let’s prepare some test files first before we start:

1
touch 1.txt 2.doc 3.html 1.c 2.c 3.c file1.txt file2.doc file3.html 1file.txt 2file.doc 3file.html

Special Pattern Characters

Match any string

We can use the * character to match any string.

Match any file that starts with file

1
ll file*
img

Finds any file with filename begin with file

Match any file that ends with .txt

1
ll *.txt
img

Finds any with filename ends with .txt

Match any character

We can use the ? character to match any character.

Match any file with a 1-character filename

1
ll ?.*
img

Finds any file with a 1-character filename

Match any file with a 4-character extension

1
ll *.????
img

Finds any file with a 4-character extension

Match any one of enclosed characters

We can use the [] range expression to match any one of enclosed characters.

Match any file that starts with 1, 2 or f

1
ll [12f]*
img

Finds any file that starts with 1, 2 or f

Match any file that starts with 1, 2 or 3

1
ll [1-3]*
img

Finds any file that starts with 1, 2 or 3

Match any file that starts without 1, 2 or 3

1
ll [^1-3]*
img

Finds any file that starts without 1, 2 or 3

References 3.5.8.1 Pattern Matching

Buy me a coffeeBuy me a coffee