Skip to content
Home » Learn the Basics of the Head Command

Learn the Basics of the Head Command

  • Linux
  • 3 min read

What is the Head Command?

The head command is a utility that prints the first N lines of a file or standard input. It can be used to quickly view the beginning or “head” of a file. By default, head prints the first 10 lines of a file, but that number can be changed with the -n flag, which specifies the number of lines to be printed.

How to Use the Head Command

The head command is used in the command-line terminal, and the syntax is as follows: head [OPTION]... [FILE].... The head command can be used with a variety of flags to modify its behavior. The most commonly used flags are -n, which specifies the number of lines to be printed, and -q, which suppresses the output of the filename before each set of data.

Five Tips for Using the Head Command

  1. Use the -n flag to specify the number of lines you want to print.
  2. Use the -q flag to suppress the output of the filename before each set of data.
  3. Combine head with other commands to quickly extract useful information. For example, you can use the combination head -n 1 filename.txt to quickly view the first line of a file.
  4. Pipe the output of head to another command to further filter and manipulate the output.
  5. Use the -v flag to print the filename before each set of data, even when the file is standard input.

Examples

# Print the first 20 lines of the file 'itvraag.nl':
head -n 20 itvraag.nl

# View the first line of the file 'itvraag.nl' without printing the filename:
head -n 1 -q itvraag.nl

# Print the first 20 lines of the file 'itvraag.nl', but print the filename before each set of data:
head -n 20 -v itvraag.nl

# Extract the first 3 lines of the file 'itvraag.nl', piping the output to another command:
head -n 3 itvraag.nl | <another_command>

Head vs Tail

The head command is similar to the tail command, but instead of printing the last N lines of a file, it prints the first N lines. Both commands can be used with the -n flag to specify the number of lines to be printed, and both can be used to quickly view the beginning or “head” of a file. The tail command can also be used with the -f flag to continuously follow a file, while the head command does not have this functionality.

Comparing Head to Other Viewers

The head command is most similar to other viewers such as cat, less, and more. All of these viewers can be used to view the contents of a file, and they all have different strengths and weaknesses. Cat is the simplest of the viewers and cannot be used to view only a portion of a file, while less and more are more powerful and can be used to view only a section of a file.

Challenge

Now that you know how to use the head command, why not test your knowledge by trying out this challenge? Write a command to print the first 15 lines of the file itvraag.nl, but suppress the output of the filename before each set of data.

Hint: You’ll need to use the -n and -q flags.

Leave a Reply

Your email address will not be published. Required fields are marked *

twenty − fourteen =