DaleSchool

Reading File Contents

Beginner25min

Learning Objectives

  • Print entire file contents with cat
  • View parts of a file with head and tail
  • Save output to files using redirection (>, >>)
  • Explain the difference between pipes (|) and redirection

Working Code

Example 1: Reading files with cat

First, create a sample file:

echo "Line one" > sample.txt
echo "Line two" >> sample.txt
echo "Line three" >> sample.txt
echo "Line four" >> sample.txt
echo "Line five" >> sample.txt

Read the file:

cat sample.txt

Output:

Line one
Line two
Line three
Line four
Line five

With line numbers:

cat -n sample.txt

Output:

     1	Line one
     2	Line two
     3	Line three
     4	Line four
     5	Line five

Example 2: Viewing just the beginning or end

# First 3 lines
head -3 sample.txt

Output:

Line one
Line two
Line three
# Last 2 lines
tail -2 sample.txt

Output:

Line four
Line five

Example 3: File statistics with wc

wc -l sample.txt

Output:

       5 sample.txt
# Compare line counts across files
wc -l sample.txt Documents/notes.md

Output:

       5 sample.txt
       3 Documents/notes.md
       8 total

Try It Yourself

Redirection: Sending Output to a File

> and >> send a command's output (stdout) to a file.

# > : write to file (overwrites existing content)
echo "Hello" > greeting.txt
cat greeting.txt

Output:

Hello
# >> : append to file (preserves existing content)
echo "Nice to meet you" >> greeting.txt
echo "Welcome" >> greeting.txt
cat greeting.txt

Output:

Hello
Nice to meet you
Welcome

Summary:

  • > : creates or overwrites the file
  • >> : appends to the end (preserves existing content)

Pipes: Feeding Output into Another Command

The pipe (|) passes the left command's output as input to the right command.

# Pass cat output to grep
cat Documents/notes.md | grep "item"

Output:

- item 1
- item 2
# Count lines in ls output
ls | wc -l

Output:

       6

"Why?" — Pipes vs. Redirection

Both "send" data, but in different directions:

Pipe (|):          command -> command  (data goes to a program)
Redirection (>):   command -> file     (data gets saved to a file)
Redirection (>>):  command -> file     (appended to a file)

Compare with examples:

# Pipe: pass ls output to grep (displayed on screen)
ls | grep ".txt"

# Redirection: save ls output to a file
ls > file-list.txt

# Combined: ls -> filter with grep -> save to file
ls | grep ".txt" > txt-list.txt

Choosing the Right File Reading Command

| Command | When to Use | Real-World Example | | --------- | ---------------------------- | --------------------------- | | cat | View short files entirely | Config files, short logs | | head | View the beginning of a file | CSV headers, start of logs | | tail | View the end of a file | Recent log entries | | tail -f | Monitor logs in real time | Watching server logs | | wc -l | Count lines | Log volume, file size check |

Using head and tail in Practice

# Last 10 lines of a log (default)
tail /var/log/system.log

# Last 50 lines
tail -50 /var/log/system.log

# Follow in real time
tail -f /var/log/system.log

# Check just the header (first line) of a CSV
head -1 data.csv

Common Mistakes

Mistake 1: Overwriting with >

echo "Important content" > notes.txt
# ... later ...
echo "New content" > notes.txt   # previous content is gone!

Use >> when you want to append. Default to >> when you want to keep existing content.

Mistake 2: Using cat on large files

cat /var/log/system.log   # tens of thousands of lines flood the screen

Use less to scroll through large files, or head/tail to view parts.

Mistake 3: Confused pipe direction

# Correct: left -> right
cat file.txt | grep "word"

# Wrong: this doesn't work as expected
grep "word" | cat file.txt

Pipes always flow from left to right.

Deep Dive

Scrolling through long files with less

less is a pager that lets you view large files page by page:

less /etc/hosts

Controls inside less:

  • Up/Down or j/k — move one line
  • Space — page down
  • b — page up
  • G — jump to end
  • 1G or gg — jump to beginning
  • /searchterm — search (n for next, N for previous)
  • q — quit

Unlike cat which dumps everything and exits, less lets you explore interactively.

Concatenating files (the original purpose of cat)

cat is short for conCATenate:

# Print contents of two files together
cat file1.txt file2.txt

# Merge two files into a new file
cat file1.txt file2.txt > combined.txt

This is what cat was originally designed for. It's commonly used to read files, but it's actually a "concatenate and output to stdout" command.

stderr: Separating error messages

Command output comes in two flavors:

  • stdout (standard output, normal results)
  • stderr (standard error, error messages)
# Redirect stderr to a file
command 2> error.log

# Redirect both stdout and stderr to a file
command > output.log 2>&1

# Redirect stdout and stderr to separate files
command > output.log 2> error.log

2> redirects stderr, > redirects stdout.

  1. Append echo "Line 1", echo "Line 2", echo "Line 3", echo "Line 4", echo "Line 5" to lines.txt using >>.
  2. View the result with cat -n lines.txt to see line numbers.
  3. Print the first 3 lines with head -3 lines.txt and the last 2 with tail -2 lines.txt.
  4. Count lines with wc -l lines.txt.
  5. Run cat lines.txt | grep "Line" and check the result.

Q1. Which redirection appends new content while keeping the existing file contents?

  • A) echo "content" > file.txt
  • B) echo "content" >> file.txt
  • C) echo "content" | file.txt
  • D) echo "content" => file.txt