Linux tail command
About tail
tail outputs the last part, or "tail", of files. It can also monitor new information written to the file in real time, displaying the newest entries in a system log, for example.
Syntax
tail [{-c |--bytes=}num] [-f] [--follow[={name|descriptor}]] [-F] [{-n |--lines=}num] [--max-unchanged-stats[=num]] [--pid=pid] [{-p|--quiet|--silent}] [--retry] [{-s |--sleep-interval=}num] [{-v|--verbose}] [file ...]
tail --help
tail --version
Description
By default, tail prints the last 10 lines of each file to standard output. If you specify more than one file, each set of output is prefixed with a header showing the file name.
If no file is specified, or if file is a dash ("-"), tail reads from standard input.
Options
Option | Description |
---|---|
-c [+]num, --bytes=[+]num |
Output the last num bytes of each file. You can also use a plus sign before num to output everything starting at byte num. For instance, -c +1 will print 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 will cause tail will loop forever, checking for new data at the end of the file(s). When new data appears, it will be printed. If you follow more than one file, a header will be printed to indicate which file's data is being printed. If the file shrinks instead of grows, tail will let you know with a message. If you specify 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 | "Follow and retry". Same as using --follow=name --retry. |
-n num, --lines=num |
Output the last num lines, instead of the default (10). If you put a plus sign before num, tail will output all lines beginning with that line. For example, -n +1 will print every line. |
--max-unchanged-stats=num | If you are following a file with -f or --follow=name, tail
continuously checks the file to see if its size has changed. If the
size has changed, it reopens the file and looks for new data to print.
The --max-unchanged-stats option reopens a file, even if its size has not changed, after every num checks. This option is useful if the file might be spontaneously unlinked or renamed, such as when log files are automatically rotated. |
--pid=pid | When following with -f or --follow, terminate operation after process ID pid 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 following with -f or --follow, sleep for approximately num seconds between file checks. With --pid=pid, check process pid at least once every num seconds. |
-v, --verbose |
Always print headers. |
--help | Display a help message, and exit. |
--version | Display version information, and exit. |
Examples
tail myfile.txt
Outputs the last 10 lines of the file myfile.txt.
tail -n 100 myfile.txt
Outputs the last 100 lines of the file myfile.txt.
tail -f myfile.txt
Outputs the last 10 lines of myfile.txt, and monitors myfile.txt for updates; tail then continues to output any new lines that are added to myfile.txt.
Tip: tail will follow the file forever. To stop it, press CTRL + C.
tail -f access.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 the file access.log. It pipes access.log's final ten lines, and any new lines added, to the grep utility. grep reads the output from tail, and outputs only those lines which contain the IP address 24.10.160.10.
Related commands
cat — Output the contents of a file.
head — Display the first lines of a file.
more — Display text one screen at a time.
pg — Browse page by page through text files.
ref:
https://www.computerhope.com/unix/utail.htm