You are currently viewing Linux Head Command With Examples

Linux Head Command With Examples

Linux Commands – head

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed less command in Linux which is used for the pagination of a large file.

https://cloudaffaire.com/linux-commands-less/

In this blog post, we will discuss head command in Linux. head command is used to print some lines from the top of the file and output to standard output. By default, head command prints the first 10 lines of each FILE to standard output. With more than one FILE, precede each with a header giving the file name. You can also use head command with pipe.

Linux Commands – head:

You can use head command to print some lines form the top of a file and output to standard output.

You can use head -n K or –lines=K options to output the first K lines of a file or pipe. However, if K starts with a ‘-‘, head command prints all but the last K lines of each file.

You can use head -c K or –bytes=K options to print the first K bytes, instead of initial lines. However, if K starts with a ‘-‘, head prints all but the last K bytes of each file. K maybe an integer optionally followed by, one of the following multiplicative suffixes:

  • ‘b’ => 512 (“blocks”)
  • ‘KB’ => 1000 (KiloBytes)
  • ‘K’ => 1024 (KibiBytes)
  • ‘MB’ => 1000*1000 (MegaBytes)
  • ‘M’ => 1024*1024 (MebiBytes)
  • ‘GB’ => 1000*1000*1000 (GigaBytes)
  • ‘G’ => 1024*1024*1024 (GibiBytes)

You can use head -v or –verbose options to always print file name headers. By default, head prints the file name header only if multiple files were provided as input.

You can use head -q or –quiet or –silent options to never print file name headers. This is the default behavior of head command if a single file is provided as input.

Hope you have enjoyed this article. In the next blog post, we will discuss tail command in Linux.

Leave a Reply