When you need to view the header or tail of a text file, the head command and tail command can easily complete this operation. The head command is used to view the beginning of a text file, while the tail command is used to display the last few lines of the text file. The two commands are as follows:
Head example.txt displays the top 10 lines of example.txt;
Head-N 20 example.txt displays the first 20 rows of content in the file example.txt;
Tail example.txt displays the last 10 lines of example.txt;
Tail-N 20 example.txt displays the last 20 lines of content in the file example.txt;
Tail-F example.txt displays the last 10 lines of example.txt file. After the file content is added, the new file content is automatically displayed.
Note:
The last command is very useful, especially when monitoring log files, you can always display new log information on the screen.
You can use the SED command to view the middle part of the file, for example:
Sed-n'5, 10p'/etc/passwd
In this way, you can only view rows 5th to 10th of the file.