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

WBOY
Release: 2024-02-23 23:54:06
Original
1473 people have browsed it

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
Copy after login

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
Copy after login

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!