How to use the SSH grep command

The SSH grep command searches for text patterns in files and displays matching lines. The name stands for “Get Regular Expression and Print.” In particular, it’s essential for finding specific strings in logs, config files, or code. Furthermore, grep supports regular expressions, case-insensitive search, line numbers, and counting matches – making it one of the most useful tools for server management via SSH.

Basic search

To search for text in a file, run grep followed by the pattern in quotes and the filename:

grep "textToSearchFor" file.txt

Grep prints every line that contains the search string. For example, to find “database” in your WordPress config:

grep "DB_NAME" wp-config.php

Case-insensitive search

Use -i to ignore uppercase and lowercase:

grep -i "error" error_log

This matches “error”, “Error”, “ERROR”, and any other casing.

Invert match (exclude lines)

Use -v to invert the match show lines that do not contain the pattern:

grep -v "comment" file.txt

Useful for filtering out noise—for example, excluding comment lines from config output.

Word matching

By default, grep matches substrings. Use -w to match whole words only – equivalent to applying a word-boundary (\b) search:

grep -w "word" file.txt

This matches “word” but not “password” or “keyword”. Prevents false matches when searching for short terms.

Show line numbers

Use -n to display the line number of each match:

grep -n "pattern" file.txt

Helps you locate matches quickly when editing files. You can combine options: grep -in "error" error_log for case-insensitive search with line numbers.

Count occurrences

Use -c to count how many lines match instead of showing them:

grep -c "404" access_log

Useful for quick statistics how many lines contain the string. Note that -c counts matching lines, not total occurrences if the pattern appears multiple times on one line.

Search multiple files

SSH grep command can search across many files at once. Use a wildcard or combine with find:

grep "pattern" *.php

To search recursively in all subdirectories, use grep -r "pattern" /path/to/dir/. For finding files by name first, see our guide on find and locate.

Common use cases

Typical uses for SSH grep command on a Linux hosting account:

  • Searching error logs for specific messages or IP addresses
  • Finding configuration values in wp-config.php or .htaccess
  • Locating function or variable names in PHP files
  • Filtering log output to see only relevant entries

Combine grep with other commands using pipes—for example, cat error_log | grep -i "fatal" or tail -100 access_log | grep "404". See our guides on cat and head and tail for more.

Need more help?

Explore more SSH and hosting guides in our knowledgebase. JetHost web hosting includes SSH access for managing your server files directly.