User Agents

Web browsers and everyday web tech, explained

How to Search Files by Extension in Linux

· 5 min read · Extensions

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Searching for files by their extension (like .pdf, .jpg, or .txt) is a common task in Linux. You can do it quickly using built-in command-line tools like find, locate, or ls with wildcards. This guide shows you the most reliable methods and how to avoid common pitfalls.

What You'll Need

  • A Linux terminal (any distribution works).
  • Basic familiarity with navigating directories (cd, pwd).
  • For the locate command, you may need to install it (e.g., sudo apt install mlocate on Debian/Ubuntu) and update its database with sudo updatedb.

Step-by-Step

  1. Open a terminal. Press Ctrl+Alt+T or search for “Terminal” in your applications menu.
  2. Navigate to the directory you want to search in. Use cd to change directories. For example, cd ~/Documents goes to your Documents folder. If you want to search the entire system, start from / (root), but be prepared for a slower search.
  3. Use the find command with the -name option. The basic syntax is:
    find /path/to/search -type f -name "*.ext"
    Replace /path/to/search with your directory (e.g., . for current directory) and ext with the actual extension (e.g., pdf). The quotes around *.ext prevent the shell from expanding the wildcard before find sees it. Example: find . -type f -name "*.pdf" lists all PDF files in the current directory and its subdirectories.
  4. Make the search case-insensitive (optional). Add -iname instead of -name to match both uppercase and lowercase extensions. Example: find . -type f -iname "*.PDF" will find .pdf, .PDF, .Pdf, etc.
  5. Limit to a specific depth (optional). If you only want to search the current directory (not subdirectories), add -maxdepth 1. Example: find . -maxdepth 1 -type f -name "*.txt".
  6. Use locate for a faster system-wide search. If you have locate installed, run locate "*.ext". It searches a pre-built database, so it's much faster than find but may not show very recent files unless you update the database first with sudo updatedb.
  7. Use ls with a wildcard for a simple directory listing. If you just want to see files in the current directory, ls *.ext works. For example, ls *.png lists all PNG files in the current directory only. Note that ls does not search subdirectories.
  8. Combine with other filters (optional). You can add options like -size +10M to find files larger than 10 MB, or -mtime -7 to find files modified in the last 7 days. Example: find . -type f -name "*.log" -size +1M.

Common Mistakes

  • Forgetting quotes around the pattern. If you write find . -name *.pdf without quotes, the shell may expand the wildcard and pass multiple arguments, causing errors or unexpected results.
  • Using -name instead of -iname when you need case-insensitive matching. Linux filenames are case-sensitive, so *.PDF will not match .pdf unless you use -iname.
  • Searching from the root without permission. Running find / -name "*.conf" will produce many “Permission denied” errors. Use sudo or search only directories you have access to.
  • Using locate without updating the database. If you recently created files, locate may not find them until you run sudo updatedb.
  • Assuming ls searches subdirectories. ls *.ext only shows files in the current directory; it does not recurse.

Tips

  • Use -type f to exclude directories. Without it, find will also match directories whose names end with the extension, which is rarely what you want.
  • Pipe to less for long lists. If the output is huge, add | less to scroll through it.
  • Count files with wc -l. Example: find . -type f -name "*.jpg" | wc -l gives the number of JPEG files.
  • Use find with -exec to act on results. For example, to delete all .tmp files: find . -type f -name "*.tmp" -exec rm {} \; (be careful!).
  • For GUI users, use the file manager’s search. Most Linux file managers (Nautilus, Dolphin, etc.) allow you to type *.ext in the search bar.

Troubleshooting

  • “find: paths must precede expression” error. This usually happens when you forget quotes around the pattern. Wrap the pattern in quotes: find . -name "*.txt".
  • “Permission denied” messages. Run the command with sudo if you have administrative rights, or search only directories you own (e.g., your home folder).
  • No results when you know files exist. Check your current directory with pwd. If you used locate, run sudo updatedb first. Also verify the extension spelling – remember case sensitivity.
  • Results are too many and slow. Add -maxdepth to limit depth, or use locate instead of find for system-wide searches.
  • locate command not found. Install it via your package manager. On Debian/Ubuntu: sudo apt install mlocate. On Fedora: sudo dnf install mlocate. On Arch: sudo pacman -S mlocate. Then run sudo updatedb.

Frequently Asked Questions

How do I search for multiple extensions at once?

Use the -o (OR) option with find: find . -type f \( -name "*.jpg" -o -name "*.png" \). Make sure to escape the parentheses or quote them.

Can I search for files without an extension?

Yes, use find . -type f ! -name "*.*" to find files with no dot in their name.

How do I search in a specific directory without going into it?

Just provide the path as the first argument to find, e.g., find /var/log -name "*.log".

What’s the difference between find and locate?

find searches the actual filesystem in real-time, so it’s always up-to-date but slower. locate searches a pre-built database, so it’s faster but may be outdated until you run updatedb.

How do I search for files with a specific extension and copy them to another folder?

Use find with -exec cp: find . -type f -name "*.pdf" -exec cp {} /target/directory/ \;. Make sure the target directory exists.

Can I use wildcards in the middle of a filename?

Yes, find supports wildcards anywhere in the pattern. For example, find . -name "report*.pdf" finds files starting with “report” and ending with “.pdf”.