Skip to content

Following a changing file

Module
Reading and Editing Text
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab container), GNU Bash 5.2.21; Ubuntu 24.04.4 LTS (course VM)
On this page

You run a command to watch a log. Its first line appears, but the prompt does not return. Nothing else happens for several seconds. You need to know whether the terminal is stuck or still doing its job.

By the end, you can follow a file as another terminal adds lines, recognize the wait, and stop the watcher without changing the file.

Finish Viewing short and long files (B07.1). Keep your existing B04 practice directory and earlier exercises.

Use only ~/lab/b07/follow. Run the setup once; use Reset this lesson before repeating it.

Terminal window
mkdir -p ~/lab/b07/follow
cd ~/lab/b07/follow
printf 'Sample log begins\n' > events.log

This creates a disposable log containing one starting line. The > setup writes that file, as in B04; do not point it at a log you need to keep.

You need two terminal windows. Each has its own Bash, as in B03.2, but both can address the same file in your home directory. Call them left and right so you always know which one receives a command.

In the right terminal, run:

Terminal window
cd ~/lab/b07/follow
tail events.log
Captured output
Sample log begins

Only one line exists, so tail prints that line instead of ten. Your prompt returns. This is a completed read, like the tail commands in B07.1. Appending to the file afterward will not make this completed command print again.

Predict first: with the same one-line file, will adding -f make the prompt return immediately?

Terminal window
tail -f events.log
Run it, then compare with your guess

The starting line appears again, but the prompt does not return. -f means follow: after printing the end, tail keeps running and waits for more data to be appended. A quiet watcher is not the same as a shell ready for another command.

Leave that right window alone. Do not type echo there: Bash is waiting for tail to finish.

In the left terminal, run:

Terminal window
cd ~/lab/b07/follow

Then run these lines one at a time, pausing briefly between them:

Terminal window
echo 'sample: started' >> events.log
echo 'sample: checking' >> events.log
echo 'sample: complete' >> events.log

Copy each line as it is; >> adds a line to the end of the file, and B09 explains it. Here echo supplies the quoted text. Its output goes into events.log, so the left terminal returns to its prompt without printing the message there.

Look at the right window after each append. The watcher displays each line as it becomes available. It can take a short interval; this is not a promise of an exact display time.

Two terminal windows: echo append commands on the left; Sample log begins and three sample messages on the right.
The right terminal is running tail -f events.log while the left appends lines. Ubuntu 24.04.4 LTS, course VM. Scroll inside the image on narrow screens.
  1. The left terminal appends three sample lines to events.log.
  2. The right terminal is running tail -f events.log; the command line is outside this crop. The same three lines arrive in order.

The screenshot’s command line is outside the right-window crop. That window is running tail -f events.log; its displayed lines were captured on the course VM, not recreated as a replay. The left window has a prompt after each append, while the watcher has none.

Writer and watcher share one file The writer appends sample: started, sample: checking, then sample: complete to events.log. The tail -f watcher displays those same lines in that order. Arrows mean an appended line becomes visible to the watcher; timing is omitted. Writer terminalecho ... >> events.log1. sample: started2. sample: checking3. sample: complete Watcher terminaltail -f events.log1. sample: started2. sample: checking3. sample: complete events.log holds the lines; the writer is not printing them.Each arrow means the appended line becomes visible to the watcher.
Writer and watcher share one file The writer appends sample: started, sample: checking, then sample: complete to events.log. The tail -f watcher displays those same lines in that order. Arrows mean an appended line becomes visible to the watcher; timing is omitted. Writer terminalecho ... >> events.log1. sample: started2. sample: checking3. sample: complete Watcher terminaltail -f events.log1. sample: started2. sample: checking3. sample: complete Same file, same order
Simplified model. The writer appends three lines to events.log; the watcher displays those same lines in the same order.

In words: the left terminal writes sample: started, then sample: checking, then sample: complete into one shared file. The right terminal reads those additions. The diagram shows line order, not lines printed by the writer or exact polling and buffering delays.

Click the right terminal and press Ctrl+C. Then run:

Terminal window
pwd
Captured output
/home/learner/lab/b07/follow
After the three sample messages, the right window shows ^C, pwd, its directory, and a shell prompt.
Ctrl+C stops tail -f in the right terminal and returns control to Bash. Ubuntu 24.04.4 LTS, course VM. Scroll inside the image on narrow screens.
  1. Ctrl+C stops the watcher; ^C marks the interruption.
  2. pwd runs in Bash and prints /home/learner/lab/b07/follow; the prompt returns.

The interruption stops tail, not the left shell and not the file. pwd is a useful check: it runs only after Bash has regained control. In the screenshot, the terminal prints ^C before the prompt.

Read the finished file in either window:

Terminal window
cat events.log
Captured output
Sample log begins
sample: started
sample: checking
sample: complete

All four lines remain. Stopping a reader did not remove the lines the writer added.

Typing in the watcher window. A terminal without a shell prompt is still occupied by tail. Use the other window to append. Stop the watcher with Ctrl+C before asking Bash to run another command in its window.

Using another directory. A relative filename is resolved from each shell’s own current directory. Both windows need cd ~/lab/b07/follow. The fact that one window changed directory does not move the other one.

Using > instead of >>. The exercise needs additions to the existing log. > replaces its contents and can produce truncation behavior rather than the sequence shown here. Keep the exact append lines; do not shorten them by removing a greater-than sign.

Expecting every kind of log change. This exercise appends to one existing file. Programs can also truncate, replace, or rotate logs. Those cases need more detail than this first follow exercise; do not infer that every log-management operation behaves like these three appends.

Goal: Watch a different disposable file, append one completion message, and prove that you can return to Bash.

In the left window, still in follow, prepare:

Terminal window
printf 'Review begins\n' > review.log

In the right window run tail -f review.log. Use the left window to add review: complete with the same append pattern. Stop the watcher, then read the saved file.

Expected: The completion message reaches the watcher, both lines remain saved, and the right shell accepts another command after Ctrl+C.

Hint

Both windows must use the same directory and filename. The append command belongs in the window with a Bash prompt.

Solution

Right window:

Terminal window
tail -f review.log

Left window:

Terminal window
echo 'review: complete' >> review.log

Press Ctrl+C in the right window, then read the file:

Terminal window
cat review.log
Captured output
Review begins
review: complete

The method is the same as the captured events example; this challenge uses a separate file so it does not alter that evidence.

First press Ctrl+C in any terminal still following this lesson’s logs. Return to the parent and inspect the target:

Terminal window
cd ~/lab/b07
ls -AR follow

After the challenge it contains events.log and review.log.

Terminal window
rm -r ~/lab/b07/follow

Rerun Before you start. If follow is absent, skip removal. Return both windows to the recreated directory before repeating the exercise.

  • Plain tail finishes its read and returns to Bash.
  • tail -f prints the end and waits for additions; quiet output does not mean a stuck shell.
  • A second terminal can append while the first watches the same file.
  • Ctrl+C stops the watcher and leaves the file available to read.

Next, Editing with Nano changes an existing note inside an editor.

  • On your system: man tail and help echo.
  • GNU Coreutils: tail invocation, including follow behavior and the distinction between following a descriptor and a name.
  • GNU Bash: Redirections, the reference for >>; B09 teaches it step by step.