Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.


CompTIA Linux+ XK0-005 - 3.1 - Common Script Utilities: head

The head command is a useful utility in Linux that allows you to display the beginning or the first few lines of a file or standard input. It is particularly handy when dealing with large files or when you only need to preview the initial contents of a file. In this guide, we will explore the various aspects of the head command, including its purpose, common options, real-world examples, and a conclusion summarizing its utility.

Explanation

The head command is designed to display the first 10 lines of a file by default. However, you can specify a different number of lines using the -n option. It reads input from a file or standard input and outputs the specified number of lines to the standard output. The head command is commonly used to quickly preview the contents of a file, check the header of a file, or extract the initial portion of a file for further processing.

Common Command Options

  • -n NUM, --lines=NUM: Specifies the number of lines to display from the beginning of the file.
  • -c NUM, --bytes=NUM: Specifies the number of bytes to display from the beginning of the file.
  • -q, --quiet, --silent: Suppresses the printing of file names when multiple files are provided.
  • -v, --verbose: Prints the file name before displaying the output.

Real World Examples

Displaying the first 10 lines of a file

head file.txt

This command displays the first 10 lines of the file.txt file.

Displaying a specific number of lines

head -n 5 file.txt

This command displays the first 5 lines of the file.txt file.

Previewing multiple files with headers

head -v file1.txt file2.txt

This command displays the first 10 lines of both file1.txt and file2.txt, preceded by their respective file names.

Extracting a portion of a file based on bytes

head -c 100 file.txt

This command displays the first 100 bytes of the file.txt file.

Conclusion

The head command is a handy tool for quickly previewing the contents of files or extracting the initial portion of a file. By default, it displays the first 10 lines of a file, but you can specify a different number of lines using the -n option. The head command proves to be an essential utility for inspecting files, checking file headers, and extracting data for further analysis. By mastering the head command, you can efficiently handle file previews and streamline your file exploration workflows in the Linux environment.


Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.