How to use the command 'hexdump' (with examples)
Hexdump is a command-line tool that allows users to display the hexadecimal representation of a file. It can also display the ASCII representation and interpret only a specific number of bytes from the input. Additionally, hexdump can replace duplicate lines with an asterisk.
Use case 1: Print the hexadecimal representation of a file, replacing duplicate lines by ‘*’
Code:
hexdump path/to/file
Motivation: This use case is useful when you want to view the hexadecimal representation of a file while highlighting duplicate lines. By replacing duplicate lines with an asterisk, it becomes easier to identify repeated content within the file.
Explanation:
hexdump
: The command to run the hexdump tool.path/to/file
: The path to the file for which you want to display the hexadecimal representation.
Example output:
00000000 48 65 6c 6c 6f 20 57 6f 72 6c 64 0a |Hello World.|
Use case 2: Display the input offset in hexadecimal and its ASCII representation in two columns
Code:
hexdump -C path/to/file
Motivation: This use case is helpful when you want to display the input offset in hexadecimal alongside its corresponding ASCII representation. It provides a clearer and more organized view of the file contents.
Explanation:
-C
: The flag to enable the display of the input offset and ASCII representation in two columns.path/to/file
: The path to the file for which you want to display the hexadecimal and ASCII representation.
Example output:
00000000 48 65 6c 6c 6f 20 57 6f 72 6c 64 20 28 77 69 74 |Hello World (wit|
00000010 68 20 65 78 61 6d 70 6c 65 73 29 0a |h examples).|
Use case 3: Display the hexadecimal representation of a file, but interpret only n bytes of the input
Code:
hexdump -C -n<number_of_bytes> path/to/file
Motivation: This use case is beneficial when you want to examine the hexadecimal representation of only a specific number of bytes within a file. It helps to focus on a particular portion of the file’s content.
Explanation:
-C
: The flag to enable the display of the input offset and ASCII representation in two columns.-n
: The option to set the number of bytes to be interpreted.<number_of_bytes>
: The desired number of bytes to be interpreted.path/to/file
: The path to the file for which you want to display the hexadecimal representation.
Example output:
00000000 48 65 6c 6c 6f |Hello|
Use case 4: Don’t replace duplicate lines with ‘*’
Code:
hexdump --no-squeezing path/to/file
Motivation: By default, hexdump replaces duplicate lines with an asterisk (’*’). However, in some cases, you may prefer to keep the duplicate lines as they are. This use case allows you to disable the replacement of duplicate lines.
Explanation:
--no-squeezing
: The option to disable the squeezing of duplicate lines.path/to/file
: The path to the file for which you want to display the hexadecimal representation.
Example output:
00000000 48 65 6c 6c 6f |Hello|
00000005 20 57 6f 72 6c 64 0a | World.|
Conclusion:
The ‘hexdump’ command is a versatile tool for displaying the hexadecimal representation of files. Whether you want to replace duplicate lines, display the ASCII representation, or interpret a specific number of bytes, hexdump provides various options to meet your requirements. It is an essential utility for examining file contents and identifying patterns within them.