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

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

The ‘head’ command is used to output the first part of files. It is commonly used to display the first few lines or the first few bytes of a file. The ‘head’ command can also be used to exclude the last few lines or bytes of a file.

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

Code:

head --lines count path/to/file

Motivation: This use case is useful when you want to quickly preview the contents of a file without having to open the entire file. It can be particularly helpful when dealing with large files or log files where you only need to see the beginning of the data.

Explanation:

  • ‘head’ is the command to display the first part of a file.
  • ‘–lines’ is an option to specify the number of lines to display.
  • ‘count’ is the number of lines you want to display.
  • ‘path/to/file’ is the path to the file you want to view.

Example output: If we want to display the first 10 lines of the file ’example.txt’, we would use the following command:

head --lines 10 example.txt

Output:

Line 1
Line 2
Line 3
...
Line 10

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

Code:

head --bytes count path/to/file

Motivation: This use case is useful when you want to examine the contents of a file in binary format or when you only need to view a small portion of a large file.

Explanation:

  • ‘head’ is the command to display the first part of a file.
  • ‘–bytes’ is an option to specify the number of bytes to display.
  • ‘count’ is the number of bytes you want to display.
  • ‘path/to/file’ is the path to the file you want to view.

Example output: If we want to display the first 100 bytes of the file ’example.bin’, we would use the following command:

head --bytes 100 example.bin

Output:

0x01 0x02 0x03 0x04 0x05 0x06 0x07...

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

Code:

head --lines -count path/to/file

Motivation: This use case is useful when you want to exclude the last few lines of a file. It can be beneficial for analyzing log files or when you only need the initial part of the file.

Explanation:

  • ‘head’ is the command to display the first part of a file.
  • ‘–lines’ is an option to specify the number of lines to display.
  • ‘-count’ is a negative value indicating the number of lines to exclude from the end of the file.
  • ‘path/to/file’ is the path to the file you want to view.

Example output: If we want to display everything from the file ’example.txt’, excluding the last 5 lines, we would use the following command:

head --lines -5 example.txt

Output:

Line 1
Line 2
Line 3
...
Line n-6
Line n-5

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

Code:

head --bytes -count path/to/file

Motivation: This use case is useful when you want to exclude the last few bytes of a file. It can be beneficial for analyzing binary files or when you only need the initial part of the file.

Explanation:

  • ‘head’ is the command to display the first part of a file.
  • ‘–bytes’ is an option to specify the number of bytes to display.
  • ‘-count’ is a negative value indicating the number of bytes to exclude from the end of the file.
  • ‘path/to/file’ is the path to the file you want to view.

Example output: If we want to display everything from the file ’example.bin’, excluding the last 50 bytes, we would use the following command:

head --bytes -50 example.bin

Output:

0x01 0x02 0x03 0x04 0x05 0x06 0x07...

Conclusion:

The ‘head’ command is a versatile tool for viewing the first part of files. Whether you need to preview the beginning of a file, examine the binary data, or exclude the end portion, the ‘head’ command provides various options to fulfill your needs. By utilizing the different arguments, you can efficiently extract the desired information from files without the need to open them entirely.

Tags :

Related Posts

How to use the command bpftrace (with examples)

How to use the command bpftrace (with examples)

bpftrace is a high-level tracing language for Linux eBPF (Extended Berkeley Packet Filter).

Read More
How to use the command standard-version (with examples)

How to use the command standard-version (with examples)

Standard-version is a command-line tool that aims to automate the versioning and changelog generation process by using SemVer (semantic versioning) and Conventional Commits.

Read More
How to use the command fast (with examples)

How to use the command fast (with examples)

Fast is a command-line tool that allows users to test their internet download and upload speed.

Read More