Lesson 3: Viewing & Reading Files

Goal: Master the different ways to read and inspect file contents.


Table of Contents

  1. Display Entire File — cat
  2. Create Files with cat
  3. Page Through Files — less & more
  4. First Lines — head
  5. Last Lines — tail
  6. Count — wc
  7. Compare Files — diff
  8. Exercises

Display Entire File — cat

cat (concatenate) displays the entire content of a file.

Basic usage

cat myfile.txt

With line numbers

cat -n myfile.txt

Output:

     1  #!/bin/bash
     2  echo "Hello World"
     3
     4  echo "Goodbye"

Show non-printing characters

# Show tabs as ^I and end-of-line as $
cat -A myfile.txt

Output:

#!/bin/bash$
echo "Hello World"$
^Iecho "indented with tab"$

This is useful for debugging whitespace issues.

Concatenate multiple files

cat file1.txt file2.txt file3.txt

Combine files into one

cat header.txt body.txt footer.txt > complete.txt

Create Files with cat

You can use cat with a heredoc to create files with content.

Write text to a new file

cat > notes.txt << EOF
This is line 1.
This is line 2.
This is the last line.
EOF

Append text to an existing file

cat >> notes.txt << EOF
This line was appended.
And this one too.
EOF

Verify the content

cat -n notes.txt

Output:

     1  This is line 1.
     2  This is line 2.
     3  This is the last line.
     4  This line was appended.
     5  And this one too.

Page Through Files — less & more

For large files, cat dumps everything at once. Use less or more to read page by page.

less /var/log/syslog

Navigation in less:

Key Action
Space / f Next page
b Previous page
Enter / j Next line
k Previous line
g Go to beginning
G Go to end
/pattern Search forward
?pattern Search backward
n Next search result
N Previous search result
q Quit
-N Toggle line numbers
F Follow mode (like tail -f)

less with line numbers

less -N /var/log/syslog

more (older, simpler)

more /var/log/syslog

more can only scroll forward. Use less for full navigation — as the saying goes: "less is more".


First Lines — head

head shows the first lines of a file (default: 10 lines).

head /etc/passwd

Show a specific number of lines

# First 5 lines
head -n 5 /etc/passwd

# First 20 lines
head -n 20 /var/log/syslog

Show first N bytes

# First 100 bytes
head -c 100 largefile.bin

Use with multiple files

head -n 3 file1.txt file2.txt

Output:

==> file1.txt <==
line 1
line 2
line 3

==> file2.txt <==
first line
second line
third line

Last Lines — tail

tail shows the last lines of a file (default: 10 lines).

tail /var/log/syslog

Show a specific number of lines

# Last 20 lines
tail -n 20 /var/log/syslog

# Everything from line 50 onward
tail -n +50 longfile.txt

Follow a file in real time

This is one of the most useful commands for monitoring logs:

tail -f /var/log/syslog

The terminal stays open and shows new lines as they are written. Press Ctrl + C to stop.

Follow with retry (if file doesn't exist yet)

tail -F /var/log/app.log

Combine head and tail

Get lines 10-20 of a file:

head -n 20 file.txt | tail -n 11

Count — wc

wc (word count) counts lines, words, and characters.

Full count

wc myfile.txt

Output:

  42  318 1847 myfile.txt

That's: 42 lines, 318 words, 1847 bytes.

Count specific things

# Lines only
wc -l myfile.txt

# Words only
wc -w myfile.txt

# Characters only
wc -m myfile.txt

# Bytes only
wc -c myfile.txt

Count lines in multiple files

wc -l *.txt

Output:

  42 notes.txt
  18 readme.txt
 103 report.txt
 163 total

Practical examples

# How many files in a directory?
ls | wc -l

# How many users on the system?
wc -l /etc/passwd

# How many running processes?
ps aux | wc -l

Compare Files — diff

diff shows the differences between two files.

Setup example files

cat > version1.txt << EOF
Hello World
This is a test
Goodbye
EOF

cat > version2.txt << EOF
Hello World
This is a modified test
Goodbye
See you later
EOF

Basic diff

diff version1.txt version2.txt

Output:

2c2
< This is a test
---
> This is a modified test
3a4
> See you later
  • < = lines from file 1
  • > = lines from file 2
  • 2c2 = line 2 changed
  • 3a4 = after line 3, line 4 was added

Side-by-side comparison

diff -y version1.txt version2.txt

Output:

Hello World                       Hello World
This is a test                  | This is a modified test
Goodbye                           Goodbye
                                > See you later

Unified format (like Git)

diff -u version1.txt version2.txt

Output:

--- version1.txt
+++ version2.txt
@@ -1,3 +1,4 @@
 Hello World
-This is a test
+This is a modified test
 Goodbye
+See you later

Color output

diff --color version1.txt version2.txt

Compare directories

diff -r dir1/ dir2/

Exercises

Exercise 1: Reading files

# 1. Display /etc/hostname
cat /etc/hostname

# 2. Show /etc/passwd with line numbers
cat -n /etc/passwd

# 3. View the first 5 lines of /etc/passwd
head -n 5 /etc/passwd

# 4. View the last 3 lines
tail -n 3 /etc/passwd

Exercise 2: Working with wc

# 1. Create a test file
cat > sample.txt << EOF
The quick brown fox
jumps over the lazy dog.
Linux is awesome.
The terminal is powerful.
EOF

# 2. Count lines, words, and characters
wc sample.txt

# 3. Count only lines
wc -l sample.txt

# 4. How many files are in /etc/?
ls /etc/ | wc -l

Exercise 3: Monitor a log file

# Terminal 1: Start following syslog
tail -f /var/log/syslog

# Terminal 2: Generate a log entry
logger "This is a test message"

# Watch it appear in Terminal 1, then Ctrl+C to stop

Key Takeaways

  • cat — display or concatenate files; create files with heredoc
  • less — read large files interactively (use / to search)
  • head -n N — first N lines
  • tail -n N — last N lines; tail -f for live log monitoring
  • wc -l — count lines (most common use)
  • diff -u — compare files in unified format

Next Lesson: Lesson 4: Text Editors →