Finding Files in Linux
One of the first things you’ll bump into on Linux is the question: “Where the heck did that file go?” 🕵️ Whether you’re trying to track down a rogue config, a missing binary, or just want to see what’s eating up disk space, Linux gives you a handful of tools — each with its own quirks.
The Toolbox
Here’s a quick rundown of the main commands you’ll use to find files:
ls
– Just Listing, Not Finding
The ls
command is for listing files in a directory, not searching for
them. Think of it like peeking into a folder — helpful when you already know
where you are, but useless if you’ve lost track of the thing entirely.
which
– Hunting Binaries in Your $PATH
If you’re wondering “Where is this program actually located?”, which
has
your back. It looks through your $PATH
(the list of directories Linux checks
for commands) and shows you where an executable lives.
Only works for binaries, though. If you’re trying to find a random text file, you’re out of luck.
locate
– Speedy (but Database-Driven)
locate
is like a cheat sheet: instead of crawling your disk live, it searches
a pre-built database. That’s why it’s lightning fast, but the results can be
stale if the database isn’t updated.
The database is refreshed by running:
Then you can search for files by name:
If it feels instant, that’s because it is —locate
already did the heavy
lifting in the background.
find
– The Heavyweight Champion
When you need flexibility, find
is your best friend. It actually walks the
filesystem, checking names, types, sizes, permissions, and even content if
you chain it with other tools. Slower than locate, but way more powerful.
Here are some real-world examples:
Find every file or directory named hosts starting at root (/
):
Find files larger than 100 MB in the root directory:
Search for files in /etc
containing the word "student", then copy them
to find/contents
:
find /etc -exec grep -l student {} \; -exec cp {} find/contents/ \; 2> /dev/null
Combine with xargs
for speed: find all files in /etc
and search them
for 127.0.0.1:
It’s a Swiss Army knife — intimidating at first, but once you get comfortable, you’ll wonder how you ever lived without it.
Common Gotchas
Even pros run into these snags:
Permission errors with find
Running find
against a directory where the user doesn't have permissions will throw lots of "Permission denied" errors. Options:
- Run with
sudo
- Or redirect errors to the void:
locate
database is out of date
Since locate
relies on a database, you may see files that don’t exist
anymore (or miss new ones). Fix it with:
which
only shows the first match in $PATH
If you have multiple versions of a binary installed, which
won’t tell you
about the others. Use:
Quoting matters in find
find / -name hosts
will work, but if your filename includes special
characters (*, ?, spaces), you’ll want quotes:
xargs
can choke on spaces
When piping filenames with spaces into xargs
, it may split them incorrectly.
Use:
Quick Recap
ls
→ Show me what’s here.which
- → Where’s that program in$PATH
?locate
→ Fast, but needs an up-to-date database.find
→ Slow but powerful; can match on almost anything.
👉 Pro tip: If you’re not sure which tool to reach for, start with locate
for speed. If it doesn’t cut it, switch to find
for more control.