Menu
Coddy logo textTech

Append To A File

Part of the Fundamentals section of Coddy's Terminal journey — lesson 39 of 82.

The > operator overwrites files, which can be dangerous when you want to keep existing content. The >> operator solves this by appending to a file instead of replacing it.

echo "First line" >> notes.txt
echo "Second line" >> notes.txt
cat notes.txt

Output:

First line
Second line

Both lines are preserved because >> adds new content at the end of the file. If the file does not exist, it creates a new one — just like > does.

This is perfect for building log files or collecting output over time:

date >> activity.log
echo "User logged in" >> activity.log
date >> activity.log
echo "User logged out" >> activity.log

Each command adds a new line to activity.log without erasing previous entries. Use > when you want a fresh start, and >> when you want to accumulate data.

challenge icon

Challenge

Easy

A file called tasks.txt already contains two tasks. Use the operator to add a third task to the file, then display the complete list.

  1. Append the text Call the dentist to tasks.txt
  2. Use cat to display all three tasks

Hint: Use echo "text" >> filename to append without erasing the existing content. The file should end up with three lines.

Cheat sheet

The operator appends to a file without overwriting existing content:

echo "First line" >> notes.txt
echo "Second line" >> notes.txt
cat notes.txt

If the file doesn't exist, creates it. Useful for building log files:

date >> activity.log
echo "User logged in" >> activity.log

Use to overwrite a file, to accumulate data over time.

Try it yourself

Terminal
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals