learn.colinkim.dev

Viewing and inspecting file contents

Learn to quickly read and inspect files in the terminal using cat, less, head, tail, wc, file, and stat — including how to follow live log files.

A terminal user should be able to look inside any file without leaving the command line.

cat README.md

cat reads a file and prints it to the terminal. It is fast and simple.

cat file1.txt file2.txt    # concatenate and print multiple files
cat -n file.txt            # show line numbers

For large files, cat scrolls too fast to read. Use less instead.

Page through a file — less

less large-file.txt

less opens a file in a scrollable viewer. This is the right tool for anything bigger than a screenful.

Navigation:

  • j / k or arrow keys — scroll down / up
  • Space — page down
  • b — page up
  • g — go to the top
  • G — go to the bottom
  • /pattern — search forward
  • q — quit

View the beginning or end — head and tail

head file.txt          # first 10 lines
head -n 20 file.txt    # first 20 lines

tail file.txt          # last 10 lines
tail -n 5 file.txt     # last 5 lines

Following live logs with tail -f

tail -f server.log

tail -f keeps the file open and prints new lines as they are appended. This is how you watch a log file in real time.

Stop with Ctrl-C.

Count lines, words, characters — wc

wc file.txt            # lines, words, bytes
wc -l file.txt         # just lines
wc -w file.txt         # just words
wc -c file.txt         # just bytes

Useful for quick checks:

wc -l access.log       # how many log entries?

What kind of file is this? — file

file image.png
file script.sh
file data.bin

file inspects a file’s contents (not just the extension) and reports what type it is.

image.png:  PNG image data, 800 x 600, 8-bit/color RGBA
script.sh:  POSIX shell script, ASCII text executable
data.bin:   data

This is useful when an extension is missing or misleading.

File metadata — stat

stat file.txt

stat shows metadata: file size, permissions, owner, creation time, modification time.

  File: file.txt
  Size: 1234        Blocks: 8          IO Block: 4096   regular file
  Access: 2025-01-15 10:30:00.000000000 +0000
  Modify: 2025-01-14 09:00:00.000000000 +0000
  Change: 2025-01-14 09:00:00.000000000 +0000

Line endings: plain text vs binary

Text files use invisible characters to mark line endings:

  • Unix (macOS, Linux): LF (\n)
  • Windows: CRLF (\r\n)

This usually does not matter, but it can cause issues when scripts written on one platform run on another. If a script fails with “bad interpreter” or strange ^M characters appear in output, line endings are likely the cause.

Binary files (images, compiled programs, archives) should not be printed with cat — they will produce garbled output. Use file to identify them first.

The main idea to carry forward

Use the right viewer for the job: cat for small files, less for anything larger, head/tail for the edges, tail -f for live logs, wc for counts, file for type identification, and stat for metadata. These tools give you complete visibility into any file.

Quick Check

One answer

Which command is usually the best first choice for reading a large text file without dumping the whole thing into the terminal at once?

Choose the best answer and use it to track your progress through the lesson.

Progress

Quick checks

No quick checks in this lesson.

Mark lesson manually or answer quick checks to track progress.