04 Viewing File Content

Learning objectives

  • View a text file directly.
  • Page through large files.
  • View a file’s beginning, end, and live appended content.

Command overview

CommandPurposeMeaningBasic usageExample
catPrint an entire fileShort for concatenate; commonly prints a small file directlycat filecat README.md
lessPage through a fileSuitable for browsing a large file, with scrolling and searchless fileless app.log
headView a file’s beginningShows the first 10 lines by defaulthead [options] filehead -n 20 app.log
tailView a file’s endShows the final 10 lines by default and can follow changestail [options] filetail -f app.log
wcCount newlines, words, bytes, or charactersBy default shows newline, word, and byte counts; -m counts characterswc [options] filewc -l app.log

Common scenarios

View a small file:

cat config.txt

Page through a large log:

less app.log

View the beginning and end:

head -n 20 app.log
tail -n 50 app.log

Follow newly appended log content:

tail -F -- app.log

Count lines in a file:

wc -l app.log

Notes for beginners

  • cat is not suitable for opening a very large log directly because it can flood the terminal.
  • In less, press /keyword to search, n for the next match, and q to quit.
  • tail -f occupies the terminal continuously; press Ctrl-c to stop it.
  • GNU tail -F retries by name and handles common log rotation. Options are not identical on every platform.
  • wc -l counts newline characters; the final text segment without a trailing newline does not increase that count. wc -c counts bytes and wc -m counts characters.
  • Logs can contain user input, tokens, internal addresses, and command lines. Do not publish an entire log directly.

Log-viewing exercise

On the fictional log from the integrated project, compare head -n 2, tail -n 2, wc -l, and less. State what range each command reads, whether it keeps running, and whether its output belongs in a report.

References