The Linux head command: show the first lines of a file

Introduction

In the Linux environment, one of the most useful commands to inspect text files ishead. Its main function is to show the first lines of a file, which allows you to get a quick view without having to load all the content into memory. This tool is especially valuable when working with extensive records, configuration files or any document where you need to verify the initial format or headers.

Basic syntax

The simplest way to use head is simply to write your name followed by the name of the file:

head name _ of _ file

This way, head will display by default the first ten lines of the specified file. If no file is provided, head reads from the standard input, which allows it to be combined with pipes and other commands.

Most commonly used options

  • -n N: indicates the number of lines to be viewed. For example,head -n 20 archivo.txtshow the top 20 lines.
  • -c N: instead of lines, show the first N bytes of the file. This is useful for inspecting binary files or checking the size of a header.
  • -q: removes headers that head normally adds when multiple files are processed. With this option, the output is continuous and uninterrupted.
  • -v: forces the inclusion of the file name as header, even when only one file is processed. It is useful to maintain clarity in scripts.
  • -helpand-version: show the help and the command version, respectively.

Practical examples

  • See the first five lines of a system record:
  • head -5 / var / log / syslog
  • Get the first 150 bytes of an image to check its header:
  • head -c 150 photo.jpg
  • Examine several unheaded configuration files simultaneously:
  • head -q -n 3 / etc / hosts / etc / resolv.conf
  • Combine head with grep to look for a pattern only in the first lines:
  • head -20 archivo.log | grep 'ERROR'
  • Use head in a script to create a sample of a large file:
  • head -n 1000 large _ archivo.csv > samala.csv

Typical cases of use

System administrators use head to quickly review service logs and detect problems without having to load files from several gigabytes. Developers use it to inspect the CSV, JSON or XML file structure before writing custom parsers. In data science environments, head allows for a preview of mass data sets, facilitating decision on which columns or rows are relevant for further analysis. In addition, head is common in processing pipelines where it is necessary to limit the amount of data that move to the next stage, improving performance and reducing resource consumption.

Tips and tricks

  • Remember that head, by default, uses a line buffer; if you need to work with bytes, the -c option is more precise for files where the line concept is not defined (e.g. binary files).
  • When you work with multiple files and want to keep each one's name as a reference, it combines -v with -n to get a clearly labeled output.
  • If you want to skip the first lines and see the rest, you can use head in combination with tail:tail -n +11 archivoshows from line 11 onwards, equivalent to omitting the first ten.
  • In scripts, it is good practice to check the existence of the file before calling head to avoid unnecessary error messages.
  • To obtain a random sample of lines, you can use shuf along with head:shuf archivo | head -n 5.

Limitations and considerations

  • Head does not modify the original file; only reading.
  • In systems with multibyte encoding, use -c can cut in the middle of a character; to avoid it, prefer -n when working with text.
  • If you need lines at the end of the file, tail is the right tool.
  • In environments with different premises, -n behavior may vary if non-standard line leaps are defined.

Conclusion

The head command is a simple but powerful tool that is part of the basic set of Linux utilities. Your ability to quickly display the first lines or bytes of a file makes it an essential ally for administrators, developers and any user who needs to inspect data without overloading the system. Dominating your options and knowing how to combine it with other commands by piping allows you to optimize workflows and speed up decision-making in command line environments.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish