An example of how to use the Unix/Linux find command to locate files with different extensions

By Alvin J. Alexander, devdaily.com

Someone asked me the other day how they could search for files with different names with one 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.

Here then is 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 Unix 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.

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.


devdaily logo