Hex Dump (hd) Command - View File Contents in Hexadecimal

View file contents in hexadecimal format with the hd command. Analyze binary data, debug files, and understand data structures. Free online tool.

Hex Dump (hd) Command

The hd command is a powerful utility for displaying the contents of files in hexadecimal format. It's invaluable for developers and system administrators who need to inspect binary data, debug file corruption, or understand the raw structure of files. Unlike simpler hex viewers, hd offers fine-grained control over the output, allowing you to specify the exact bytes to display and their offset.

Understanding Hexadecimal Output

Hexadecimal (base-16) is a common way to represent binary data because it's more human-readable than raw binary. Each hexadecimal digit represents 4 bits, so two hexadecimal digits can represent a full byte (8 bits). The hd command typically displays data in columns, showing the offset from the beginning of the file, followed by the hexadecimal representation of the bytes, and often an ASCII representation of those bytes.

Common Use Cases for hd

  • Debugging Binary Files: Inspecting the raw bytes of executables, archives, or custom data formats to identify errors or anomalies.
  • Analyzing Network Packets: Examining the hexadecimal dump of network traffic to understand protocol structures.
  • File Integrity Checks: Comparing hex dumps of files to ensure they haven't been altered.
  • Learning Data Structures: Visualizing how data is laid out in memory or files.

Example Usage of hd Command

Here's a practical example of how to use the hd command to view a specific portion of a file:

# show hex dump of 20 bytes of /etc/passwd (skip 10 first bytes)
hd -s 10 -n 20 /etc/passwd

In this command:

  • -s 10: This option specifies the starting offset. It tells hd to skip the first 10 bytes of the file.
  • -n 20: This option sets the number of bytes to display. Here, it's set to 20 bytes.
  • /etc/passwd: This is the target file whose contents will be displayed in hexadecimal.

Advanced hd Options and Tips

The hd command supports various options to customize the output. You can control the byte size (e.g., display as 16-bit or 32-bit words), specify different output formats, and more. For detailed information on all available options, consult the manual page by running man hd in your terminal.

Alternatives to hd

While hd is a standard Unix utility, other tools like hexdump and xxd offer similar functionality and sometimes more features. The choice often depends on personal preference and the specific requirements of the task.