DEV Community

Cover image for Finding Files in Linux: Why locate is 10x Faster Than find published
NJEI
NJEI

Posted on

Finding Files in Linux: Why locate is 10x Faster Than find published

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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

Limit results

locate -n 10 *.log  # Show only first 10 results
Enter fullscreen mode Exit fullscreen mode

Search only existing files

locate -e filename  # Skip files that have been deleted
Enter fullscreen mode Exit fullscreen mode

Find and execute

# Delete all .tmp files older than 7 days
find /tmp -name "*.tmp" -mtime +7 -delete
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

That's 93x faster for simple name searches.

Key Takeaways

  1. locate uses a database → Fast but potentially outdated
  2. find searches filesystem → Slower but real-time accurate
  3. Update database with sudo updatedb when 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)

Collapse
 
emmanuel_emmanuel_6df52e9 profile image
Emmanuel Emmanuel

Hello