Tail display the last few lines of a file

Source: Internet
Author: User
Tags truncated

function: The tail command outputs the end of the file, which, by default, displays the last ten lines of the file.
Displays the last 10 lines of each specified file to the standard output. If more than one file is specified, the program adds the corresponding file name as the header at the beginning of each output. If you do not specify a file or file as "-", the data is read from the standard input.
It is often used to dynamically monitor the growth of the tail content of a file, for example to monitor changes in log files.

Options File
Short Options Long Options implication
-c[+] K --bytes=[+] K Output the last k -byte; In addition, use the-C +K output from the K -byte of each file
-N[+] K --lines=[+] K Output the last K line, instead of the last 10 lines; use-n +k output from the K -byte of each file
-F --follow=descriptor
--follow=name
Descriptor is the--follow default value, so-f equivalent--follow equivalent--follow=descriptor
Instant output of data appended to the file changes.
tail -f fileDynamically tracking the growth of file files, tail every second to check whether the files add new content. If the increment is appended, it is displayed after the original output. In this case, however, you must ensure that the file already exists when you execute the tail command.
If you want to terminate the TAIL-F output, press the Ctrl+C interrupt tail program. If the output is not interrupted by pressing CTRL + C, the killall tail can be executed on the other terminal forcibly terminated.
--pid=pid With-f use, when the PID corresponding process dies, terminates.
---retry Even if the destination file is inaccessible and still tries to open it, it is often used in conjunction with the parameter-F or--follow=name.
-F --follow=name--retry The same as-F, which is also a variation of the dynamic tracking file, differs from when you execute this command when the file does not exist.
--max-unchanged-stats=N n defaults to 5, using--follow=name to reopen a file that does not change size after N iterations to see if it is unblocked or renamed (this is the usual case of a circular log file).
This option is rarely used because of the inotify.
- S --sleep-interval=S is combined with-F, which indicates the sleep s seconds at each repetition interval
For--pid=pid , check the process every s second.
-Q --quiet or--silent Do not output file name
-V --verbose Always output the header of the file name
    • K can be followed with multiplication sign:
      B, KB, K, MB 1000*1000, M 1024*1024,gb 1000*1000*1000, G 1024*1024*1024, for T, P, E, Z, Y also apply.
    • If + k (bytes or lines), start with the K item starting at the beginning of each file. Otherwise, the last K entry for the file is displayed.
    • If you want to instantly track down a valid name for a file instead of describing it (such as a circular log), the default program action (--follow=descriptor) is not what you want. In this case, you can use the--follow=name option, which causes tail to periodically track open the file with the given name to confirm that it was deleted or recreated by some other program.
    • Press the Ctrl key and the C key on the keyboard at the same time to exit the display.

Tail instances
the contents of the test file are as follows[Email protected] tail_test]$cat tail_test1 04_librarytest 2 a.out 3 A.out.symbol 4 autotools 5 core.31058 6 cpp_test 7 Download_blog 8 gpg_test 9 hello-1. 0.tar.gz hello-2.0 one hello-2.0.tgz main.cpp mysql20110512.sql rsynctest all tail_ tail_test display file Last 10 lines [[email protected] tail_test]$tail tail_testshow the last 10 lines of the file Tail_test7 Download_blog 8 Gpg_test 9 hello-1.0.tar.gz hello-2.0 one hello-2.0.tgz main.cpp (mysql20110512.sql) RS Ynctest Tail_ Tail_test Show the last n rows of the file [email protected] tail_test]$tail -n 5 tail_test show the last 5 lines of the file Tail_testMain.cpp mysql20110512.sql rsynctest tail_ tail_test start displaying files from line 5th [email protected] tail_test]$tail -n +5 tail_testNote that the specified number precedes + represents the position starting at the corresponding location of the number, showing what follows5 core.31058 6 Cpp_test 7 Download_blog 8 gpg_test 9 hello-1.0.tar.gz ten hello-2.0 hello-2.0.tgz main.cpp 13 Mysql20110512.sql rsynctest tail_ tail_test Display the contents of the last 38 bytes of the file [email protected] tail_test]$tail -c 38 tail_testYnctest Tail_ Tail_test Displays the contents of the file after the beginning of the 38th byte [email protected] tail_test]$tail -c +38 tail_test3 A.out.symbol 4 Autotools 5 core.31058 6 cpp_test 7 Download_blog 8 gpg_test 9 hello-1.0.tar.gz hello-2.0 11 Hello-2.0.tgz main.cpp mysql20110512.sql rsynctest Tail_Note that the specified number precedes + represents the position starting at the corresponding location of the number, showing what follows. 

Trace files, tail-f two modes
 default is file descriptor, trace file growth  [[email protected] tail_test]$  tail-f tail_test     at this time is equivalent to the command --follow=descriptor tail_test    7  download_blog 8  gpg_test 9  hello-1.0.tar.gz 10  hello-2.0 11  hello-2.0.tgz 12  main.cpp 13  mysql20110512.sql 14  rsynctest 15   Tail_ 16  tail_test    here, after input, displays the next 10 lines of the default file, but tail does not end, but runs continuously, maintaining the open state of the corresponding index node for that file. Next   If you use  echo new >>tail_test , append new content to the file. Then you will see tail and continue to print the appended content.   If you use  echo new > Tail_test , empty the original content and re-write the new content. Then tail will output a word like "tail:tail_test:file truncated" to tell the contents of the file to be truncated.     

This command is used to: track dynamically growing files. such as system logs. By default, the file is tracked according to its own file descriptor.
However, some programs will delete files and create a new one when they append the contents of the file. For example, some log programs will rename the appended log file at some point, and then create a new log file with the same name to append the new content, so that the command is not helpful. For example, some editors, such as VIM, cannot track changes when they are modified. Through the "Ls-il" to vim edit before the file and the inode number of the file after the comparison found that the two different, should be edited when the index node to delete the file and then create a new, the new node content contains the latest content, and the previous tail opened the index node has been deleted, I can't see it, so of course it's not going to change.
If you want to be sure, use "Ps-aux |grep tail" to find the tail process number, go to the/proc process number directory in the tail directory, and look at one of the descriptors in its FD file, such as "Cat 3" to discover the original content.

tail --follow=name tail_test7 Download_blog 8 Gpg_test 9 hello-1.0.tar.gz hello-2.0 one hello-2.0.tgz main.cpp (mysql20110512.sql) RS Ynctest Tail_ tail_test This way, tail keeps track of file changes based on file names, and by default tail keeps track of files based on its own file descriptor, as previously stated, Some programs append file content when the file is deleted and then create a new one, for example, some log programs will be appended to the log file at a certain time to rename, and then create a new log file with the same name to append new content, the default way can not track the changes in the file, Because the file descriptor represents an index node, and the newly appended content may be appended to the new index node, the--follow=name option is used at this time.  Thus, if the new appended content of the file is appended to a different index node of the same name, then tail will prompt "tail:" Tail_test "has been replaced; Following end of new file, and then re-displays the last 10 rows after the append. 

Tail-f and Tail-f

The difference between tail-f and tail-f
The-f parameter, if the document is deleted, transferred, or rebuilt when the document is traced, stop and no longer output.
The-f parameter, if the document was deleted, transferred, or rebuilt when the document was traced, then the document with the same name will be re-try again, and if rebuilt, the document will continue to be traced.

Tail-f file
-F is the abbreviation for--follow[=how]. "[=how]" There are two words, one "=descriptor", the other is "=name". "--follow=descriptor" is used by default when [=how] is omitted. - F equivalent-follow equivalent--follow=descriptor. First window tail -f messages.3 Helll test2 Second window rm messages.3 rm:remove regular file ' messages.3 '? Y[[email protected] ~]# echo "Helll test3" >>m Essages.3 But the tail-f command of the first window does not appear hello Test3

Descriptor is not necessarily the most useful, although it is the default parameter. For example, when tail a log file, the file is likely to scroll by date or size, after the file is scrolled, the TAIL-F command expires.
If the file you are tracking is deleted, transferred, or rebuilt, you also want to continue tail it, you can use this tail--follow=name or tail-f

Tail-f file
-F is the abbreviation for-follow=name--retry. --follow=name is to follow the file name tracking files, you can periodically reopen the file to check whether the file is deleted by other programs and re-established. --retry This parameter ensures that the file can continue to be tracked after it is re-established. First window tail -F messages.3  Helll test1 Tail: ' messages.3 ' have become inaccessible:no such file or directory tail: ' Messages.3 ' has appeared; Following end of new file helll test2 Second window rm messages.3 rm:remove regular file ' messages.3 '? Y[[email protected] ~]# E Cho "Helll test3" >>messages.3 the first window can be seen, the middle of the deletion of messages.3, but re-created and input helll test2, will continue to display. 
Tail-f more powerful

Tail-f function is powerful, it is equivalent to--follow=name--retry. If the file you are tracking is moved or renamed, you also want to continue to tail it, you can use this option.
Tail manual page about--retry: Keep trying to open a file even if it's inaccessible when tail starts or if it becomes Inaccessi Ble later; Useful when following by name, i.e., with--follow=name. Tail the file does not exist when the command is started or the file is inaccessible during execution, it is retried continuously.
Description of--follow:-F,--follow[={name|descriptor}] output appended data as the file grows; -F,--follow, and--follow=descriptor are equivalent. --follow=descriptor indicates that the trace is a file descriptor, and--follow=name indicates that the file name is being traced.
If the file name is changed and you want to keep track of the trailing content of the original file name, you have to use the-f option instead of the-f option.

[Email protected] imx_server]#tail -F log/IMX.LOG 14:13:28.892  info imxconnection[6] Imx.server.imxconnection-rx imx_active_test{seq=3460,client_id= 1291343201649042,presence_status=1 (presence_status_online),} 14:13:28.892 DEBUG ImxConnection[6] org.logicalcobwebs.proxool.imxdb-006417 (01/02/00)-Connection #9 served 14:13:28.892  info ImxConnection[  6] imx.dbo.imxonlineinforow-exec SQL UPDATE imx_online_info SET last_active_time = ' 2010-12-03 14:13:28.0 ' WHERE account = ' Zhy '  14:13:28.894 DEBUG imxconnection[6] org.logicalcobwebs.proxool.imxdb-update imx_online_info SET last_ Active_time = ' 2010-12-03 14:13:28.0 ' WHERE account = ' zhy ';   (1 milliseconds)  14:13:28.894 DEBUG imxconnection[6] org.logicalcobwebs.proxool.imxdb-006417 (00/02/00)- Connection #9 returned (now AVAILABLE)  14:13:29.625  info imxconnection[6] Imx.server.imxconnection-rx IMX_ Active_test{seq=3461,client_id=1291343201649042,presence_status=1 (presence_status_online),} 14:13:29.626 DEBUG imxconnection[6] org.logicalcobwebs.proxool.imxdb-006418 (01/02/00)-Connection #8 served 14:13:29.626  info IMXCONNECTION[6] imx.dbo.imxonlineinforow-exec SQL UPDATE imx_online_info SET last_active_time = ' 2010-12-03 14:13:29.0 ' WHERE account = ' Zhy '  14:13:29.627 DEBUG imxconnection[6] org.logicalcobwebs.proxool.imxdb-update imx_online_ Info SET last_active_time = ' 2010-12-03 14:13:29.0 ' WHERE account = ' zhy ';   (0 milliseconds)  14:13:29.653 DEBUG imxconnection[6] org.logicalcobwebs.proxool.imxdb-006418 (00/02/00)- Connection #8 returned (now AVAILABLE)  Ctrl+C[Email protected] imx_server]#To summarize: to track the renamed log, use Tail-f instead of Tail-f

Tail display the last few lines of a file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.