User Agents

Web browsers and everyday web tech, explained

Exclude File Extensions from find in Bash

· 6 min read · Extensions

In this article
  1. 1. Use -not -name with Wildcards
  2. 2. Combine with -o (OR) and Parentheses
  3. 3. Use -regex with Negative Lookahead (GNU find)
  4. 4. Use -name with Character Classes
  5. 5. Use -prune for Directories
  6. 6. Use -exec with grep -v
  7. 7. Use -path to Exclude by Full Path
  8. 8. Use -name with Multiple -o and -not
  9. 9. Use -regextype posix-extended with Alternation
  10. 10. Use -name with Brace Expansion (Bash)
  11. How to Choose
  12. Frequently Asked Questions
  13. Final Thoughts

This guide covers practical ways to exclude file extensions when using the find command in Bash. You'll learn multiple methods—from simple -not expressions to regex patterns and prune tricks—so you can filter out logs, caches, or any unwanted files. It's written for Linux/macOS users who already know basic find syntax but need reliable exclusion techniques.

1. Use -not -name with Wildcards

The most direct approach is find . -type f -not -name "*.txt". This excludes every file ending in .txt. You can chain multiple -not -name conditions: find . -type f -not -name "*.log" -not -name "*.tmp". Remember that the wildcard * matches any characters, so *.log catches error.log and access.log. This method is simple and readable, but it gets verbose if you need to exclude many extensions.

2. Combine with -o (OR) and Parentheses

When you want to exclude multiple extensions in one expression, use find . -type f \( -name "*.log" -o -name "*.tmp" \) -prune -o -print. The parentheses group the OR conditions, and -prune prevents descent into matching directories. However, for plain files, a cleaner variant is find . -type f ! \( -name "*.log" -o -name "*.tmp" \). The ! negates the whole group, so only files that match neither extension are printed. This avoids repeating -not for each pattern.

3. Use -regex with Negative Lookahead (GNU find)

GNU find (on Linux) supports -regex with Emacs-style regular expressions. To exclude extensions, you can use a negative lookahead: find . -type f -regextype posix-extended -regex '.*\.\(log\|tmp\)$' -prune -o -print. But that's clunky. A better pattern is find . -type f -regextype posix-extended -regex '.*\.(?!log$|tmp$).*'. Note that -regextype posix-extended does not support lookahead; you need -regextype emacs (the default) for lookahead. Example: find . -type f -regextype emacs -regex '.*\.\(?!log$\).*'. This is powerful but harder to read, and it's not portable to BSD/macOS find.

4. Use -name with Character Classes

You can exclude a set of extensions using bracket expressions in -name. For instance, find . -type f -name "*.c" -o -name "*.h" includes both, but to exclude both, do find . -type f ! -name "*.[ch]". The bracket [ch] matches either 'c' or 'h', so it excludes .c and .h files. This works for any single-character extension differences. For multi-character extensions, you'd need multiple patterns, but it's handy for short lists.

5. Use -prune for Directories

If you want to exclude files with certain extensions inside specific directories (like .git or node_modules), -prune is the tool. Example: find . -type d \( -name node_modules -o -name .git \) -prune -o -type f -print. This prevents find from entering those directories, so any files with any extensions inside are skipped. It's not about extensions per se, but it's a common companion when you want to avoid scanning certain areas that contain many unwanted file types.

6. Use -exec with grep -v

You can pipe find output to grep -v to filter extensions: find . -type f | grep -v -E '\.(log|tmp)$'. This is simple and works on any system with GNU grep. However, it breaks if filenames contain newlines (rare but possible). For robustness, use find . -type f -print0 | grep -vz -E '\.(log|tmp)$' | xargs -0 .... This method is easy to remember and debug, but it's less efficient for huge directory trees because it processes all files before filtering.

7. Use -path to Exclude by Full Path

Sometimes you want to exclude files based on their directory path, not just extension. For example, find . -type f ! -path "*/build/*" excludes all files under any build directory, regardless of extension. Combine with extension exclusions: find . -type f ! -path "*/build/*" ! -name "*.log". This is useful when you have generated files in certain folders that you never want to see. It's not purely extension-based, but it's a practical addition to your exclusion toolkit.

8. Use -name with Multiple -o and -not

For a moderate number of extensions, you can write find . -type f -not -name "*.log" -not -name "*.tmp" -not -name "*.bak". This is straightforward but repetitive. To reduce repetition, you can define a variable: EXCLUDES="*.log *.tmp *.bak"; for ext in $EXCLUDES; do ... but that's not a direct find solution. The direct method is fine for up to 5-6 extensions; beyond that, consider a regex approach.

9. Use -regextype posix-extended with Alternation

On GNU find, you can use -regextype posix-extended to match any of several extensions and then negate: find . -type f -regextype posix-extended ! -regex '.*\.(log|tmp|bak)$'. The ! negates the regex, so files that do NOT end with those extensions are printed. This is cleaner than multiple -not -name when you have many extensions. Note that the regex must match the entire path, so .* is needed. This works on Linux but not on macOS's BSD find (which lacks -regextype).

10. Use -name with Brace Expansion (Bash)

Bash's brace expansion can generate multiple -name patterns. For example, find . -type f ! -name "*.{log,tmp,bak}" does NOT work as expected because find doesn't interpret braces. Instead, you can do: find . -type f ! -name "*.log" ! -name "*.tmp" ! -name "*.bak" (as in #8). But you can use brace expansion to build the find command dynamically: eval "find . -type f $(printf '! -name "*.%s" ' log tmp bak)". This is advanced and requires careful quoting, but it's a neat trick for scripting. However, it's error-prone with spaces in filenames, so it's better to stick with explicit lists.

How to Choose

The right method depends on your environment and how many extensions you need to exclude. For a one-off command with one or two extensions, use -not -name. For a handful, use ! \( -name "*.log" -o -name "*.tmp" \). If you're on Linux and need to exclude many extensions, the posix-extended regex is concise. On macOS, stick with -not -name because BSD find lacks -regextype. If you also need to skip entire directories, add -prune. Always test with -print before using -exec to avoid surprises.

Frequently Asked Questions

How do I exclude multiple extensions in one find command?

Use find . -type f ! \( -name "*.log" -o -name "*.tmp" \). The parentheses group the OR, and the ! negates the whole group.

Does -name "*.log" match files in subdirectories?

Yes, -name matches the basename of the file, so *.log matches any file ending in .log in any directory.

Why doesn't -name "*.{log,tmp}" work?

Because find does not interpret brace expansion; braces are treated literally. Use multiple -name or a regex instead.

How do I exclude files with a specific extension but not directories?

Add -type f to your find command. For example, find . -type f -not -name "*.log" only considers regular files.

Is there a portable way to exclude extensions on macOS and Linux?

Yes, use -not -name with multiple patterns. It works on both BSD and GNU find. Avoid -regex and -regextype because they are not portable.

Can I exclude files based on extension when using -exec?

Yes, you can combine -exec with the exclusion patterns. For instance, find . -type f ! -name "*.log" -exec ls -l {} \; will only list non-log files.

Final Thoughts

Excluding file extensions from find is a common task that has several solutions. Start with the simple -not -name for clarity, and reach for regex or -prune when your needs grow. Always test your command on a small sample to ensure it behaves as expected, especially when using negation and grouping. With these techniques, you'll be able to filter out exactly what you need from any directory tree.