Linux Tail Command Help and Examples – Computer Hope

tail command

In Unix-like operating systems, the tail command reads a file and generates the last part of it (the “tail”).

The tail command can also monitor data streams and open files, displaying new information as it is typed. For example, it is a useful way to monitor the most recent events in a system log in real time.

This page covers the GNU/Linux version of tail.

Description

By default, tail prints the last 10 lines of each file to standard output. If you specify more than one file, each output set is prefixed with a header that displays the name of the file.

If no file is specified, or if the file is a hyphen (““), tail reads the standard input.

Queue syntax

[{-c | -bytes=}num] [-f] [-follow[={name| descriptor}]] [-F] [{-n | –lines=}num] [-max-unchanged-stats[=num]] [-pid=pid] [{-p| -silent| -silent}] [-retry] [{-s | -sleep interval=} num] [{-v| -detailed}] [file …] tail -help tail -version

Options Option Description -c [+]num,-bytes=[+]num Output of the last number of bytes in each file. You can also use a plus sign before num to generate everything that starts at the num byte. For example, -c +1 prints everything. A multiplier suffix can be used after num to specify units: b (512), kB (1000), K (1024), MB (1000*1000), M (1024*1024), GB (1000*1000*1000), G (1024*1024*1024), and so on for T (terabyte), P (petabyte), E (exabyte), Z (zettabyte), Y (yottabyte). –f,-follow[={name| descriptor}] This option causes the queue to repeat forever, looking for new data at the end of the files. When new data appears, it will be printed. If more than one file follows, a header will be printed to indicate what data in the file is being printed. If the file shrinks instead of growing, tail lets you know with a message. If you specify the name, the file with that name is followed, regardless of its file descriptor. If you specify descriptor, the same file is followed, even if it is renamed. This is the default behavior. -F “Go on and try again”. Same as using –follow=name -retry. -n num,-lines=num Generates the last number lines, instead of the default ones (10). If you place a plus sign before num, tail generates all lines that begin with that line. For example, -n +1 prints each line. –max-unchanged-stats=num If you are following a file with –f or -follow=name, tail continuously checks the file to see if it has changed. If the size has changed, reopen the file and search for new data to print. The –max-unchanged-stats option reopens a file, even if its size has not changed, after each numerical check. This option is useful if the file can be unlinked or renamed spontaneously, such as when log files are automatically rotated. -pid=pid When followed with –f or -follow, terminate the operation after the pid process ID dies. -q,-quiet,-silent Never output headers. -retry Keep trying to open a file even if it is temporarily inaccessible; useful with the -follow=name option. -s num,-sleep-interval=num When you follow –f or -follow, suspend for approximately num seconds between file checks. With -pid=pid, check the pid process at least once every num seconds. –v,-verb Always print headers. -help Display a help message and leave. -version Show version information and exit.

Examples

tail myfile.txt Generates the last 10 lines of the myfile.txt file. tail -n 100 myfile.txt Generates the last 100 lines of the myfile.txt file. tail -f myfile.txt Generates the

last 10 lines of myfile.txt and monitors

myfile.txt for updates; Tail then continues to generate the new lines that are added to MyFile.txt.

Access tail -f.log | grep 24.10.160.10

This is a useful example of using tail and grep to selectively monitor a log file in real time

.

In this command, tail monitors access to the file.log. It channels access.log the last ten lines, and any new lines added, to the grep utility. grep reads tail output and only generates those lines that contain the IP address 24.10.160.10.

Related commands

cat — Output the contents of a file.

head — Displays the first few lines of a file. more — Display text from one screen at a time. pg — Navigate page by page through text files.