Analyze log files on the command line
Updated · Every command on this page ran in our practice shell
Short answer
Use tail -n 50 app.log for the latest lines, grep ERROR app.log to filter, and grep -c to count. For the most common values, chain cut -d' ' -f5 app.log | sort | uniq -c | sort -rn | head. For JSON, jq '.[] | select(.status == "error")' filters records.
You don't need a dashboard to find out what a server has been doing. A handful of commands, chained with pipes, answer most questions about a log: what happened last, how many errors, which pages are busiest, what went wrong at 09:15. The examples below ran in a practice shell on a small web-app log and two JSON files; try your own variations in the mini terminal.
See the latest lines with tail
New events are appended to the end of a log, so tail -n 20 app.log shows the 20 most recent. On a live server, tail -f app.log keeps the file open and prints new lines as they arrive until you press Ctrl+C; the practice shell has no live logs, so it doesn't support -f.
learner@shellstreak:~$ tail -n 5 logs/app.log 2026-03-02 09:17:52 WARN GET /old-menu 404 2ms 2026-03-02 09:19:03 INFO GET /menu 200 13ms 2026-03-02 09:20:15 INFO POST /order 200 47ms 2026-03-02 09:21:40 INFO GET /hours 200 9ms 2026-03-02 09:23:02 INFO GET /menu 200 11ms
Find errors with grep
grep ERROR app.log keeps only matching lines. Add -i to ignore case, -v to show everything except the matches, and -c to count them instead of printing them.
learner@shellstreak:~$ grep ERROR logs/app.log 2026-03-02 09:06:55 ERROR POST /order 500 2003ms 2026-03-02 09:10:33 ERROR POST /order 500 2010ms 2026-03-02 09:15:09 ERROR POST /order 500 2001ms
Count with grep -c and wc -l
grep -c pattern file prints the number of matching lines. Put spaces around short patterns like status codes so 500 doesn't also match 1500ms. For counts in the middle of a pipeline, end with wc -l.
learner@shellstreak:~$ grep -c ' 500 ' logs/app.log 3
Top N: sort | uniq -c | sort -rn | head
This is the most useful pipeline for logs. Cut out one field (here the 5th, the path), sort so equal values are next to each other, uniq -c to count each one, sort -rn to put the biggest numbers first, and head to keep the top few. To list each value once, without counts, sort -u sorts and drops repeats. Forgetting the first sort is the classic bug: uniq only merges lines that are adjacent.
learner@shellstreak:~$ cut -d' ' -f5 logs/app.log | sort | uniq -c | sort -rn | head -n 3 7 /menu 6 /order 4 /hours
A time window with grep
When lines start with a timestamp, a prefix match selects a window. grep ' 09:1' matches every time from 09:10:00 to 09:19:59. The leading space anchors it to the hours-and-minutes position, so seconds like 09:09:12 don't sneak in. For an exact range across hours, awk '$2 >= "09:10" && $2 < "09:20"' compares the time field as text.
learner@shellstreak:~$ grep ' 09:1' logs/app.log 2026-03-02 09:10:33 ERROR POST /order 500 2010ms 2026-03-02 09:11:48 INFO POST /order 200 51ms 2026-03-02 09:12:05 WARN GET /old-menu 404 2ms 2026-03-02 09:13:20 INFO GET /hours 200 8ms 2026-03-02 09:14:41 INFO GET /menu 200 12ms 2026-03-02 09:15:09 ERROR POST /order 500 2001ms 2026-03-02 09:16:30 INFO GET /hours 200 6ms 2026-03-02 09:17:52 WARN GET /old-menu 404 2ms 2026-03-02 09:19:03 INFO GET /menu 200 13ms
Filter on a number with awk
awk splits each line into fields $1, $2, and so on, and can compare them as numbers. Here $7 is the response time like 2003ms; $7+0 turns it into the number 2003, so you can find slow requests.
learner@shellstreak:~$ awk '$7+0 > 1000' logs/app.log 2026-03-02 09:06:55 ERROR POST /order 500 2003ms 2026-03-02 09:10:33 ERROR POST /order 500 2010ms 2026-03-02 09:15:09 ERROR POST /order 500 2001ms
JSON logs and data with jq
Many tools log JSON instead of plain lines. jq is grep and cut for JSON: jq '.' file pretty-prints it all, .[] steps into every item of a list, select(...) keeps the ones that match, and -r prints strings without quotes.
learner@shellstreak:~$ jq -r '.[] | select(.status == "refunded") | .item' data/orders.json rye sourdough
Count JSON items
length counts the items in a list. Wrap a filter in [ ... ] to collect its results into a list first, then count them.
learner@shellstreak:~$ jq '[.[] | select(.status == "pending")] | length' data/orders.json 2
Check yourself
One real exercise from the “Logs & data on the command line” unit. No account needed.Which paths get the most requests? Show the top 3, each with its count, busiest first. The path is the 5th field.
What's checked: the output of your last command.
Common mistakes
Running
uniq -con unsorted input. Equal values that aren't adjacent get counted separately;sortfirst.Matching too loosely:
grep 500also hits1500msor an order id. Add spaces or use a field withawk '$6 == 500'.Opening a multi-gigabyte log in an editor.
tail,grepandlessread it without loading it all into memory.Forgetting
-rin jq and then piping"Ben"(with quotes) into other commands.
Questions
How do I watch a log file live?+
`tail -f app.log` prints new lines as they're written, until you press Ctrl+C. Combine it with grep to watch only errors: `tail -f app.log | grep ERROR`. (The practice shell has no live files, so it doesn't support `-f`.)
How do I search compressed, rotated logs?+
Rotated logs are often gzipped (`app.log.2.gz`). `zgrep ERROR app.log.2.gz` searches them without unpacking, and `zcat` prints them for a pipeline.
How do I count errors per hour?+
Cut out the hour and count it: `grep ERROR app.log | cut -c12-13 | sort | uniq -c`. The `cut -c` positions depend on your timestamp format.
Reading is step one. Now make it stick.
Practise this in a five-minute lesson with instant feedback. No account needed.