The Problem
You're SSH'd into a server. You created a config file two hours ago. You remember it's called nginx.conf, but you have no idea where it is.
This was me on day one with Linux. Here's what I learned about finding files fast.
The Obvious Choice: find
First attempt:
find / -name 'nginx.conf'
What this does:
- find - The search command
- / - Star from root directory (searches everything)
- -name 'nginx.conf - Find this exact filename
Result: It worked, but took 30+ seconds and threw "Permission denied" errors everywhere.
Why So Slow?
find searches your file system in real-time. It literally opens every directory and checks every file when you run it.
When find Shines
Use find when you need:
# Files modified in last 24 hours
find /home -mtime -1
# Files larger than 100MB
find / -size +100M
# Specific file type
find . -type f -name "*.log"
The Game Changer: locate
A senior dev saw me waiting and said, "Just use locate
locate nginx.conf
Results: Instant. Less than 1 second. No errors.
How locate Works
Here's the key: locate searches a database, not your filesystem
Linux maintains a database of all files (stored at /var/lib/mlocate/mlocate.db). This database updates automatically once a day via cron
When you run locate, you're querying a database → extremely fast.
The tradeoff? Just-created files won't appear until the database updates.
Updating the Database
Created a file and need to find it now?
sudo updatedb
Takes 1-2 minutes to refresh, then locate knows about all your new files.
Real Example: Finding Logs
Find all error logs:
# Using find (slow but real-time)
find /var/log -name '*error*'
# Using locate (fast but uses database)
locate error | grep /var/log
Common Mistakes
1. Permission Denied Spam
# ❌ This gives tons of permission errors
find / -name 'config.txt'
# ✅ Better options
sudo find / -name 'config.txt'
# OR
find ~/ -name 'config.txt'
2. Forgetting About the Database
# Just created a file
touch /tmp/newfile.txt
# This won't find it yet
locate newfile.txt
# Update database first
sudo updatedb
# Now it works
locate newfile.txt
3. Using find When locate Would Work
If you just need to find a file by name, start with locate. It's faster 99% of the time.
The Decision Framework
Use locate when:
- ✅ You know the filename
- ✅ Speed matters
- ✅ File isn't brand new
Use locate when:
- ✅ File was just created
- ✅ Searching by
- ✅ Need to perform actions on results
Useful Patterns
Case-insensitive search
locate -i nginx.conf # Finds NGINX.conf, nginx.CONF, etc.
Limit results
locate -n 10 *.log # Show only first 10 results
Search only existing files
locate -e filename # Skip files that have been deleted
Find and execute
# Delete all .tmp files older than 7 days
find /tmp -name "*.tmp" -mtime +7 -delete
Performance Comparison
On my test system(16GB data):
# find: ~28 seconds
time find / -name 'nginx.conf' 2>/dev/null
# locate: 0.3 seconds
time locate nginx.conf
That's 93x faster for simple name searches.
Key Takeaways
-
locateuses a database → Fast but potentially outdated -
findsearches filesystem → Slower but real-time accurate -
Update database with
sudo updatedbwhen needed 4.** Choose based on need**: Speed and Accuracy
The moment I understood this difference, file searching in Linux clicked. Now I always ask: "Do I need real-time results?" If not, locate it is.
What's your go-to file finding trick? Drop in the comments, I'm still learning and would love to hear what works for you!
Follow me for more Linux learning notes as I document my journey from beginner to cloud engineer.
Top comments (1)
Hello