Using Glob Pattern with the find Utility
Learn to use glob pattern with the find command.
We'll cover the following
What is a glob pattern?
A glob pattern is a search query that contains wildcard characters. Bash allows three wildcard characters: *
, ?
, and []
. The asterisk stands for any number of characters. A question mark means a single character of any kind.
Here is an example of glob patterns. The string README
matches all following patterns:
*ME
READM?
*M?
R*M?
Square brackets indicate a set of characters at a specific position. For example, the pattern [cb]at.txt
matches the cat.txt
and bat.txt
files. You can apply this pattern to the find
call this way:
find . -name "[cb]at.txt"
Applying glob patterns
Let’s put glob patterns into practice. Let’s suppose that we do not know the location of the Bash README
file. We should use the find
utility in this case. Start searching with the find
utility from the root directory. Now, we need a search condition.
It’s a common practice to store documentation in directories called doc
in Unix. Therefore, we can search files in these directories only. This way, we get the following find
call:
find / -path */doc/*
The command shows us all documentation files on all mounted disks. This is a huge list. We can shorten it with an extra search condition. There should be a separate directory for the Bash documentation. The directory is called bash
. Add this path as the second search condition. Then, we get the following command:
find / -path */doc/* -path */bash/*
The following find
call provides the same result:
find / -path */doc/* -a -path */bash/*
Use the following terminal to run the commands discussed in this lesson.
Get hands-on with 1400+ tech skills courses.