Home  >  Article  >  Operation and Maintenance  >  How to check the number of last lines of a file in Linux

How to check the number of last lines of a file in Linux

WBOY
WBOYOriginal
2024-02-23 23:54:061522browse

How to check the number of last lines of a file in Linux

In Linux systems, there are many ways to view the last few lines of a file. The following will introduce several common methods and give specific code examples.

Method 1: Use the tail command

The tail command is a very convenient tool that can display the content at the end of the file by adding -nThe parameter can specify how many lines of content to display. The following is a sample code:

tail -n 10 filename.txt

The above code will display the last 10 lines of the file filename.txt. Numbers can be adjusted to suit specific needs.

Method 2: Use cat combined with pipeline and tail command

Another method is to use cat combined with pipeline and tail command to view files The function of the last few lines, the example is as follows:

cat filename.txt | tail -n 10

The above code will also display the last 10 lines of the file filename.txt.

Method 3: Use the sed command

The sed command is also a powerful text processing tool in Linux. It can be combined with regular expressions to view the last few lines of the file. Function. The following is a sample code:

sed -n '$p' filename.txt
sed -n '1,10p' filename.txt

The first line of code will display the last line of the file filename.txt, and the second line of code will display the content of the file filename.txt Contents from the 1st to 10th line from the last. Numbers can be adjusted as needed.

In general, viewing the last few lines of a file in a Linux system is a very common operation, and there are many ways to do it. The methods introduced above are relatively common and simple methods. Readers can choose the appropriate method according to their own habits and actual needs.

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn