微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

linux tail

Linux tail command

Updated: 05/21/2018 by Computer Hope

About tail

tail outputs the last part, or "tail", of files. It can also monitor new @R_423_4045@ion 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 @R_423_4045@ion, 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.

@H_595_404@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.

cat — Output the contents of a file.
headdisplay the first lines of a file.
moredisplay text one screen at a time.
pgbrowse page by page through text files.

 

ref:

https://www.computerhope.com/unix/utail.htm

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。