Home >System Tutorial >LINUX >How to view the last few lines of a file in Linux

How to view the last few lines of a file in Linux

WBOY
WBOYforward
2023-12-28 23:41:271286browse

Rendering

linux查看文件的后几行(文件查看 如何显示最后几行 ,某几行)

linux查看文件的后几行(文件查看 如何显示最后几行 ,某几行)

tail -n 20 filename
Description: Display the last 20 lines of filename.

How to use the tail command under Linux

The purpose of the linux tail command is to output the last part of the specified file to a standard device as required, usually a terminal. Generally speaking, it is to display the last few lines of a certain file on the terminal, assuming that the file has been updated. , tail will automatically refresh itself to ensure that you see the latest file content.

1.tail command syntax

tail [ -f ] [ -c Number | -n Number | -m Number | -b Number | -k Number ] [ File ]
Parameter explanation:
-f This parameter is used to monitor File file growth.
-c Number Read the specified file from Number byte position
-n Number Reads the specified file from the Number line position.
-m Number reads the specified file from the Number multi-byte character position. For example, if your file contains Chinese characters, if the -c parameter is specified, it may cause truncation, but using -m will avoid this problem.
-b Number Reads the specified file from the 512-byte block position represented by Number.
-k Number Read the specified file from the 1KB block position represented by Number.
File specifies the target file name of the operation
The above commands all involve number. If not specified, 10 lines will be displayed by default. A positive or negative sign can be used in front of the Number to indicate whether the offset is calculated from the top or the tail.
The tail executable files are generally under /usr/bin/.

2. Demonstration example of how to use the tail command

1. tail -f filename
Description: Monitor the tail content of filename file (default 10 lines, equivalent to adding parameter -n 10), refresh and display on the screen. To exit, press CTRL C.
2. tail -n 20 filename
Description: Display the last 20 lines of filename.
3. tail -n 20 filename
Description: Display the first 20 lines of filename.
4. tail -r -n 10 filename
Description: Display the last 10 lines of filename in reverse order.
Replenish:

Commands with similar functions to tail include:
cat displays the file contents starting from the first line.
tac displays the file contents starting from the last line.
The more tab displays the file contents.
less is similar to more, but supports page forward
head only displays the first few lines
tail only displays the next few lines
n Display file contents with line numbers
od displays file contents in binary format

1. View n lines in the header of the text file

head -n 200 filename #--200 can be replaced with any number

2. View n lines at the end of the text file

tail -n 200 filename #--200 can be replaced with any number

3. Check the number of lines in the text file

wc -l filename

linux查看文件的后几行(文件查看 如何显示最后几行 ,某几行)

Linux – Centos file viewing how to display the last few lines and certain lines

1. Display the last 20 lines

tail -n 20 file name

Such as tail -n 20 log.txt

2. Display the middle 20, starting from 1000

cat log.txt|tail -n 1000|head -n 20

Note: tail -n line number means how many lines to start from, if it is tail -n -line == tail -n line means how many lines of data are at the end of the file
head -n 20 The first 20 rows of data

3. You can also use the sed command

Such as sed -n ‘100,120p’ log.txt 100 to 120 lines of data.

The above is the detailed content of How to view the last few lines of a file in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete