Find files in directory by wildcard matching in Linux

In this article, we will learn how to find all files in a directory based on wildcard matching in Linux.

Table Of Contents

Introduction

Sometime we will get a requirement to find all the files that starts with specific text or ends with specific extension. In that case we will get the files using wildcard matching.

Wildcards are just the special characters or symbols. You can use those wildcards with any command. There are three main wildcards in Linux.

  • (*) match the occurrences of one or more characters.
  • (?) match the single occurrence of any character
  • ([]) match the occurrence of any character which are in those square brackets. In that braces you can give any thing like symbols,alphanumerics.

Syntax :

find [-H] [-L] [-P] [-Olevel] [starting-point...] [expression]     

-Olevel: Enables query for optimization.

  • O1 – This is the default optimization level
  • O2 – Filters the file name and next the file type
  • O3 – Reorder the search automatically base don resources

Finding files based on wildcard matching

Lets create a directory with users and inside files in it. Some empty files in it.

Example 1: Search for a file with specific Filename

find -P O1 ./users -name test.json

The above example check in the folder named test and looks for the specific file named test.json. It will search recursively and prints the path of it.

Output:

./testactivate/test.json
./testactivate/routes/test.json

Example 2: Search for a file with specific pattern

find ./test -name *.json

The above example check in the folder named test and looks for the specific file with the extension as .json. It will search recursively and prints the path of it.

Output :

./testactivate/test.json
./testactivate/routes/test.json
./testactivate/routes/test2.json

Example 3: Search for a file with pattern

find . -name "??st*.json"

The above command will check filenames prefixed with any two characters followed by st but ending with one or more occurrence of any character.

Output :

./test.json
./testactivate/test.json
./testactivate/routes/pest.json
./testactivate/routes/rest2.json

Let’s see an another example,

find . -name "[pac]??[k]*"

The above command will check filename starting with [pac] any of these characters followed by any thing for next 2 characters later on [k] and lastly any occurrences.

Output :

./pend/package.json

Example 4: You can Negate the pattern using ^.

find . -name "[^pc]amp*"

The above command will check filename not to start with [pc] any of these characters followed by amp and lastly any occurrences.

Output :

./sample

Normally wildcard matching is case sensitive means abc will not match Abc or aBc. If we want to match both upper and lower case letters in names, then we include both characters in []. For example if we want to match abc and Abc then use the wildcard pattern [aA]bc*.

Finding files based on extended patterns in wildcard matching

Along with the wildcard patterns that come out of the box with most of the linux operating systems, bash allows us to use some additional patterns. We can enable those using below option

shopt -s extglob

After enabling we can use the below patterns

  • ?(pattern): Match up to one occurrence of the pattern
  • *(pattern): Match any number of occurrences of the pattern
  • +(pattern): Match at least one occurrence of the pattern
  • @(pattern): Match exactly one occurrence of the pattern
  • !(pattern): Match everything except the pattern

Lets looks different examples on using these extended patterns

Example: 1

If we want to display all pdf files in a directory that has zero or one occurrence of “a”, then we can use the below command

ls  ?(a).pdf

Output :

a.pdf  .pdf

Example: 2

If we want to display all pdf files in a directory that has zero or more occurrence of “a”, then we can use the below command

ls  *(a).pdf

Output :

aaa.pdf  aa.pdf  a.pdf  .pdf

Example: 3

If we want to display all pdf files in a directory that has one or more occurrence of “a”, then we can use the below command

ls  +(a).pdf

Output :

aaa.pdf  aa.pdf  a.pdf

Example: 4

If we want to display all pdf files in a directory that has only one occurrence of “a”, then we can use the below command

ls  @(a).pdf

Output :

a.pdf

Finding files based on patterns

We can also specify a range of characters using square brackets. This will help us to find all files matching that pattern. Lets look at some of the patterns that we can use.

  • [[:alnum:]] – Matches any alphanumeric character
  • [[:space:]] – Matches any whitespace character
  • [![:space:]] – Matches any character that is not whitespace
  • [[:digit:]] – Matches any digit.

Lets look at one of the example to find all files that contains numbers in the filename.

ls  [[:digit:]].pdf

Output :

1.pdf

Summary

That’s all for now. We learnt about finding files in a directory that matches the wildcard pattern. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top