Get the App
SLTechnology News&Howtos  ›  Servers  › 

The usage of head command in Linux

Shulou Source: shulou.com Published: 2022-06-01 12:28:37 10月27日 Update

This article mainly explains "the usage of head command in Linux". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the usage of the head command in Linux.

How to use the head command

Use head to display the first ten lines of / etc/passwd:

[root@localhost ~] # head / etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0 : shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologin

How to display the specified number of rows

Use the-n command to display the specified number of lines, or you can omit the letter n and use only-and numbers (there is no space between the numbers and -).

For example, use the-n parameter to display the first five lines of / etc/passwd:

[root@localhost] # head-n 5 / etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

View multiple files

If multiple files are provided as input to the head command, it displays the first ten lines of each file. The following example uses the head command to display the first two lines of the / etc/passwd and / etc/shadow files:

[root@localhost ~] # head-n 2 / etc/passwd / etc/shadow== > / etc/passwd / etc/shadow

Tags: Commands files contents multiple numbers ten lines learning practical deeper between five lines examples interests parameters letters practicality practical simple operation file name method Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Xiaomi Microsoft Huawei Shulou Technology