πŸ” Linux/Unix: Searching, Text Processing & Regex
Estimated reading: 3 minutes 28 views

πŸ“‚ 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 find for detailed and recursive searches
  • How to use locate for fast lookups via indexing
  • How which and whereis help 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

CommandPurposeSpeedUses Indexed DBSearches
findReal-time file searchSlower❌ NoBy name, size, type, perms
locateFast indexed file lookupFastβœ… YesBy name only
whichPath of command in $PATHFastβœ… Yes (PATH)Executables only
whereisPath to binary, source, and manFastβœ… YesCommands/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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

πŸ”΅ Linux/Unix: File Location (find, locate, which, whereis)

Or Copy Link

CONTENTS
Scroll to Top