How linux reads the first few lines of a file
Today, I would like to share with you the relevant knowledge of how linux reads the first few lines of the file. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.
In linux, you can use the head command to read the first few lines of the file, which can be used to view the contents of the first few lines of the specified file, with the syntax of "head [- n K] file name"; "K" represents the number of lines, indicating that the contents of the first K line of the file are displayed; if you use "- K", it shows all the remaining contents except the last K line of the file.
The operating environment of this tutorial: CentOS 6 system, Dell G3 computer.
In linux, you can use the head command to read the first few lines of a file.
The head command can be used to view the contents of the beginning of a file. A common parameter,-n, is used to display the number of lines. The default is 10, that is, 10 lines.
The basic syntax format for reading the first few lines of the file is as follows:
Head [- n K] file name
Description:
K represents the number of lines. This option is used to display the contents of the first K line of the file.
If you use "- K" as a parameter, it means that all the remaining contents are displayed except for the last K line of the file.
If K is omitted, 10 lines are displayed by default.
Head command other two commonly used options and meaning option meaning-c K where K represents the number of bytes, this option is used to display the contents of the first K bytes of the file; if you use "- K", it shows all the rest except the last K bytes of the file. -v displays the file name
An example of reading the first few lines of a file:
Specify the number of rows
[root@xuexi ~] # head-n 2 / etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologin
Specifies that all except N lines are displayed at the end.
[root@xuexi] # head-n-40 / etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologin
Always show the title file name
[root@xuexi] # head-n 2-v / etc/passwd== > / etc/passwd / etc/passwd / etc/firewalld/firewalld.conf