hexdump Command in Linux

Topics Covered

Overview

Hexdump command in Linux is a highly useful utility that converts and displays binary data in hexadecimal format. This can be instrumental when analyzing files at a bit-level, especially when dealing with data encoding, debugging, or network packet analysis.

Syntax of hexdump Command in Linux

The basic syntax of the hexdump command in Linux is as follows:

Where:

  • options: These are optional and alter the output format or behavior of the hexdump command.
  • file: The file for which you want to generate the hexdump.

Options in hexdump Command in Linux

  1. -C: This option displays the output in 'canonical' hex+ASCII display.

    For example -

    Output:

    This command provides the hexadecimal representation of each byte in myfile.txt, along with the corresponding ASCII values.

  2. -v: The -v option causes hexdump to display all input data.

    For example -

    Output:

    Unlike the default behavior, the -v option prevents the use of '*' notation to indicate repeated lines.

  3. -n length: The -n length option tells hexdump to display 'length' bytes of input.

    For example -

    Output:

    This command will only display the first 10 bytes of myfile.txt.

Example Usages

  • Basic usage of hexdump:

    Output:

    Explanation: This will output the hexadecimal representation of myfile.txt, grouping input into short words.

  • Using hexdump with -b option for one-byte octal display:

    Output:

    Explanation: This command outputs the octal values of each byte in myfile.txt.

Tips

  • To read hexdump output more easily, remember that it is grouped into 16-byte sequences by default.

  • The hexdump command in Linux works with pipes and redirections, making it versatile in scripting and data analysis.

Advanced Use Cases of hexdump Command in Linux

  • Using hexdump with -e option for custom formats:

    Output:

    Explanation: This command provides a custom formatted hexadecimal dump of myfile.txt with offset in file, sixteen space-separated, two-column, zero-filled, hexadecimal bytes, and a newline.

  • Creating a hexadecimal color code using hexdump:

    Output:

    Explanation: This command generates a hex color code from a string by reading the first three characters ('H', 'e', 'l'), translating each to its hexadecimal equivalent and then taking the first 6 hex digits.

  • Reading binary file data with hexdump:

    Output:

    Explanation: This command outputs a 'canonical' hexadecimal plus ASCII dump of a binary file. It is especially useful when dealing with unknown or corrupt file formats.

Conclusion

  • The hexdump command in Linux is a powerful tool for examining binary files or producing human-readable data from binary input.

  • Hexdump provides a range of options to alter the output format, including '-C' for canonical hex+ASCII display, and '-n length' to control the length of output.

  • With advanced usage, hexdump can be customized to display data in different formats and used for tasks like generating hex color codes.