Find Command: Search Multiple File Extensions in Linux
In this article
The Linux find command is a powerful tool for locating files and directories. When you need to search for files with multiple extensions—like .txt, .pdf, and .log—you can combine conditions to match any of them. This guide shows you how to build such commands, using logical operators and wildcards, and covers common pitfalls and troubleshooting.
What You'll Need
- A Linux system with a terminal (any distribution works).
- Basic familiarity with the command line (cd, ls, etc.).
- Permission to read the directories you search (use
sudoonly if necessary).
Step-by-Step
- Open a terminal. Press
Ctrl+Alt+T(or use your system's terminal app). - Understand the basic syntax. The generic form is:
find [starting directory] [options] [expression]
For example,find /home/user -name "*.txt"finds all.txtfiles under/home/user. - Use the
-o(OR) operator. To match multiple extensions, combine-nametests with-o. For instance:find /home/user -name "*.txt" -o -name "*.pdf"
This returns files ending in.txtOR.pdf. - Group conditions with parentheses. When mixing with other tests (like
-type f), you must group the OR conditions using escaped parentheses\( ... \)to avoid precedence issues. Example:find /home/user -type f \( -name "*.txt" -o -name "*.pdf" \)
Without parentheses, the command would apply-type fonly to the first-nametest, not the whole group. - Add more extensions. Simply append more
-o -name "*.ext"inside the parentheses:find /home/user -type f \( -name "*.txt" -o -name "*.pdf" -o -name "*.log" \)
- Use wildcards for flexibility. The asterisk
*matches any characters. For example,*.doc*matches.doc,.docx, and.document. You can also use?for a single character, but it's rarely needed for extensions. - Search from the current directory. Use
.as the starting point to search the current folder and subfolders:find . -type f \( -name "*.sh" -o -name "*.py" \)
- Add actions. By default,
findprints results. You can add actions like-delete(careful!),-exec, or-ls. For example, to list details:find . -type f \( -name "*.jpg" -o -name "*.png" \) -ls
- Use case-insensitive matching. If you want to ignore case, replace
-namewith-iname:find . -iname "*.txt" -o -iname "*.pdf"
- Test your command. Before running a destructive action, run the command without the action to see what it matches. For example, run the find command without
-deletefirst.
Common Mistakes
- Forgetting parentheses. Without
\( ... \), the OR condition can cause unexpected results when combined with other tests like-type f. Always group OR conditions. - Using single quotes incorrectly. In most shells, single quotes prevent wildcard expansion, which is fine, but double quotes work too. The key is to escape parentheses properly—they are special to the shell.
- Not escaping parentheses. In bash, parentheses must be escaped as
\(and\)or quoted. If you forget, you'll get a syntax error. - Placing
-oincorrectly. The-ooperator has lower precedence than implicit AND, so without grouping, the command may not behave as intended. - Using
-a(AND) instead of-o. To match any of several extensions, you need OR, not AND. AND would require a file to have both extensions, which is impossible.
Tips
- Use
-maxdepthto limit recursion. For example,find . -maxdepth 1 -name "*.txt"searches only the current directory, not subdirectories. - Combine with
-execfor bulk operations. To copy or move matching files, use-exec cp {} /target/ \;(note the escaped semicolon). - Use
-printffor custom output. For instance,-printf "%p\n"prints just the path. - Consider using
findwithgrepfor content search. If you need to search inside files, pipe togrep:find . -name "*.txt" -exec grep -l "pattern" {} \; - For many extensions, consider using a regular expression. The
-regexoption (with-regextypefor syntax) can match patterns like.*\.\(txt\|pdf\)$. But the-omethod is simpler for beginners.
Troubleshooting
- Command not found or syntax error. Check that you have spaces around
-oand that parentheses are escaped. For example,find . -type f \( -name "*.txt" -o -name "*.pdf" \)works;find . -type f (-name "*.txt" -o -name "*.pdf")does not. - No results found. Verify the starting directory exists and you have read permissions. Use
lsto confirm files exist. Also, check the case sensitivity—use-inameif needed. - Results include directories. If you only want files, add
-type f. If you only want directories, use-type d. - Permission denied errors. Run with
sudoif you have admin rights, but be cautious with-delete. Alternatively, redirect errors:find . -name "*.txt" 2>/dev/null. - Parentheses cause shell errors. If you use
shorcsh, the escaping may differ. In bash,\(works; in some shells, you may need to quote them:find . -type f '(' -name "*.txt" -o -name "*.pdf" ')'.
Frequently Asked Questions
Can I search for multiple extensions without typing -o many times?
Yes, you can use a regular expression with -regex. For example: find . -type f -regex '.*\.\(txt\|pdf\)$'. But the -o method is more readable for a few extensions.
How do I search case-insensitively for multiple extensions?
Use -iname instead of -name for each condition. For example: find . -iname "*.txt" -o -iname "*.PDF".
What does -type f do?
It restricts the search to regular files only, excluding directories, symlinks, and other special files. It's often used to avoid matching directories that happen to have a dot in their name.
Can I use wildcards like *.doc* to match both .doc and .docx?
Yes, the asterisk matches any characters, so *.doc* matches filenames containing .doc followed by anything, including .docx. However, it also matches .document, so be specific if needed.
How do I exclude certain extensions while searching?
Use ! -name "*.tmp" (with escaping if needed) or use -not. For example: find . -type f \( -name "*.txt" -o -name "*.pdf" \) ! -name "*.tmp".
Is there a performance difference between -o and -regex?
For a handful of extensions, -o is usually faster because it uses simple string matching. -regex can be slower due to regex parsing, but the difference is negligible for typical searches.