Git & Command Line Tutorial · Command Line Basics · Lesson 4 of 20

Viewing and Searching Files

Read files with cat, head, tail and less; count with wc; search text with grep and find files with find.

Create a free account to save this page, take notes and track your progress. Sign in

Reading files

  • cat file prints a whole file; less file lets you scroll (press q to quit, /word to search).
  • head -n 5 file / tail -n 5 file show the start / end; tail -f app.log follows a growing log.
  • wc -l file counts lines (-w words, -c bytes).

Searching

  • grep "error" app.log prints matching lines. Useful options: -i ignore case, -n line numbers, -r search folders recursively, -c count, -v invert, -E extended regular expressions.
  • find . -name "*.md" finds files by name; add -type f (files) or -type d (folders), -mtime -1 (changed in the last day), -size +1M.
head, tail, wc, grep and find
cat > app.log <<'EOF'
2026-01-15 10:00:01 INFO  server started on port 8080
2026-01-15 10:00:07 INFO  GET /home 200
2026-01-15 10:00:09 ERROR database timeout after 5s
2026-01-15 10:01:12 WARN  slow request GET /search 2.3s
2026-01-15 10:02:40 ERROR payment provider returned 503
2026-01-15 10:03:05 INFO  GET /home 200
EOF
mkdir -p docs/api && touch docs/intro.md docs/api/auth.md docs/api/notes.txt

head -n 2 app.log
echo "--- last line:"; tail -n 1 app.log
echo "--- lines: $(wc -l < app.log)"
echo "--- errors with line numbers:"; grep -n "ERROR" app.log
echo "--- count of GET requests: $(grep -c "GET" app.log)"
echo "--- not INFO:"; grep -v "INFO" app.log | cut -c21-
echo "--- markdown files:"; find docs -name "*.md" | sort

Output

2026-01-15 10:00:01 INFO  server started on port 8080
2026-01-15 10:00:07 INFO  GET /home 200
--- last line:
2026-01-15 10:03:05 INFO  GET /home 200
--- lines: 6
--- errors with line numbers:
3:2026-01-15 10:00:09 ERROR database timeout after 5s
5:2026-01-15 10:02:40 ERROR payment provider returned 503
--- count of GET requests: 3
--- not INFO:
ERROR database timeout after 5s
WARN  slow request GET /search 2.3s
ERROR payment provider returned 503
--- markdown files:
docs/api/auth.md
docs/intro.md

Run this on your computer — see “Get Started” for setup.

Test yourself

Which command shows the lines of app.log that contain 'error', ignoring case?

  1. find error app.log
  2. grep -i error app.log
  3. cat -i error app.log
  4. search error app.log
Show answer

Answer: grep -i error app.log

Was this page helpful?