← Back to all posts

Save a Linux Handover Note Without Losing Earlier Lines

Use shell redirection to replace a practice note, append a reminder, and verify every saved line.

A terminal shows commands that save a cafe handover note and append a reminder.

When a note needs both a fresh starting section and one later reminder, the redirection operator matters. A single > replaces a file; >> adds to the end of the file that is already there.

This LabEx video uses a small cafe handover to make the difference visible. The task is to save the first two menu lines, add Check milk., and read back the complete note.

A Linux terminal shows a handover file with Coffee, Tea, and Check milk on separate lines.

Replace the old practice note

Start with a menu.txt file that contains three lines. To put just the first two lines into handover.txt, run:

head -n 2 menu.txt > handover.txt

head -n 2 writes the first two lines to standard output. The single greater-than sign redirects that output into handover.txt. If that file already contains an old note, its old contents are replaced.

At this point, the handover file contains:

Coffee  $3
Tea     $2

Append a reminder without replacing the menu lines

Now add the reminder with two greater-than signs:

printf 'Check milk.\n' >> handover.txt

printf produces the reminder and its newline. >> appends that output after the existing contents, so the two menu lines stay in place. If this command used > instead, the file would contain only the reminder.

Verify the saved note

Read the complete file before handing it over:

cat handover.txt

The expected result is:

Coffee  $3
Tea     $2
Check milk.

This is a useful pattern whenever a command builds the first part of a file and a later command adds a small follow-up. Use > deliberately when the old file should be replaced, and use >> when existing lines must remain.

Practice the redirect operators

Try the same workflow in LabEx’s Linux Commands course, then change the reminder and run cat handover.txt again. For the narrated walkthrough, watch Mission: Leave a Cafe Handover Note.