student@ubuntu:~$
shell 1/5 20 XP

Viewing File Contents

0%

Quick Reference

Command What It Does
cat file Display entire file at once
cat file1 file2 Display multiple files in sequence
less file Page through a file (scrollable)
head file Show first 10 lines
head -n 20 file Show first 20 lines
tail file Show last 10 lines
tail -n 20 file Show last 20 lines
tail -f file Follow file in real time (Ctrl+C to stop)
wc file Count lines, words, bytes
wc -l file Count lines only

When to Use What

Situation Tool
Short file (< 50 lines) cat
Long file you want to read through less
Just need the first few lines head
Just need the last few lines tail
Watching a live log tail -f
Need a line/word count wc

less Navigation

Key Action
q Quit
Space / Page Down Forward one screen
b / Page Up Back one screen
Arrow keys Scroll line by line
/pattern Search forward
?pattern Search backward
n Next search match
N Previous search match
g Go to beginning
G Go to end

How It Works

cat – Quick Dump

Terminal
student@ubuntu:~/cscd240$ cat hello.c
#include <stdio.h>

int main(void) {
printf("Hello, World!\n");
return 0;
}

head and tail – Just the Edges

Terminal
student@ubuntu:~$ head -n 3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin

student@ubuntu:~$ tail -n 2 /etc/passwd
student:x:1000:1000:Student:/home/student:/bin/bash
sshd:x:119:65534::/run/sshd:/usr/sbin/nologin

wc – Counting Without Reading

Terminal
student@ubuntu:~/cscd240$ wc hello.c
6 10 78 hello.c
# 6 lines, 10 words, 78 bytes

student@ubuntu:~/cscd240$ wc -l hello.c
6 hello.c

Common Pitfalls

  • cat on a binary file – dumps garbage characters that corrupt your terminal display. Type reset to fix it.
  • Stuck in less – press q. Not Ctrl+C, not Escape. Just q.
  • wc output order – it’s lines, words, bytes. Students constantly mix up the order.
  • head -n 30 | tail -n 11 – the math: you want lines 20-30, that’s 11 lines from the end of the first 30. Off-by-one errors are real.
  • tail -f won’t stop – press Ctrl+C to exit. It’s designed to run forever until you interrupt it.

Unlocks

Complete this skill to see what it unlocks.