How to Search for a File in Linux: Commands, Options, and When to Use Each
Searching for files in Linux is something every user encounters — whether you're tracking down a config file buried in /etc, hunting for a log you know exists somewhere, or trying to locate a script a colleague dropped on a shared server. Linux gives you several powerful tools to do this, and knowing how they differ is what determines whether you find what you need in two seconds or two frustrating minutes.
The Main Tools for File Search in Linux
Linux doesn't rely on a single search tool. The three you'll encounter most often are find, locate, and fd. Each works differently at a fundamental level.
find — The Most Flexible Option
find is the standard, built into virtually every Linux system. It searches the filesystem in real time, which means it reflects the current state of your directories — no caching, no delay between a file being created and being discoverable.
Basic syntax:
find /path/to/search -name "filename.txt" To search from the current directory:
find . -name "filename.txt" Case-insensitive search (useful when you're not sure of capitalization):
find . -iname "filename.txt" You can also search by file type, size, modification date, and more:
find /home -type f -name "*.log" # Files ending in .log find /var -mtime -7 # Modified in the last 7 days find / -size +100M # Files larger than 100MB The -type flag is particularly useful: f targets regular files, d targets directories, l targets symbolic links.
One thing to understand: find can be slow on large filesystems because it walks the directory tree live. On a system with millions of files, a broad find / search can take a while.
locate — Fast, But Relies on a Database
locate queries a pre-built index of your filesystem rather than searching in real time. This makes it dramatically faster for simple name-based searches.
locate filename.txt The tradeoff: the index (built by a tool called updatedb) isn't updated continuously. On most systems it runs once daily via a scheduled job. If a file was created recently, locate may not find it until the database updates. You can trigger a manual update:
sudo updatedb locate also supports pattern matching:
locate "*.conf" It's the right choice when you need speed and you're looking for files that have existed on the system for more than a day or so.
fd — A Modern Alternative
fd is a third-party tool that offers a cleaner syntax and faster performance than find in many cases, with sensible defaults like ignoring hidden files and respecting .gitignore patterns.
fd filename.txt It's not pre-installed on most distributions, so you'd need to install it via your package manager (apt, dnf, pacman, etc.). For developers working in code repositories, it's often significantly more practical than find.
Searching File Contents, Not Just Names
Sometimes you know roughly what's inside a file but not what it's called. That's where grep comes in — it searches for text patterns within files.
grep -r "search_term" /path/to/directory The -r flag makes it recursive (searches subdirectories). Add -l to return only filenames rather than every matching line:
grep -rl "search_term" /home/user/ Combine find and grep for targeted content searches:
find /etc -name "*.conf" -exec grep -l "timeout" {} ; This finds all .conf files under /etc and then checks which ones contain the word "timeout."
🔍 Quick Reference: Which Tool Does What
| Tool | Search Type | Speed | Real-Time | Pre-installed |
|---|---|---|---|---|
find | Name, type, size, date | Moderate | ✅ Yes | ✅ Yes |
locate | Name/pattern | Fast | ❌ No (database) | Usually |
fd | Name/pattern | Fast | ✅ Yes | ❌ No |
grep | File contents | Varies | ✅ Yes | ✅ Yes |
Variables That Affect Which Approach Works Best
The "right" command depends on factors specific to your situation:
Filesystem size and structure — On a small local machine, find is rarely slow enough to matter. On a large server with deep directory trees and millions of files, the database-backed speed of locate becomes genuinely valuable.
How recent the file is — A file created in the last few hours won't appear in locate's index until updatedb runs. For newly created files, find is the only reliable option.
Whether you know the name or the contents — Name-based searches and content-based searches are fundamentally different problems. Mixing find with grep gives you both dimensions.
Your permission level — find respects filesystem permissions. Searching directories your user can't read will produce "permission denied" errors unless you run it with sudo. locate shows results from its database regardless, though you still can't open files you lack permission to access.
Distribution and environment — Some minimal Linux installs (containers, embedded systems, stripped-down servers) may not have locate installed at all. find is nearly universally available.
Shell scripting vs. interactive use — find's composability with -exec and pipes makes it the standard choice for scripts. fd's cleaner output is often better for interactive terminal use.
🗂️ Practical Examples Worth Bookmarking
Find all files owned by a specific user:
find /home -user username Find empty files:
find . -type f -empty Find files modified in the last 24 hours:
find / -mtime -1 -type f Search for a filename containing a partial string:
locate "*report*" The breadth of what you can do with these tools is significant — but which combination actually fits your workflow depends on your system configuration, how you're using Linux (desktop, server, scripting, development), and whether convenience or precision is the priority in a given moment. 🐧