Home  >  Article  >  Operation and Maintenance  >  How to read the first few lines of a file in linux

How to read the first few lines of a file in linux

青灯夜游
青灯夜游Original
2022-04-14 18:27:4614355browse

In Linux, the head command can be used to read the first few lines of the file. This command can be used to view the contents of the first few lines of the specified file. The syntax is "head [-n K] file name"; "K " indicates the number of lines, indicating that the contents of the first K lines of the file are displayed. If "-K" is used, it means that except for the last K lines of the file, all remaining contents are displayed.

How to read the first few lines of a file in linux

#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 the file.

The head command can be used to view the contents of the beginning of the file. There is a commonly used parameter -n for displaying the number of lines. The default is 10, which means 10 lines of content are displayed.

The basic syntax format for reading the first few lines of the file is as follows:

head [-n K] 文件名

Instructions:

  • K represents the number of lines. This option is used to display the contents of the first K lines of the file;

  • If you use "-K" as a parameter , means that except for the last K lines of the file, all remaining contents will be displayed.

  • If K is omitted, 10 lines of content will be displayed by default.

Head command other two common options and their meanings
Options Meaning
-c K K here represents the number of bytes. This option is used to display the contents of the first K bytes of the file; if "-K" is used, it means that except the last K of the file Bytes of content, display all remaining content.
-v Display the file name;

Read the first few lines of the file Example:

Specify the number of lines

[root@xuexi ~]# head -n 2 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

Specify that all lines will be displayed except N lines at the end.

[root@xuexi ~]# head -n -40 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

Always display the title file name

[root@xuexi ~]# head -n 2 -v /etc/passwd
==> /etc/passwd <==
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

Specify multiple files

[root@xuexi ~]# head -n 5 /etc/passwd /etc/firewalld/firewalld.conf 
==> /etc/passwd <==
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
        //两个文件之间会空一行
==> /etc/firewalld/firewalld.conf <==
# firewalld config file
 
# default zone
# The default zone used if an empty zone string is used.
# Default: public

Do not display header file names

[root@xuexi ~]# head -n 5 -q /etc/passwd /etc/firewalld/firewalld.conf
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin        //此时中间就没有空行
# firewalld config file
 
# default zone
# The default zone used if an empty zone string is used.
# Default: public

Note: head is also often used for pipe redirection

The above is the detailed content of How to read the first 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