LinuxCommandLibrary

hexdump

Display file contents in hexadecimal format

TLDR

Print the hexadecimal representation of a file, replacing duplicate lines by *

$ hexdump [path/to/file]
copy

Display the input offset in hexadecimal and its ASCII representation in two columns
$ hexdump -C [path/to/file]
copy

Display the hexadecimal representation of a file, but interpret only a specific number of bytes of the input
$ hexdump -C -n [number_of_bytes] [path/to/file]
copy

Verbose - no suppression by * on duplicate lines
$ hexdump -v [path/to/file]
copy

Format output using printf-like format string
$ hexdump -e '[element_format .. end_format]' [path/to/file]
copy

SYNOPSIS

hexdump [options] [file...]

Common Usage Examples:
hexdump -C [file...]
hexdump -n length -s offset [file...]

PARAMETERS

-A type
    Specifies the output address format. type can be `d` (decimal), `o` (octal), `n` (none), or `x` (hexadecimal).

-b
    One-byte octal display. Displays input offset followed by 16 space-separated, three-digit octal bytes per line.

-c
    One-byte character display. Displays input offset followed by 16 space-separated, three-character representations of bytes per line.

-C
    Canonical hex+ASCII display. This is the most common and readable format, showing the input offset, 16 bytes in hexadecimal, and their ASCII equivalent in one line.

-d
    Two-byte decimal display. Displays input offset followed by 8 space-separated, five-digit unsigned decimal shorts per line.

-e format_string
    Specify one or more format strings to be used for displaying data. This offers powerful custom formatting using a syntax similar to printf.

-f format_file
    Specify a file containing one or more format strings. Useful for complex, reusable formatting definitions.

-L
    Little-endian byte order. Forces the output for `-d`, `-o`, and `-x` to be little-endian. By default, byte order is system-dependent.

-n length
    Interpret only length bytes of input. Useful for examining specific data blocks or file headers.

-o
    Two-byte octal display. Displays input offset followed by 8 space-separated, six-digit octal shorts per line.

-s offset
    Skip offset bytes from the beginning of the input before displaying. offset can be specified with suffixes like `k` (Kilo), `m` (Mega), `g` (Giga).

-v
    Verbose output. Displays all input data. By default, hexdump replaces successive lines of identical output with a single `*` character to save space.

-x
    Two-byte hexadecimal display. Displays input offset followed by 8 space-separated, four-digit hexadecimal shorts per line.

DESCRIPTION

hexdump is a command-line utility used to display the contents of files or standard input in a user-specified format, typically hexadecimal, octal, decimal, or ASCII.
It's an indispensable tool for debugging and reverse-engineering binary files, inspecting network packets, or analyzing memory dumps. Unlike simple text editors, hexdump allows users to see every byte of a file, including non-printable characters, which is crucial for understanding low-level data structures and identifying inconsistencies. Its flexibility in output formatting, from simple byte-by-byte views to canonical hex+ASCII, makes it versatile for a wide range of analytical tasks.

CAVEATS

By default, hexdump will suppress lines of identical output with a single `*` character. Use the -v option to see all data.
The default output format without any flags can be less intuitive, often defaulting to two-byte octal. For human-readable debugging, -C (canonical display) is almost always preferred.
Be mindful of byte order (big-endian vs. little-endian) when using -d, -o, or -x without -L, as it's system-dependent.

DEFAULT OUTPUT BEHAVIOR

Without any specific format options, hexdump defaults to displaying data as two-byte octal values. This can be unexpected for new users who often anticipate a hexadecimal or ASCII representation. Always specify a format option like -C for clarity.

UNDERSTANDING BYTE ORDER

When displaying multi-byte values (e.g., with -d, -o, -x), the order of bytes can differ between systems (big-endian vs. little-endian). Intel x86 architectures are little-endian, meaning the least significant byte is stored at the lowest memory address. The -L option can force little-endian output regardless of the system's native byte order, which is crucial for cross-platform consistency.

POWERFUL CUSTOM FORMATTING

The -e and -f options allow for highly customized output using a format string syntax similar to `printf`. This enables users to define specific byte arrangements, data types, and ASCII/hex/decimal representations, making hexdump an incredibly powerful tool for detailed binary analysis.

HISTORY

hexdump is part of the standard Unix utilities, commonly found in the `util-linux` package on Linux distributions. Its functionality is similar to the older `od` (Octal Dump) command, but hexdump typically provides more flexible and user-friendly formatting options, especially with its powerful custom format string capabilities.

SEE ALSO

od(1), xxd(1), strings(1), cat(1)

Copied to clipboard