Quick Reference for grep Command to search for string(s)

Learn to search strings recursively across files and directories, use multiple patterns, perform case-insensitive searches, and display line numbers in your search results.

Basic Parameters Explanation

-r – Recursively searches through all directories and subdirectories.

-e – Specifies multiple search patterns.

-i – Performs a case-insensitive search.

-n – Displays line numbers with the matching lines.

Command: Search Recursively for a String

Searches all files within a directory and its subdirectories for "search_string".

grep -r "search_string" /path/to/directory

Command: Specify Multiple Patterns Recursively

Searches for multiple patterns across all files in a directory and its subdirectories.

grep -r -e "pattern1" -e "pattern2" /path/to/directory

Command: Case-Insensitive Search in a File

Searches for "error" in a file, ignoring case differences.

grep -i "error" filename.txt

Command: Recursive and Case-Insensitive Search

Searches all files in /var/log for "error", ignoring case, and searching through all subdirectories.

grep -ri "error" /var/log

Command: Search with Line Numbers

Displays the line numbers along with the lines that match "search_string" in all files within a directory and its subdirectories.

grep -rn "search_string" /path/to/directory

Post a comment