Exercise
Output and Redirection Exercise
Overview
In this hands-on exercise, you will compare the
overwrite operator (>) with the append
operator (>>). You will deliberately
overwrite one file, then create a second file and add
lines without replacing its existing content.
- Target host
servera
-
Connect to the lab host
From your lab workstation, connect to
serveraand clear the terminal.ssh student@servera -
Create an important file
Open a new file in Vim.
vim evil_plan.mdPress i to enter Insert mode, then enter the following text.
== Plan for World Domination! == 1. Make humanity reliant on AI by having students use AI to do their homework ... 13. Poison the training data for all new AIs ... 96. Become the supreme-leader of the world!Press Esc to return to Normal mode. Type :, enter
wq, and press Enter to write the file and quit Vim. -
Verify the file
Display the file and confirm that it contains your plan.
cat evil_plan.md -
Redirect output with the overwrite operator
Try to add one last step by redirecting the output from
echowith a single greater-than sign.echo "97. Call mom and tell her the good news about world domination" > evil_plan.mdDisplay the file again.
cat evil_plan.md -
Create a new file and append content
Start a second plan. Use
>once to create the file, then use>>to append the next two lines.echo "== Eviler Plan for World Domination! ==" > evil_plan-v2.md echo "1. Invent a <Top Secret Information Redacted>" >> evil_plan-v2.md echo "2. Use the <Top Secret Information Redacted> to poison the minds of <Top Secret Information Redacted>" >> evil_plan-v2.mdDisplay the second file and confirm that all three lines remain.
cat evil_plan-v2.md -
Append one more line
Use
>>again to add another line without replacing the existing plan.echo "3. Call mom and tell her the good news about world domination" >> evil_plan-v2.mdVerify the result.
cat evil_plan-v2.md -
Clean up
Remove both files created during the exercise.
rm evil_plan.md evil_plan-v2.md