Unix Linux: Find Files by Multiple Extensions
In this article
To find files with multiple filename extensions in Unix or Linux, use the find command with the -o (OR) operator and parentheses to group the conditions, for example: find . -type f \( -name "*.jpg" -o -name "*.png" \). This command searches the current directory and its subdirectories for files ending in either .jpg or .png.
Why You Need This
When working in a Unix-like environment, you often need to locate files that share a common purpose but have different extensions—for instance, image files (.jpg, .png, .gif), document files (.pdf, .docx, .txt), or source code files (.c, .h, .py). The find command is the standard tool for this, but its syntax for multiple conditions can be confusing because it uses logical operators that differ from most programming languages. Understanding how to combine patterns correctly saves time and prevents errors like missing files or accidentally matching the wrong ones.
How the find Command Handles Multiple Extensions
The find command evaluates expressions from left to right and uses three logical operators: -a (AND), -o (OR), and ! (NOT). By default, if you write multiple conditions without an operator, find treats them as AND. For example, find . -name "*.txt" -name "*.log" would only match files that end with both .txt and .log—which is impossible for a single filename—so it returns nothing. To match files that have either extension, you must use -o and group the conditions with parentheses. The parentheses need to be escaped with backslashes (\( and \)) or quoted so that the shell doesn't interpret them as subshell syntax.
Basic Syntax and Examples
Here is the general structure:
find [path] -type f \( -name "*.ext1" -o -name "*.ext2" \)
Replace [path] with the directory you want to search (use . for the current directory, / for the root, or a specific path like /home/user/docs). The -type f limits results to regular files, excluding directories and special files.
Concrete examples:
- Find all JPEG and PNG images:
find . -type f \( -name "*.jpg" -o -name "*.png" \) - Find all C and header source files:
find /src -type f \( -name "*.c" -o -name "*.h" \) - Find all text and Markdown files:
find . -type f \( -name "*.txt" -o -name "*.md" \)
You can also add more extensions by chaining more -o conditions: find . -type f \( -name "*.jpg" -o -name "*.png" -o -name "*.gif" -o -name "*.bmp" \). This is equivalent to saying “match any of these patterns.”
Using Regular Expressions for More Flexibility
If you have many extensions to match, writing a long chain of -o conditions becomes tedious. The find command supports regular expression matching with the -regex option (or -iregex for case-insensitive matching). The syntax varies slightly between GNU find (common on Linux) and BSD find (common on macOS), but a typical GNU example is:
find . -type f -regex ".*\.\(jpg\|png\|gif\)$"
This uses a basic regular expression (BRE) by default; you can use -regextype posix-extended to make it more readable: find . -type f -regextype posix-extended -regex ".*\.(jpg|png|gif)$". On macOS, the -E flag enables extended regex, but the syntax for -regex is different; a simpler approach on macOS is to use find . -type f -print | grep -E "\.(jpg|png|gif)$", though that is less efficient for very large directory trees.
Case-Insensitive Matching
By default, -name is case-sensitive, so *.JPG would not match a file named photo.jpg. To match regardless of case, use -iname instead. For example:
find . -type f \( -iname "*.jpg" -o -iname "*.png" \)
This matches .JPG, .Jpg, .jpg, and so on. Similarly, -iregex makes regular expression matching case-insensitive.
Common Pitfalls and How to Avoid Them
One frequent mistake is forgetting to escape the parentheses. If you write find . -type f ( -name "*.jpg" -o -name "*.png" ) without backslashes, the shell will try to interpret the parentheses and produce a syntax error. Always use \( and \) or quote them as '( ... )'.
Another issue is combining -o with other conditions without grouping. For instance, find . -type f -name "*.jpg" -o -name "*.png" works because the default action is to print matches, but if you add a -mtime condition, you must group the name conditions: find . -type f \( -name "*.jpg" -o -name "*.png" \) -mtime -7. Without the parentheses, the -mtime would apply only to the *.png part, giving unexpected results.
Also, be careful with the -name pattern: it matches the entire filename, not just the extension. So -name "*.jpg" matches any file ending in .jpg, but it also matches a file named my.jpg.backup? Actually no, because the pattern must match the whole name; my.jpg.backup does not end with .jpg, so it won't match. That is usually what you want, but if you need to match files that contain .jpg anywhere, use -name "*.jpg*"—but that would also match .jpg.html, which may not be intended.
Alternatives: grep, locate, and Shell Glob
While find is the most powerful and reliable tool, there are alternatives depending on your needs:
- Shell globbing: In bash, you can use extended globbing (
shopt -s extglob) to match multiple extensions:ls *.@(jpg|png|gif). This works only in the current directory and does not recurse into subdirectories unless you enableglobstarand use**. - grep with find: Piping
findoutput togrepcan filter by extension, but it's less efficient and can break on filenames with newlines (though using-print0andgrep -zmitigates that). - locate: The
locatecommand searches a pre-built database, which is very fast but may be outdated. You can uselocate "*.jpg"but combining multiple extensions requires regex:locate --regex "\.(jpg|png)$"(GNU locate).
For most scripting and system administration tasks, find is the recommended choice because it is POSIX-compliant, handles unusual filenames safely, and gives you fine control over the search.
Frequently Asked Questions
Can I use find to match files with multiple extensions in one command without parentheses?
Yes, but only if you use a regular expression. For example, find . -type f -regex ".*\.\(jpg\|png\)$" works without parentheses because the regex itself groups the alternatives. The parentheses in the regex are part of the pattern, not shell syntax.
Why does find . -name "*.jpg" -o -name "*.png" -mtime -7 not work as expected?
Because the -mtime -7 condition is evaluated as part of the OR chain. The expression is parsed as ( -name "*.jpg" ) OR ( -name "*.png" AND -mtime -7 ), so files matching *.jpg are printed regardless of modification time, while *.png files must also be modified within 7 days. To fix it, group the name conditions: find . -type f \( -name "*.jpg" -o -name "*.png" \) -mtime -7.
How do I search for files with extensions like .tar.gz or .log.1?
You can use a pattern that matches the whole suffix: find . -type f -name "*.tar.gz" matches exactly files ending with .tar.gz. To match both .tar.gz and .tar.bz2, use -name "*.tar.*" or the OR approach: find . -type f \( -name "*.tar.gz" -o -name "*.tar.bz2" \).
Is there a way to exclude certain extensions while searching?
Yes, use the ! operator. For example, to find all files except those ending with .tmp or .bak: find . -type f ! \( -name "*.tmp" -o -name "*.bak" \). The parentheses are needed to group the exclusions.
What is the difference between -name and -path in this context?
-name matches only the base filename (the last component of the path), while -path matches the entire path relative to the search starting point. If you want to match files based on their full path (e.g., all files in a directory named backup), use -path. For extension matching, -name is usually sufficient.