Start with a known suffix
Tail copies the end of each named file to standard output. With no filename, it reads standard input. It is a standalone utility, not a Bash built-in, and its default selection is the final ten logical lines or line segments. A final segment does not need to end with a newline to count: tail includes it but does not add the missing newline. Tail preserves the input's existing order; it does not parse timestamps or decide which records are most important.
Create a disposable directory for the examples. If tail-lab already exists, choose another name. Every setup command is joined with &&, so a failed directory creation or write stops the remaining redirects instead of placing files in the caller's directory. The shell's > operator creates or replaces a file. Printf's %s inserts each following string, and \n appends a newline. The octal escapes in the utf8.txt command create the two UTF-8 bytes for é. Later examples run from tail-lab.
The final command gives tail one filename and no options. Events.log contains twelve lines, so only lines three through twelve appear.
mkdir tail-lab &&
cd tail-lab &&
printf '%s\n' \
'01 INFO boot' \
'02 INFO config loaded' \
'03 WARN retry=1' \
'04 INFO ready' \
'05 INFO request=alpha' \
'06 ERROR auth user=alex' \
'07 INFO retry accepted' \
'08 WARN disk=81%' \
'09 INFO rotate begin' \
'10 INFO rotate end' \
'11 ERROR auth user=sam' \
'12 INFO cache warm' > events.log &&
printf '%s\n' \
'ACCEPT user=alex' \
'REJECT user=alex' \
'ACCEPT user=sam' \
'REJECT user=sam' \
'ACCEPT user=lee' > auth.log &&
printf 'cafe\303\251\n' > utf8.txt &&
printf '%s\n' \
'WARN source delayed' \
'ERROR source unavailable' \
'INFO source restored' > ./-alerts.log &&
tail events.log03 WARN retry=1
04 INFO ready
05 INFO request=alpha
06 ERROR auth user=alex
07 INFO retry accepted
08 WARN disk=81%
09 INFO rotate begin
10 INFO rotate end
11 ERROR auth user=sam
12 INFO cache warmUse -n when the count is part of the question. In tail -n 3 events.log, -n selects line-count mode and 3 is its argument; the result is lines 10 through 12. A ten-line or three-line suffix is a bounded view, not proof that nothing relevant happened earlier.
Choose an origin, not only a count
A plus sign changes a count into a starting position. In -n +9, -n still selects lines, while +9 means begin with line nine and continue through the end. The plus sign is part of tail's option argument; the shell does not add anything.
This form is useful when a parser has already committed the first eight records or when a header occupies a known number of lines. It is specified by POSIX, but the selected position still refers to the file as tail reads it. Inserting or deleting earlier records changes which content is line nine.
tail -n +9 events.log
tail -n 2 -- -alerts.log09 INFO rotate begin
10 INFO rotate end
11 ERROR auth user=sam
12 INFO cache warm
ERROR source unavailable
INFO source restoredThe second command introduces --, the option terminator. Everything after it is an operand, so the dash-leading filename -alerts.log is not parsed as options. Its final two lines follow the first command's four lines on the same standard output.
A byte suffix can split text
The -c option changes the unit from lines to bytes. Utf8.txt ends with é followed by newline. In this controlled UTF-8 fixture, é is the two bytes c3 a9 and newline is 0a. Asking for two bytes therefore begins with the second half of é, not with a complete character.
Od renders bytes instead of sending the malformed suffix to the terminal. Its -A n option suppresses address labels, -t x1 displays one-byte hexadecimal units, and -v prevents repeated rows from being abbreviated. The wc deep dive explains why byte, character, line, and display-width counts answer different questions.
tail -c 2 utf8.txt | od -A n -t x1 -v
tail -c 3 utf8.txt | od -A n -t x1 -v a9 0a
c3 a9 0aThe first suffix is not valid UTF-8 text because it starts with a continuation byte. The second contains the complete é plus newline. A byte bound is appropriate for exact storage or protocol questions, but it does not preserve text characters or structured records automatically.
Keep several suffixes attributable
When GNU tail receives more than one file, it normally prints a header before each file's suffix. Here -n 2 applies separately to events.log and auth.log, not to their combined lines.
GNU -q, also spelled --quiet, suppresses those headers. Compact output can be useful for a homogeneous stream, but it discards provenance. If two sources can emit similar records, retain the default headers or add a machine-readable source field before merging them.
tail -n 2 events.log auth.log
tail -q -n 1 events.log auth.log==> events.log <==
11 ERROR auth user=sam
12 INFO cache warm
==> auth.log <==
REJECT user=sam
ACCEPT user=lee
12 INFO cache warm
ACCEPT user=leeThe quiet command's final two records have no labels. Their content happens to differ in this fixture; that is not a reliable source identifier.
Follow a known producer, then stop
The -f option keeps tail running after it reaches the current end and copies data appended later. An endless follower is awkward in automation, so this GNU example ties the follower to a known writer process.
The shell's ( ... ) & runs the parenthesized writer in the background. The special parameter $! expands to that background process's ID, saved in writer. The shell's >> redirection appends to a file instead of replacing it as > does. Tail's GNU --pid option tells -f to stop shortly after that process exits. The -n 0 selection suppresses any content already present. On this Linux build, inotify normally notifies tail about file changes; because --pid is also present, -s 0.05 makes tail check the writer PID at least every 0.05 seconds. On a system where tail must poll the file, -s also sets that check interval. Wait then collects the writer's completion status. The sleeps make the fixture's order reproducible; they are not production latency guarantees.
: > follow.log
( sleep 0.15
printf '%s\n' 'INFO worker ready' >> follow.log
sleep 0.15
printf '%s\n' 'ERROR token rejected' >> follow.log
) &
writer=$!
tail --pid="$writer" -n 0 -f -s 0.05 follow.log
wait "$writer"INFO worker ready
ERROR token rejectedThe shell's : command does nothing successfully; its > redirection creates or truncates follow.log before tail opens it. This example observes a controlled writer. In a service script, --pid is useful only when that PID really represents the producer whose lifetime should bound the watch.
Rotation changes the identity question
GNU tail -f normally follows the open file descriptor. If a log is renamed, that descriptor still refers to the renamed file. A newly created file at the old pathname is a different object. This is useful when the writer keeps the old descriptor open, but it can miss records written only to the replacement path.
The first controlled writer renames app.log to app.log.1, creates a new app.log, and then appends to the renamed file. Descriptor-following prints only old descriptor. The second writer repeats the sequence with app2.log. GNU -F is equivalent to --follow=name --retry: it watches the pathname and reopens it after replacement, while also tolerating temporary disappearance. During detection it can still read a final append from the old descriptor, so the observed output contains both records.
printf '%s\n' 'old baseline' > app.log
( sleep 0.12
mv app.log app.log.1
printf '%s\n' 'new path' > app.log
printf '%s\n' 'old descriptor' >> app.log.1
sleep 0.35
) &
writer=$!
tail --pid="$writer" -n 0 -f -s 0.03 app.log
wait "$writer"
printf '%s\n' 'old baseline' > app2.log
( sleep 0.12
mv app2.log app2.log.1
printf '%s\n' 'new path' > app2.log
printf '%s\n' 'old descriptor' >> app2.log.1
sleep 0.35
) &
writer=$!
tail --pid="$writer" -n 0 -F -s 0.03 app2.log 2>/dev/null
wait "$writer"old descriptor
old descriptor
new pathThe second tail's 2>/dev/null redirection hides expected rotation diagnostics only to keep this synthetic transcript stable; 2> redirects standard error. Do not suppress such diagnostics blindly in monitoring. Neither -f nor -F reconstructs records lost before tail opened a file, and copy-truncate rotation has different failure windows from rename-and-create rotation.
Wait for a pathname that does not exist yet
A service may create its log after a watcher starts. GNU --follow=name makes the pathname, rather than only the initial descriptor, the object of interest. --retry keeps trying when the file is inaccessible. Without --retry, a missing initial path can make tail stop.
A readiness handshake makes this transcript independent of which background process the scheduler starts first. The writer's while loop waits until the empty delayed.ready sentinel exists. The test [ ! -e delayed.ready ] is true while that path does not exist, and do ... done encloses the commands repeated by while. The parent launches tail in the background too, saving its PID as follower. The > delayed.out and 2> delayed.err redirections capture standard output and standard error separately. The parent repeats the loop while [ ! -s delayed.err ] is true. The -s test becomes true once the file has nonzero size; ! negates that result, so the loop stops after tail emits its first diagnostic. The : > delayed.ready command creates the sentinel and releases the writer. Wait collects each background process, and cat displays the captured data. On this Linux build, inotify normally signals file changes; with --pid present, -s 0.05 makes tail check the writer PID at least every 0.05 seconds.
( while [ ! -e delayed.ready ]; do sleep 0.01; done
printf '%s\n' 'INFO created' > delayed.log
sleep 0.15
printf '%s\n' 'WARN changed' >> delayed.log
sleep 0.30
) &
writer=$!
tail --pid="$writer" --retry --follow=name -n 0 -s 0.05 delayed.log > delayed.out 2> delayed.err &
follower=$!
while [ ! -s delayed.err ]; do sleep 0.01; done
: > delayed.ready
wait "$writer"
wait "$follower"
cat delayed.outINFO created
WARN changedThe initial 'cannot open' and later 'has appeared' messages remain in delayed.err because their wording can vary with timing and translation. In an operational watcher, keep or route diagnostics so an unavailable path is observable. The sentinel coordinates this laboratory example; it is not a production log-readiness protocol.
Polling is not delivery
GNU -s sets the sleep interval between checks when tail must poll a file. On Linux, inotify normally signals file changes, so that file-polling interval is usually ignored; when --pid is present, tail still checks the named process at least once per interval. A smaller interval can reduce one source of delay at the cost of more wakeups. It cannot make an application flush its own user-space buffer, force a filesystem or network mount to expose bytes, or guarantee that a rename is detected without a gap.
Follow mode also does not impose record boundaries beyond what reaches the file. A writer can append half a line, pause, and append the rest later. If downstream processing requires complete JSON objects, transaction boundaries, or authenticated messages, tail alone does not establish those contracts. Treat -s as a check interval, not an end-to-end delivery promise.
Triage a recent authentication window
During an authorized assessment or incident review, a bounded suffix can answer a narrow first question: which rejection records occur among the four most recent authentication lines in this supplied artifact? Tail selects that window, and grep keeps lines containing the literal text REJECT. The shell's | operator sends tail's standard output to grep's standard input. The grep article develops pattern matching and exit-status behavior beyond this literal filter.
This reads the synthetic auth.log without changing it. Keep the original file and record the exact selection command when the result informs an investigation. A match in the suffix is evidence about that selected window, not proof of compromise; absence from the suffix says nothing about earlier records.
tail -n 4 auth.log | LC_ALL=C grep 'REJECT'REJECT user=alex
REJECT user=samLC_ALL=C gives grep a simple byte-oriented locale for this ASCII fixture. The four-line input window excludes only the first ACCEPT record. On a rotated log set, establish file order and coverage before describing any suffix as 'recent.'
Define the claim before the command
Before relying on tail, decide what existing order means, whether the unit is a line or a byte, whether a pathname or an open file object should survive rotation, and how the watcher will stop. For live data, decide which layer owns buffering and where diagnostics go.
Tail is dependable when the conclusion stays as narrow as the selection: these are the last N units currently available from this input, or these are bytes observed after this follower opened this file identity. It becomes misleading when 'last' silently turns into 'complete,' 'most important,' 'delivered immediately,' or 'the whole incident.'