How to find multiple patterns with GNU findutils

Actually, searching for multiple patterns should be a trivial task. Find provides a -o operator (and many others) that lets you combine multiple expressions.

A simple Example: You want to find all files in the current directory whose filename extension are either .c or .h

$ find . \( -name "*.c" -o -name "*.h" \) -print

This is not limited to the -name test but can be combined with any other test (like -perm, -size, -type, etc.)

But Careful! You need to quote patterns that contain metacharacters (such as * in the example above). Singe quotes work as well as double quotes. The braces surrounding the expression have to be escaped, too. And watch those spaces right before and after the braces, they’re essential.

See also find manpage, GNU find documentation

2 thoughts on “How to find multiple patterns with GNU findutils

  1. avatarLeho Kraav

    There needs to be simpler way to thumbs-up random hacking blog posts than having to write a whole WP comment for them :>

Leave a Reply

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.