SSH Commands – grep
On this page
The grep command is used to search for specific text inside files. It helps you quickly find matching lines in logs, configuration files, or any text-based file when working through SSH.
It is commonly used in server administration to locate errors, settings, or patterns without opening files manually.
How the command works
The basic structure of the command is:
grep "text" file.txt
This searches for the specified text inside a file and returns all lines that contain a match.
Common usage examples
Search for a word in a file
grep "error" error.log
This returns all lines containing the word error.
Case-insensitive search
grep -i "error" error.log
This finds matches regardless of uppercase or lowercase letters.
Show line numbers
grep -n "error" error.log
This displays matching lines together with their line numbers.
Invert match (exclude results)
grep -v "debug" log.txt
This shows all lines that do not contain the word debug.
Count occurrences
grep -c "error" log.txt
This returns the number of matching lines.
Searching in multiple files
You can also search across multiple files or entire directories:
grep -r "error" /path/to/directory
The -r option searches recursively through all subdirectories.
Practical implications
The grep command is especially useful when:
- analyzing server logs
- debugging application errors
- searching configuration files
- filtering output from other commands
For large directories, recursive searches (-r) may take longer to complete depending on the number of files.
Summary
The grep command allows fast text searching inside files and directories. It is a core tool for working efficiently in a Linux environment through SSH.
Didn't find an answer?
Our support team is here for you 24/7 – write to us and we will help you sort it out.