Have you ever misplaced a configuration file or forgotten which script contained a specific password or URL on your Linux server? Searching through hundreds of folders manually is exhausting. Luckily, Linux provides two super-powered search companions: find and grep.
The Detective Duo: find vs grep
To understand the difference, remember this simple real-world analogy:
- find is the Librarian: It searches for the book cover (file names, sizes, or folders).
- grep is the Magnifying Glass: It searches inside the pages of a book (text inside files).
Locating Files with find
When you know the name or partial name of a file, find tracks it down instantly starting from any directory:
# Search in the current directory (.) for any file ending in .conf
find . -name "*.conf"
# Search without caring about uppercase or lowercase letters (-iname)
find . -iname "readme.txt"The dot (.) means "start looking from where I am right now", and -name tells it the filename pattern to match.
Searching Inside Files with grep
What if you know the text inside, but forgot the file name? That is where grep shines:
# Search for the word 'database' inside config.php
grep "database" config.php
# Search recursively (-r) inside all files in the current folder, ignoring case (-i)
grep -rn "DB_PASSWORD" .The flag -r searches subfolders recursively, and -n shows the exact line number where your text was discovered!
3 Things to Remember
- Use find for File Names: When you need to find where a file lives on disk, reach for
find . -name "filename". - Use grep for Text Content: When you need to find words or code phrases inside files, use
grep -rn "text" .. - Combine Flags for Power: Adding
-iignores case sensitivity, saving you time when you cannot remember exact capitalizations.