Linux head command

In Linux, the “head” command is used to display the beginning lines of a file or the output of another command. It allows you to see a specified number of lines from the start of a file. Here’s how to use the “head” command:

  1. Open a Terminal:
    Launch a terminal emulator on your Linux system.
  2. Type the “head” command:
    The basic syntax of the “head” command is as follows:
   head [options] [file(s)]
  • The “[file(s)]” parameter represents the name(s) of the file(s) you want to display. You can specify multiple file names separated by spaces.
  • If you don’t provide any file name, “head” will read input from the standard input (e.g., the output of a command or piped input). For example, to display the first 10 lines of a file called “file.txt”, you can use the following command:
   head file.txt

By default, the “head” command displays the first 10 lines of the file.

  1. Specifying the Number of Lines:
    You can specify the number of lines to display using the “-n” option followed by the desired number. For example, to display the first 5 lines of a file, you can use the following command:
   head -n 5 file.txt

The “head” command will display the first 5 lines of “file.txt”.

  1. Using Options:
    The “head” command provides various options to modify its behavior. Some commonly used options include:
  • “-n” (number of lines): Specify the number of lines to display.
  • “-c” (bytes): Display the specified number of bytes instead of lines.
  • “-q” (quiet): Suppress the printing of headers when multiple files are specified.
  • “-v” (verbose): Always print headers when multiple files are specified. For example, to display the first 3 lines of a file without printing the file name, you can use the following command:
   head -n 3 -q file.txt
  1. Combining “head” with Other Commands:
    You can use the “head” command in conjunction with other commands by piping the output of one command as input to “head”. For example, to display the first 5 lines of the output of a command, you can use the following command:
   command | head -n 5

Replace “command” with the actual command whose output you want to display.

  1. Exiting the Command:
    Once the desired lines are displayed using the “head” command, you can continue executing other commands or exit the terminal as needed.

The “head” command is useful for quickly previewing the beginning lines of a file or the output of other commands. Use the available options to customize the behavior of “head” according to your requirements.