How do I search for a literal error message inside a Linux text file?
grep locates matching lines in text files. Use -F when the search term should be treated literally rather than as a regular expression. Restrict the input to the relevant file or directory so a routine search does not scan every customer’s data or overwhelm the server.
Before you start
- An authorised SSH session on the Linux server.
Instructions
1. Search an exact file
Replace the message and file with the incident you are investigating. Quotes preserve spaces and shell punctuation in the search phrase.
grep -nF -- "connection refused" "$HOME/server-notes/report.txt"-n adds line numbers; -F treats characters such as dots or brackets as plain text. Matching is case-sensitive by default.
2. Include a little surrounding context
Context can identify which request or service produced a matching line. Use a small limit and redact sensitive identifiers before sharing excerpts.
grep -nF -C 3 -- "connection refused" "$HOME/server-notes/report.txt"-C 3 adds up to three lines on either side. Add -i only if case differences are irrelevant to your search.
Check the result
- Matching line numbers and surrounding events explain the error. An exit status of 1 normally means no matches; it is different from a read or syntax error.
Troubleshooting
- An expected message is absent
- Check the file, log rotation and time window, then try a shorter literal phrase.
- Permission denied appears
- Request access to that specific log; do not widen file permissions just to search it.
Official references
Use the documentation for your installed version when a flag, service name or platform feature differs.
