Linux/Unix: File Location β find, locate, which, whereis Explained with Examples
Introduction β Why Learn File Location Commands in Linux/Unix?
On a Linux/Unix system, locating files quickly is essential for system administration, scripting, troubleshooting, and software development. Tools like find, locate, which, and whereis let you search files by name, type, location, and moreβsaving time and boosting productivity.
In this guide, youβll learn:
- How to use
findfor detailed and recursive searches - How to use
locatefor fast lookups via indexing - How
whichandwhereishelp identify command paths and binaries - Practical examples and output explanations
find β Locate Files in Real-Time
Syntax:
find [path] [options] [expression]
Description:
Searches directories recursively based on name, type, size, permissions, etc.
Examples:
find /home -name "*.txt" # Find all .txt files under /home
find . -type f -size +1M # Files larger than 1MB
find /etc -perm 644 # Files with specific permissions
find / -name "config.php" 2>/dev/null # Suppress permission errors
Real-time search, very accurate but slower for large file systems.
locate β Fast File Lookup Using Index
Syntax:
locate filename
Description:
Searches from an indexed database (/var/lib/mlocate/mlocate.db) for fast results.
Example:
locate passwd
Much faster than find, but results may be outdated unless the database is updated.
Update Database:
sudo updatedb
which β Show Executable Path in $PATH
Syntax:
which command
Description:
Displays the full path of a command or executable from the PATH variable.
Example:
which python
Returns something like:
/usr/bin/python
Helpful in scripts to verify whether a command is available.
whereis β Locate Binary, Source, and Manual
Syntax:
whereis command
Description:
Locates the binary, source code, and man page locations for a command.
Example:
whereis ls
Output:
ls: /bin/ls /usr/share/man/man1/ls.1.gz
Useful for system auditing and documentation lookup.
Comparison Table
| Command | Purpose | Speed | Uses Indexed DB | Searches |
|---|---|---|---|---|
find | Real-time file search | Slower | No | By name, size, type, perms |
locate | Fast indexed file lookup | Fast | Yes | By name only |
which | Path of command in $PATH | Fast | Yes (PATH) | Executables only |
whereis | Path to binary, source, and man | Fast | Yes | Commands/binaries |
Summary β Recap & Next Steps
Locating files efficiently is key to system management. Whether you need to find a config file, check if a command exists, or inspect a manual pathβfind, locate, which, and whereis offer precise tools for the task.
Key Takeaways:
find= real-time recursive search with filters.locate= lightning-fast indexed search.which= path to executable in current$PATH.whereis= binary, source, and man page locations.
FAQs
When should I use find vs locate?
Use find when you need real-time and filtered results. Use locate for quick file name lookups.
How do I update locate results?
Run:
sudo updatedb
How do I find files owned by a specific user?
Use:
find / -user username
Whatβs the difference between which and whereis?
which shows the commandβs location in $PATH. whereis shows binary + source + man pages.
Can I search by file size using find?
Yes:
find . -size +10M
Finds files larger than 10 MB.
Share Now :
