Overview
hexdump reads the contents of a file in 8-bit byte units and converts them to hexadecimal format for output. This command is particularly used for visually analyzing the contents of binary files such as executables, images, and audio files. Even content that appears corrupted when opened with a regular text editor can be viewed with hexdump, showing the offset (position), hexadecimal values, and, if possible, ASCII strings, which greatly helps in understanding the file's structure.
Key Features
The key features of the hexdump command are as follows:
- Outputs file contents in various formats, including hexadecimal, octal, and decimal.
- Suitable for analyzing the contents of binary files.
- Allows specifying output format to view results in desired forms.
- Can be used with pipes (`|`) to analyze the output of other commands.
Output Format
hexdump's output typically consists of three parts:
- Offset: Indicates the number of bytes from the beginning of the file to the current position. By default, it is displayed in hexadecimal format.
- Byte Data: Shows the actual byte values of the file content in hexadecimal format.
- ASCII String: Converts byte data into human-readable ASCII characters. Non-printable characters are represented by a dot (.).
Key Options
Commonly used hexdump command options are grouped by purpose.
1) Output Options
2) Help
Generated command:
Try combining the commands.
Description:
`hexdump` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Learn the functions of the hexdump command through various usage examples.
Output File Content in Canonical Format
hexdump -C /bin/cat | head
The most commonly used format, outputs the beginning of the `/bin/cat` file in canonical format.
Output from a Specific Position in the File
hexdump -C -s 0x20 -n 32 hexdump.test
Outputs 32 bytes of content from hexadecimal offset `0x20` of the `hexdump.test` file.
Check Special Characters
echo 'Hello\nWorld' | hexdump -C
Checks the hexadecimal content of a text file containing newline (`\n`) characters. `0a` is the hexadecimal value for a newline character.
Use with Pipe (|)
strings /bin/cat | hexdump -C
Pipes the output of the `strings` command to `hexdump` to analyze the hexadecimal values of extracted strings.
Tips & Cautions
Here are some points to keep in mind when using the hexdump command.
Tips
- `hexdump` is a very powerful tool when dealing with binary files. You can infer the file type by checking the file header or specific byte values.
- Outputting an entire binary file can cause the terminal to hang, so it's recommended to use the `-n` option or the `head` command together.
- The `od` (`octal dump`) command is similar to `hexdump` and outputs in octal format by default.