Overwrite To A File
Part of the Fundamentals section of Coddy's Terminal journey. Lesson 38 of 82.
Now that you understand standard output, let's redirect it somewhere useful. The > operator takes the output of a command and writes it to a file instead of displaying it on screen.
echo "Hello" > greeting.txtThis creates a file called greeting.txt containing the word Hello. Nothing appears on screen because the output went into the file instead.
You can redirect the output of any command:
ls > filelist.txt
cat names.txt > backup.txt
date > timestamp.txtWarning: The > operator overwrites the file completely. If the file already exists, its previous contents are erased and replaced with the new output.
echo "First line" > notes.txt
echo "Second line" > notes.txt
cat notes.txtOutput:
Second lineThe first line is gone because the second command overwrote the entire file. This behavior is powerful but requires caution. You can accidentally lose data if you redirect to an existing file.
Challenge
EasyUse the operator to redirect output into a file. Run these two commands in order:
- Use
echoto write the textSystem initializedto a file calledstatus.txt - Use
catto display the contents ofstatus.txt
Hint: The format is
echo "text" > filename. After redirecting, nothing appears on screen. You need to usecatto verify the file was created.
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 PipelinePractice on your own: Terminal playground