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
  1. Connect to the lab host

    From your lab workstation, connect to servera and clear the terminal.

    ssh student@servera
  2. Create an important file

    Open a new file in Vim.

    vim evil_plan.md

    Press 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.

  3. Verify the file

    Display the file and confirm that it contains your plan.

    cat evil_plan.md
  4. Redirect output with the overwrite operator

    Try to add one last step by redirecting the output from echo with a single greater-than sign.

    echo "97. Call mom and tell her the good news about world domination" > evil_plan.md

    Display the file again.

    cat evil_plan.md
  5. 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.md

    Display the second file and confirm that all three lines remain.

    cat evil_plan-v2.md
  6. 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.md

    Verify the result.

    cat evil_plan-v2.md
  7. Clean up

    Remove both files created during the exercise.

    rm evil_plan.md evil_plan-v2.md