How to Display the First 10 Lines of a File in Linux Using the head Command

If you are managing a Linux server and need to quickly inspect a massive system log or verify the formatting of a 5-gigabyte CSV database, attempting to open the file in a text editor like Nano or Vim is a terrible idea. Loading that much text into the active memory will cause the terminal to freeze entirely.

Instead, Linux administrators rely on the incredibly lightweight head command. This utility instantly slices off the very beginning of a file and prints it to your terminal screen, allowing you to instantly preview the data structure without actually opening the file itself.

How to Use the head Command

By default, if you use the command without any special modifications, it is hardcoded to print exactly the first 10 lines of the target file.

  1. Open your Linux terminal.
  2. Type head, followed by a space, and then the exact path to the file you want to inspect.
    head /var/log/syslog
  3. Press Enter.

The terminal will instantly spit out the top 10 lines of the file and immediately return you to the command prompt. Because it never attempted to read the remaining 99% of the gigabyte-sized file, the command executes in a fraction of a millisecond.

How to Change the Number of Lines Displayed

Ten lines is often not enough to fully understand the context of a log file, or it might be too many if you just want to read the top header row of a spreadsheet.

You can override the default behavior by passing the -n (number) flag, followed by the specific number of lines you want the command to extract.

  • To read the first 50 lines:
    head -n 50 /var/log/syslog
  • To read only the very first line (useful for extracting just the header row from a CSV):
    head -n 1 database.csv

Note: If you need to read the end of the file rather than the beginning (which is extremely common when looking for the most recent error in a log file), you simply swap the command for tail, which functions identically but pulls from the absolute bottom of the document.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.