Following a changing file
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.
Before you start
Section titled “Before you start”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.
mkdir -p ~/lab/b07/followcd ~/lab/b07/followprintf 'Sample log begins\n' > events.logThis 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.
A finished read returns to Bash
Section titled “A finished read returns to Bash”In the right terminal, run:
cd ~/lab/b07/followtail events.logSample log beginsOnly 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?
tail -f events.logRun 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.
Append from the other window
Section titled “Append from the other window”In the left terminal, run:
cd ~/lab/b07/followThen run these lines one at a time, pausing briefly between them:
echo 'sample: started' >> events.logecho 'sample: checking' >> events.logecho 'sample: complete' >> events.logCopy 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.

- The left terminal appends three sample lines to events.log.
- 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.
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.
Stop watching without changing the log
Section titled “Stop watching without changing the log”Click the right terminal and press Ctrl+C. Then run:
pwd/home/learner/lab/b07/follow
- Ctrl+C stops the watcher; ^C marks the interruption.
- 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:
cat events.logSample log beginssample: startedsample: checkingsample: completeAll four lines remain. Stopping a reader did not remove the lines the writer added.
Mistakes to recognize
Section titled “Mistakes to recognize”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.
Try it yourself
Section titled “Try it yourself”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:
printf 'Review begins\n' > review.logIn 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:
tail -f review.logLeft window:
echo 'review: complete' >> review.logPress Ctrl+C in the right window, then read the file:
cat review.logReview beginsreview: completeThe method is the same as the captured events example; this challenge uses a separate file so it does not alter that evidence.
Reset this lesson
Section titled “Reset this lesson”First press Ctrl+C in any terminal still following this lesson’s logs. Return to the parent and inspect the target:
cd ~/lab/b07ls -AR followAfter the challenge it contains events.log and review.log.
rm -r ~/lab/b07/followRerun Before you start. If follow is absent, skip removal. Return both windows to the recreated directory before repeating the exercise.
- Plain
tailfinishes its read and returns to Bash. tail -fprints 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.
Further reading
Section titled “Further reading”- On your system:
man tailandhelp 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.