GNU tail cheat sheet
Summary
GNU tail cheat sheet: view the end of files. Default shows last 10 lines. Key options: -n for lines, -c for bytes, -f to follow changes, -F for robust log watching. Useful for real-time log monitoring and parsing file data via the command line.
Introduction #
The GNU tail command is part of the GNU Core Utilities (coreutils) and is used to display the end of files. By default, it prints the last 10 lines of a file, but it offers several options to control the number of lines, the starting position, and whether the file should be continuously monitored for changes. It is frequently used for viewing log files in real time.
Synopsis #
The general syntax of the GNU tail command is:
tail [OPTION]... [FILE]...
OPTIONspecifies how the output should be displayed, such as the number of lines, bytes, or whether the file should be followed for changes.FILErepresents one or more input files. If no file is specified, tail reads from standard input.
Options #
Here is a table of commonly used GNU tail options:
| option | description |
|---|---|
-n NUM | Print the last NUM lines of the file. By default, tail prints 10 lines. |
-n +NUM | Print starting from line NUM to the end of the file. |
-c NUM | Print the last NUM bytes of the file instead of lines. |
-c +NUM | Print starting from byte NUM to the end of the file. |
-f | Output appended data as the file grows (follow mode). |
-F | Same as -f, but retry if the file is inaccessible or recreated (useful for log rotation). |
--max-unchanged-stats=N | With -F, reopen the file after N iterations if the file has not changed. |
--retry | Keep trying to open a file even if it is temporarily unavailable. |
-q, --quiet, --silent | Never output headers when operating on multiple files. |
-v, --verbose | Always output headers with file names. |
--pid=PID | With -f, terminate tail when the process with ID PID dies. |
--help | Display help text with all available options and exit. |
--version | Show version information and exit. |
-z, --zero-terminated | Line delimiter is NUL, not newline. Essential for safe handling of filenames. |
Examples #
Basic usage of tail #
To display the last 10 lines of a file:
tail filename.txt
Show a specific number of lines with -n #
Use the -n option to specify how many lines you want to see:
tail -n 20 filename.txt
This example prints the last 20 lines of filename.txt.
Follow file changes with -f #
To continuously display new lines as they are appended to a file, use:
tail -f filename.log
This is useful for monitoring log files that are actively written to.
Print from a specific line with -n +N #
To start printing from a specific line number N, use a plus sign:
tail -n +15 filename.txt
This prints the file starting from line 15 until the end.
Combine following with retry using -F #
The -F option is similar to -f, but it will also retry if the file is rotated or recreated:
tail -F filename.log
Monitor multiple files #
You can pass multiple files to tail:
tail -f file1.log file2.log
GNU tail will display headers to show which file each section of output belongs to.
Limit output size with -c #
You can specify output size in bytes using the -c option:
tail -c 100 filename.txt
This prints the last 100 bytes of the file.
Use with pipelines #
The tail command can be combined with pipes for processing output. For example, to view the last 50 lines of a log and search for the keyword “error”:
tail -n 50 filename.log | grep "error"
Extract a section of a file (head + tail) #
To print lines 11 through 20:
head -n 20 filename.txt | tail -n 10
Use tail with standard input #
The tail command can process data from a pipe, reading from standard input when no filename is given:
dmesg | tail -n 10
Handle NUL-separated input for safety #
Safely process filenames (or other data containing newlines) from find:
find . -name "*.log" -print0 | tail -z -n 3
Terminate tail after a process dies #
Follow a log file until the associated process (with PID 1234) exits:
tail -f --pid=1234 /path/to/app.log
FAQ's #
Most common questions and brief, easy-to-understand answers on the topic:
What does the GNU tail command do?
The GNU tail command prints the last part of a file, usually the last 10 lines by default.
How can I continuously monitor a log file with GNU tail?
You can use tail -f filename to follow new content as it is added to a file.
Can I use GNU tail on multiple files?
Yes, you can provide multiple filenames, and GNU tail will display headers to indicate which file each output belongs to.
How do I specify the number of lines shown with GNU tail?
You can use the -n option, for example tail -n 20 filename will display the last 20 lines.
Further readings #
Sources and recommended, further resources on the topic:
License
GNU tail cheat sheet by Jonas Jared Jacek is licensed under CC BY-SA 4.0.
This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, for noncommercial purposes only. To give credit, provide a link back to the original source, the author, and the license e.g. like this:
<p xmlns:cc="http://creativecommons.org/ns#" xmlns:dct="http://purl.org/dc/terms/"><a property="dct:title" rel="cc:attributionURL" href="https://www.ditig.com/tail-cheat-sheet">GNU tail cheat sheet</a> by <a rel="cc:attributionURL dct:creator" property="cc:attributionName" href="https://www.j15k.com/">Jonas Jared Jacek</a> is licensed under <a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="license noopener noreferrer">CC BY-SA 4.0</a>.</p>For more information see the Ditig legal page.