How do you search for files in Linux with find and locate?
Learn how to search for files in Linux using find and locate, when to use each, key find predicates, and why locate needs updatedb to stay current.
Expected Interview Answer
find searches the filesystem live by walking directories in real time using rich criteria like name, size, type, and modification time, while locate does a near-instant lookup against a prebuilt database (updatedb) of file paths.
find is accurate and always current because it scans the actual filesystem, but it can be slow on large trees; it also supports actions like -exec to run commands on matches. locate is extremely fast because it queries an index rather than scanning disk, but its results are only as fresh as the last updatedb run, so newly created files may be missing until the database updates. In practice you use locate for quick 'where is this file' lookups and find when you need precise, up-to-date filtering or to act on the results.
- find gives precise, up-to-the-second results
- find can filter by size, time, type, and permissions
- find can run actions on matches with -exec
- locate returns results almost instantly
- locate is ideal for quick path lookups
AI Mentor Explanation
Using find is like a fielding coach personally walking the entire ground checking every blade of grass for a lost ball right now, guaranteeing an accurate result but taking time. locate is like glancing at the printed ground map made this morning: instant, but it won't show a ball that rolled into a new corner since the map was drawn.
Step-by-Step Explanation
Step 1
Choose the right tool
Use find for precise, current, criteria-based searches; use locate for fast path lookups when freshness matters less.
Step 2
Run a basic find
find /path -name 'file.txt' searches under /path by exact name; use -iname for case-insensitive matches.
Step 3
Filter with predicates
Combine -type, -size, -mtime, and -perm to narrow results, e.g. find . -type f -size +10M.
Step 4
Act on matches
Use -exec or -delete to operate on results, e.g. find . -name '*.log' -exec rm {} +.
Step 5
Keep locate fresh
Run sudo updatedb to rebuild the index, then locate filename for an instant lookup.
What Interviewer Expects
- Knowing find scans live and locate queries an index
- Awareness that locate needs updatedb to stay current
- Common find predicates like -name, -type, -mtime, -size
- Using -exec to act on found files
- When to prefer one tool over the other
Common Mistakes
- Thinking locate always reflects the current filesystem
- Forgetting to run updatedb before locate on new files
- Confusing -name (case-sensitive) with -iname
- Misusing -exec by omitting {} or the terminator
- Assuming find is always too slow to use
Best Answer (HR Friendly)
“find searches your files live by actually looking through folders, so results are accurate but can be slow. locate is much faster because it checks a saved list of files, though that list needs periodic updating to include brand-new files.”
Code Example
# Find files by name under a directory (case-insensitive)
find /var/log -iname '*.log'
# Find files larger than 100MB modified in the last 7 days
find /home -type f -size +100M -mtime -7
# Run a command on every match
find . -name '*.tmp' -exec rm {} +
# locate needs an up-to-date database first
sudo updatedb
locate nginx.conf
# Count locate results
locate -c '*.conf'Follow-up Questions
- How does find's -exec {} + differ from -exec {} \;?
- What does updatedb do and how often should it run?
- How do you search file contents rather than names?
- How would you find files modified in the last 24 hours?
- What is the difference between -name and -path in find?
MCQ Practice
1. Which tool queries a prebuilt database instead of scanning the filesystem live?
locate reads an index built by updatedb, making it fast but potentially stale.
2. Which command finds files by name case-insensitively?
-iname performs a case-insensitive name match, while -name is case-sensitive.
3. Why might locate not show a file you just created?
locate only knows about files present the last time updatedb ran, so new files may be missing until it re-runs.
Flash Cards
How does find work? — It walks the filesystem live, matching criteria like -name, -type, -size, and -mtime.
How does locate work? — It queries a prebuilt database (updatedb) of paths, returning results almost instantly.
Keeping locate current? — Run sudo updatedb to rebuild the index.
Acting on found files? — Use find's -exec cmd {} + or -delete to operate on matches.
find vs locate tradeoff? — find is accurate but slower; locate is fast but only as fresh as the last updatedb.