Apple Terminal
Read-only, can be slowSearch a Log for Errors With Context
A local log contains too many lines to manually scan for the relevant failure.
Command
grep -n -C 2 'ERROR' ./app.log
Before you run this
System impact: Read-only. Can create load on large logs, directories, filesystems, or process tables.
When not to use it: Do not use it for structured JSON analysis when jq would be more precise.
Expected output
Matching ERROR lines with two lines before and after each match.
System impact
Read-only, can be slow. Nothing changes. The command prints matching lines with line numbers and nearby context.
Scope this to the smallest useful path or service on busy systems.
Recovery / rollback: no state is changed.
When to use it
Use when debugging build logs, app logs, export logs, or CLI output saved to a file.
When not to use it
Do not use it for structured JSON analysis when jq would be more precise.
Watch this command run
Command transcript
This sanitized transcript shows the commands and output shape without exposing host details.
$ nl -ba project/logs/app.log | sed -n '1,8p'
1 server started
2 loading config
3 ERROR missing API_KEY
4 retry disabled
5 exit
$ grep -n -C 2 'ERROR' project/logs/app.log
1-server started
2-loading config
3:ERROR missing API_KEY
4-retry disabled
5-exit
View commands shown
These are the commands shown in the sanitized transcript.
Commands shown
nl -ba project/logs/app.log | sed -n '1,8p'grep -n -C 2 'ERROR' project/logs/app.log
next steps
Related commands
Watch a Log or Build File Update
Need to see whether a file is still changing? Let tail follow it live.
tail -f ./app.log
Show Context Around the First Error
The line before the error often explains the error.
grep -RInC 3 -m 1 'ERROR' artifacts logs
Scan Every CI Log for Error Lines
One grep pass can turn a log pile into a failure list.
grep -RInE 'error|failed|failure|exception|traceback' artifacts logs | head -50
Flush macOS DNS Cache
Changed DNS but your Mac still visits the old place? Flush the resolver cache.
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
Stop the Process Blocking a Dev Port
Free a stuck dev port without hunting through Activity Monitor.
lsof -ti tcp:3000 | xargs kill
Study mapping
Use this as independent command practice: read the notes, predict the output, then compare it with the example before using a real shell.
Useful for
- LPIC-1 style command-line practice
- LFCS style performance tasks
- Linux+ style troubleshooting review
Independent study support only. No affiliation, endorsement, exam dumps, or real exam questions.