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.
Reading files
cat fileprints a whole file;less filelets you scroll (pressqto quit,/wordto search).head -n 5 file/tail -n 5 fileshow the start / end;tail -f app.logfollows a growing log.wc -l filecounts lines (-wwords,-cbytes).
Searching
grep "error" app.logprints matching lines. Useful options:-iignore case,-nline numbers,-rsearch folders recursively,-ccount,-vinvert,-Eextended 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.
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?
find error app.loggrep -i error app.logcat -i error app.logsearch error app.log
Show answer
Answer: grep -i error app.log