π 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 :
