tail: Follow a Growing Text File and Read Its Last Lines
tail prints the end of a file, and tail -f keeps watching it, printing new lines as they are added. Run tail -n 20 notes.log for the last 20 lines, or tail -f notes.log to follow the file until you press Control-C.
Quick answer
Without options, tail prints the last 10 lines. POSIX says so directly: if neither -c nor -n is given, -n 10 is assumed. The macOS and GNU versions agree.
tail notes.log # last 10 lines
tail -n 20 notes.log # last 20 lines
tail -f notes.log # last 10 lines, then wait for new onesStep-by-step
Read the last lines with -n
Give -n a number to change how many lines you get. On macOS, the number can also be written on its own, so tail -n 5 file.txt and tail -5 file.txt do the same thing.
tail -n 5 journal.txt
tail -5 journal.txtA plain number counts from the end. A leading plus sign counts from the start instead, so tail -n +2 data.csv starts at line 2. That makes it a quick way to skip a header row. POSIX counts from 1, so -n +1 is the whole file and -n -1 is the last line. GNU's manual words it as "skip NUM-1 lines at the start".
Use -c when you want bytes instead of lines. tail -c 100 file.txt prints the last 100 bytes, and tail -c +100 file.txt starts at byte 100.
If you pass several files, tail puts a header with the file name above each one. -q hides the headers. On GNU, -v always prints them, even for a single file.
Follow a growing file with -f
-f prints the last 10 lines and then waits for more. It never reaches the end on its own. Press Control-C when you're done.
tail -f /var/log/wifi.log
tail -40 -f /var/log/system.logThe second line shows the common pairing: start with 40 lines of context, then follow. Both examples come from the macOS reference page, and which log files exist on your Mac varies by system version.
On macOS, -f is ignored when the input is a pipe, so tail prints the last lines and exits. Point it at the file itself. If you are checking how long a file already is, count lines and words with wc first. If you would rather scroll and search a file than watch it, less has its own follow mode.
Use -F when the file gets rotated
Log files are often renamed and replaced on a schedule, which is called rotation. Plain -f can end up watching the old file. That's the case -F is for.
tail -F app.logOn macOS, -F implies -f, but tail also checks whether the name now points to a different file. It closes and reopens the file when the name gets a new inode number.
GNU tail works a little differently, and the default matters. Its manual says -f follows the file descriptor by default, so a renamed file is still tracked. For log rotation you want to follow the name instead, with --follow=name. GNU defines -F as --follow=name --retry, which also keeps trying if the file is missing for a moment.
GNU adds a few more follow options: --pid=PID exits once that process has ended, and -s N sets how many seconds to sleep between checks (1.0 by default). Those are on the GNU page and not on the macOS one, so run man tail on your own machine to see what it supports.
Common problems and fixes
- It prints a few lines and exits. You probably piped data in on macOS.
-fonly follows a real file. - It stops showing new lines after a log is rotated. Use
-Finstead of-f. - You need the whole file except a header. Use
tail -n +2 file, not a large-nnumber. - You want the last lines in reverse order. macOS has
-r, but BSDtailcan only reverse files that fit in its buffer, typically 32k. - You need a middle slice. Combine two commands:
head -50 file.txt | tail -10prints lines 41 to 50.
The same pipe trick works with other tools, for example sort and uniq on a list you pulled out with tail.
Doing this with Carets
tail watches a file from the Terminal. Carets is where you read and edit the file once you have a copy. It is a native notes and code editor for iPhone, iPad, and Mac, so a log excerpt or a config file you copied out of tail can be opened, cleaned up, and kept next to the rest of your notes.
Syntax highlighting helps with config and code files, and Markdown, plain text, and code live in one editor, organized into files, projects, and tags. Your files stay plain text, so they stay portable.
Carets is a lightweight editor, not a Terminal or a log viewer, and it does not follow a file as it grows. Use tail -f for that and Carets for the writing.
Download Carets for iPhone, iPad, and Mac on the App Store.
Frequently Asked Questions
How do I stop tail -f?
Press Control-C in the Terminal window. tail -f waits for new data and never finishes by itself. On GNU tail, --pid=PID with -f makes it exit when that process ends.
What is the difference between tail -f and tail -F?
On macOS, -F implies -f and also reopens the file when its name gets a new inode number, which happens when a log is rotated. On GNU tail, -F is the same as --follow=name --retry.
How do I print everything from line 2 onward?
Use tail -n +2 file.txt. A leading plus sign counts from the start of the file, so +2 starts at line 2.
Why does tail -f show nothing when I pipe into it?
On macOS, -f is ignored when standard input is a pipe, so tail prints the last lines and exits. Point tail at the file instead.