Finding Files in Linux
There are several ways to find files in Linux when you don’t know their exact location. This guide covers the most common tools and adds useful tips and examples.
1. find
If you have a rough idea of the directory tree where the file might be, you can use find:
find directory -name targetfile -printFor example:
find /home -name "*.txt" -print 2>/dev/null*.txt→ matches all text files2>/dev/null→ suppresses permission error messages- Quotes (
") → prevent the shell from expanding the*
Useful Options
-
By type:
find . -type f→ only files
find . -type d→ only directories -
By permissions:
find . -perm o=r→ files readable by others -
By size:
find . -size +10M→ files larger than 10 MB -
Execute commands on matches:
find . -name "*.txt" -exec wc -l '{}' ';'Counts the lines in every
.txtfile. -
Case-insensitive search:
find . -iname "*.jpg"
📌 For more details: man find or info find
2. which (sometimes whence)
If you can run a command, you can locate its binary on disk:
> which ls
/bin/lsUseful Alternatives
type ls→ shows if it’s a built-in, alias, or binarycommand -v ls→ POSIX-compliant alternative towhich
3. locate
locate uses a prebuilt index of filenames, making it much faster than find.
Example:
locate ".txt"This will list all files with .txt in their path.
Notes
-
The index is usually updated daily. To refresh it manually:
sudo updatedb -
May list files that have been deleted or miss very new files.
-
Unlike
find, it cannot filter by permissions, size, or type.
Comparison
| Command | Speed | Freshness of results | Search criteria | Example use case |
|---|---|---|---|---|
| find | Slow | Always up-to-date | Flexible (name, size, type, perms, exec) | General-purpose searching |
| which | Fast | Always up-to-date | Executables only | Find binary locations |
| locate | Very fast | May be outdated | Filename patterns only | Quickly list many matches |
Other Useful Tools
whereis <command>→ shows binary, source, and man page locationfd <pattern>→ a modern, faster alternative tofind(if installed)grep -rl <pattern> <dir>→ find files containing a text string