Linux find command - multiple filename patterns

Someone asked me the other day how they could search for files with different names with one Linux find command. They wanted to create a list of all files that ended with the extensions .class and .sh.

Although this is actually very easy to do with the find command, the syntax is obscure and probably not well documented, so let's look at how to do this.

Linux find command - two filename patterns

Here's an example of how to search in the current directory and all subdirectories for files ending with the the extensions .class and .sh using the find command:

find . -type f \( -name "*.class" -o -name "*.sh" \)

That should work on all types of Unix systems, including vanilla Unix, Linux, BSD, freeBSD, AIX, Solaris, and Cygwin.

Finding files with three different filename extensions

While I'm in the neighborhood, here is an example of how to search the current directory for files that end in any of three different files extensions:

find . -type f \( -name "*cache" -o -name "*xml" -o -name "*html" \)

(FWIW, I did that one on a Mac OS/X machine.)

In these examples I always include the -type f option, which tells find to only display files, and specifically not directories. That's not always necessary, but when someone tells me they want to find files, I always include it.

Post new comment

The content of this field is kept private and will not be shown publicly.