How to Use the Command 'head' (with examples)

How to Use the Command 'head' (with examples)

  • Osx
  • December 17, 2024

The head command is a versatile tool found in Unix and Unix-like operating systems, primarily used to display the beginning sections of files. This makes it particularly useful for quickly viewing the structure of a document or for extracting a snapshot of its initial content without needing to open an entire file, which can be especially useful for large files. Its straightforward syntax enables users to view a specified number of lines or bytes from the start of a file, offering a convenient way to handle text files or streams efficiently.

Use case 1: Output the first few lines of a file

Code:

head --lines 8 path/to/file

Motivation:

When editing or analyzing a file, especially one that’s large, you may not always need to see the entire contents right away. Perhaps you only need to verify that the file starts as expected with a certain format, header information, or specific data points. By using the head command, you can easily see the first few lines of any file without needing to load or scroll through the entire content.

Explanation:

  • head: This is the core command used to output the start of files.
  • --lines 8: This option specifies that the first 8 lines of the file should be printed to the terminal. The --lines argument allows you to define how many lines you want to retrieve from the beginning of the file.
  • path/to/file: This refers to the location of the file you want to view the initial lines from. You replace this with the actual path to your file.

Example Output:

Suppose your file “example.txt” begins with:

Line 1: Introduction to the document
Line 2: Summary of the sections
Line 3: Details about the author
Line 4: Date of creation
Line 5: Version information
Line 6: Table of contents
Line 7: Preface
Line 8: Introduction
Line 9: Main content starts here

Using the command would output:

Line 1: Introduction to the document
Line 2: Summary of the sections
Line 3: Details about the author
Line 4: Date of creation
Line 5: Version information
Line 6: Table of contents
Line 7: Preface
Line 8: Introduction

Use case 2: Output the first few bytes of a file

Code:

head --bytes 8 path/to/file

Motivation:

In certain situations, especially when dealing with binary files or files with non-textual data, it might be crucial to inspect the first few bytes rather than lines. This can be valuable for verifying file signatures, checking encoding schemes, or investigating formatting bytes at the start of files. Using bytes lets you analyze data at a more granular level than lines.

Explanation:

  • head: The command used to output the beginning part of files.
  • --bytes 8: This option specifies that the first 8 bytes from the file should be shown. The --bytes argument is handy when you want a more byte-oriented view of the file content.
  • path/to/file: The path indicates the location of the file from which you want to read the first few bytes.

Example Output:

For a text file that starts with “HelloWorld”, using this command might yield:

HelloWor

(As “HelloWor” comprises the first 8 bytes of the file).

Use case 3: Output everything but the last few lines of a file

Code:

head --lines -8 path/to/file

Motivation:

There might be cases where you want to exclude the last few lines of a file from viewing. This could be useful if a file has summary lines, footers, or signature lines that you want removed from a data processing pipeline or report. Instead of counting lines to omit or scrolling endlessly, the head command allows you to tailor your view precisely by excluding these lines.

Explanation:

  • head: The command used to systematically retrieve the beginning sections of files.
  • --lines -8: By using a negative number, head will output everything except the last 8 lines of a file. This option is useful when you want to exclude trailing content that isn’t needed.
  • path/to/file: This is the path to the file from which you’ll be excluding the last few lines.

Example Output:

A file “log.txt” that ends with:

...
Line 90: End section start
Line 91: Summary part 1
Line 92: Summary part 2
Line 93: Summary part 3
Line 94: Conclusion
Line 95: References
Line 96: Index
Line 97: Footer information

Would produce:

...
Line 89: Some middle document content

Use case 4: Output everything but the last few bytes of a file

Code:

head --bytes -8 path/to/file

Motivation:

There are scenarios where you might need to strip off trailing bytes from a file, such as when files end with unwanted padding bytes, footers, or file delimiters that interfere with further processing. By dictating a negative byte count, you can quickly retrieve all but the trailing bytes of content, making data ready for immediate use.

Explanation:

  • head: The command that serves in presenting the initial portions of files.
  • --bytes -8: A negative byte count instructs head to output all except the last 8 bytes of the file. This option is advantageous when you need to eliminate concluding bytes for tasks requiring precision.
  • path/to/file: This identifies the file path you’re working with to view all but the last few bytes.

Example Output:

Consider a data file ending with “DataEnd”:

...
DataStrHeader12345DataEnd

Would result in:

...
DataStrHeader12345

Conclusion:

The head command provides an efficient way of examining the initial segments of files, whether you’re interested in lines or byte content. Its flexibility in specifying the number of lines or bytes, as well as the option to exclude trailing content, makes it a powerful tool for developers, system administrators, and data analysts dealing with diverse files. Whether inspecting starting points, verifying file headers, or preparing data for processing, head remains a staple command for quick and precise file content examination.

Tags :

Related Posts

How to Effectively Use the Command 'dnf group' (with examples)

How to Effectively Use the Command 'dnf group' (with examples)

The dnf group command is a powerful tool for managing virtual collections of packages, known as package groups, on Fedora-based systems.

Read More
How to Use the Command 'zfs' (with Examples)

How to Use the Command 'zfs' (with Examples)

ZFS, or the Zettabyte File System, is an advanced file system designed for high storage capacities, integrating file system and volume management capabilities.

Read More
How to use the command 'unix2dos' (with examples)

How to use the command 'unix2dos' (with examples)

The unix2dos command is a useful utility typically found in Unix and Linux environments which is used to convert text files with Unix line endings (LF) to DOS-style line endings (CRLF).

Read More