How to Search Files by Extension in Linux
In this article
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
locatecommand, you may need to install it (e.g.,sudo apt install mlocateon Debian/Ubuntu) and update its database withsudo updatedb.
Step-by-Step
- Open a terminal. Press
Ctrl+Alt+Tor search for “Terminal” in your applications menu. - Navigate to the directory you want to search in. Use
cdto change directories. For example,cd ~/Documentsgoes to your Documents folder. If you want to search the entire system, start from/(root), but be prepared for a slower search. - Use the
findcommand with the-nameoption. The basic syntax is:find /path/to/search -type f -name "*.ext"
Replace/path/to/searchwith your directory (e.g.,.for current directory) andextwith the actual extension (e.g.,pdf). The quotes around*.extprevent the shell from expanding the wildcard beforefindsees it. Example:find . -type f -name "*.pdf"lists all PDF files in the current directory and its subdirectories. - Make the search case-insensitive (optional). Add
-inameinstead of-nameto match both uppercase and lowercase extensions. Example:find . -type f -iname "*.PDF"will find.pdf,.PDF,.Pdf, etc. - 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". - Use
locatefor a faster system-wide search. If you havelocateinstalled, runlocate "*.ext". It searches a pre-built database, so it's much faster thanfindbut may not show very recent files unless you update the database first withsudo updatedb. - Use
lswith a wildcard for a simple directory listing. If you just want to see files in the current directory,ls *.extworks. For example,ls *.pnglists all PNG files in the current directory only. Note thatlsdoes not search subdirectories. - Combine with other filters (optional). You can add options like
-size +10Mto find files larger than 10 MB, or-mtime -7to 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 *.pdfwithout quotes, the shell may expand the wildcard and pass multiple arguments, causing errors or unexpected results. - Using
-nameinstead of-inamewhen you need case-insensitive matching. Linux filenames are case-sensitive, so*.PDFwill not match.pdfunless you use-iname. - Searching from the root without permission. Running
find / -name "*.conf"will produce many “Permission denied” errors. Usesudoor search only directories you have access to. - Using
locatewithout updating the database. If you recently created files,locatemay not find them until you runsudo updatedb. - Assuming
lssearches subdirectories.ls *.extonly shows files in the current directory; it does not recurse.
Tips
- Use
-type fto exclude directories. Without it,findwill also match directories whose names end with the extension, which is rarely what you want. - Pipe to
lessfor long lists. If the output is huge, add| lessto scroll through it. - Count files with
wc -l. Example:find . -type f -name "*.jpg" | wc -lgives the number of JPEG files. - Use
findwith-execto act on results. For example, to delete all.tmpfiles: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
*.extin 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
sudoif 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 usedlocate, runsudo updatedbfirst. Also verify the extension spelling – remember case sensitivity. - Results are too many and slow. Add
-maxdepthto limit depth, or uselocateinstead offindfor system-wide searches. locatecommand 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 runsudo 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”.