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.txtOutput:
First line
Second lineBoth 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.logEach 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
EasyA file called tasks.txt already contains two tasks. Use the operator to add a third task to the file, then display the complete list.
- Append the text
Call the dentisttotasks.txt - Use
catto display all three tasks
Hint: Use
echo "text" >> filenameto 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.txtIf the file doesn't exist, creates it. Useful for building log files:
date >> activity.log
echo "User logged in" >> activity.logUse to overwrite a file, to accumulate data over time.
Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Directories
Create A DirectoryCopy A DirectoryMove And Rename A DirectoryDelete A DirectoryRecap - Directory Operations7File Content
Head And TailWord CountSort CommandUnique CommandGrep BasicsGrep With FlagsRecap - Text Detective2Navigation
Print Working DirectoryList FilesChange DirectoryAbsolute vs Relative PathsHome And Root DirectoryRecap - Find Your Way8Redirection
Standard OutputOverwrite To A FileAppend To A FileStandard InputStandard ErrorRecap - Log Builder6Wildcards And Patterns
The Star WildcardThe Question Mark WildcardBracket WildcardsCombining WildcardsRecap - Selective Operations9Piping
What Is A PipeChaining Two CommandsChaining Multiple CommandsPipe With GrepRecap - Data Pipeline