Home > Article > Operation and Maintenance > 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.
The tail
command is a very convenient tool that can display the content at the end of the file by adding -n
The 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.
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
.
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!