Home  >  Article  >  Operation and Maintenance  >  How to quickly display the last few lines of a file in Linux

How to quickly display the last few lines of a file in Linux

WBOY
WBOYOriginal
2024-02-23 18:21:061030browse

How to quickly display the last few lines of a file in Linux

How to quickly view the last few lines of a file under Linux

In Linux systems, you often encounter situations where you need to view the last few lines of a file, such as viewing logs The latest lines of the file. In this case, we can use some commands to quickly see what is at the end of the file. Some common methods will be introduced below and specific code examples will be provided.

  1. Using the tail command

The tail command is a very common command used to view the content at the end of the file. The number of lines to be displayed can be specified through the tail command. The -t option is used to specify how many lines to display. If the -t option is not added, the last 10 lines will be displayed by default.

tail -n 5 filename

The above command will display the last 5 lines of the file "filename".

  1. Use the cat command to combine pipes and tail commands

You can use the cat command to output the file contents to the standard output, and then pipe the output to the tail command for display Specify the number of lines of content.

cat filename | tail -n 5

The effect of this command is the same as the tail command above, which displays the last 5 lines of the file "filename".

  1. Use the sed command

In addition to the tail and cat commands, you can also use the sed command to view the last few lines of the file. The sed command can process the file line by line. Using the '$!N' method, the file content can be read line by line, and then the specified number of lines can be printed out.

sed -n '$-5,$p' filename

This command will display the contents of the fifth to last line of the file "filename", which is equivalent to displaying the contents of the last five lines.

The above is how to quickly view the last few lines of a file under Linux system, and provides specific code examples. Choosing an appropriate method to view the content at the end of the file according to the actual situation can improve work efficiency.

The above is the detailed content of How to quickly display the last few 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