Mission 4 of 30
Writing and Reading File Content
Write a short note, append to it, and confirm the result.
Write text, append text, read files, reverse lines, and save command output.
Your outcome: You understand the difference between creating a file and putting content into it.
Practice inside
Practice inside
~/linux-practice. Check your setup before you begin.
Prerequisite: Creating and Inspecting Files
.
What you’ll practice
- Use echo and cat
- Understand overwrite vs append
- Use tee to view and save output
The mental model
- > overwrites a file; >> appends to it.
- Ctrl+D ends interactive cat input.
- tee is useful when you want to see output and save it.
Try it in your terminal
Write and append
Safe in practice folderCreates a file and appends another line. Remember: > overwrites.
Run from ~/linux-practice
cd ~/linux-practice
echo 'First line' > journal.txt
echo 'Second line' >> journal.txt
cat journal.txt Number and reverse lines
Safe in practice folderShows numbered lines and reverse order.
Run from ~/linux-practice
cat -n journal.txt
tac journal.txt Save output with tee
Safe in practice folderSaves a directory listing while also displaying it.
Run from ~/linux-practice
ls -la | tee listing.txt
cat listing.txt Put it into practice
- Create journal.txt with three lines.
- Use cat -n and tac to inspect it.
Check your result
The saved file contains both the original and appended lines.
Common mistakes
- You append to a file with `>>` for twenty minutes, then accidentally type `>` once. Your file now has one line. I've done this. The fix: when the file matters, copy it first. `cp journal.txt journal.txt.bak` before any redirect.
Check your understanding
Explain these in your own words. If you’re unsure, revisit the examples before moving on.
- Which operator appends?
- What does tee do?
Your practice, your pace
Loading saved progress…