hexdump command in Linux with Examples



Name

hexdump, hd - ASCII, decimal, hexadecimal, octal dump

Synopsis

hexdump [-bcCdovx] [-e format_string] [-f format_file] [-n length] [-s skip] file ...
hd [-bcdovx] [-e format_string] [-f format_file] [-n length] [-s skip] file ...

Description

The hexdump utility is a filter which displays the specified files in a user specified format. In case no files are specified, it reads input from standard input.

Options

The options for hexdump or the hd commands are:

-b   One-byte octal display. Displays the input offset (address) of data in hexadecimal format. It is followed by sixteen space-separated, three column, zero-filled, bytes of input data, in octal, per line.

-c   One-byte character display. Display the input offset in hexadecimal, followed by sixteen space-separated, three column, space-filled, characters of input data per line.

-C   Canonical hex+ASCII display. Display the input offset in hexadecimal, followed by sixteen space-separated, two column, hexadecimal bytes, followed by the same sixteen bytes in %_p format enclosed in ``|'' characters.

Calling the command hd implies this option.

-d   Two-byte decimal display. Display the input offset in hexadecimal, followed by eight space-separated, five column, zero-filled, two-byte units of input data, in unsigned decimal, per line.

-e format_string
   Specify a format string to be used for displaying data.

-f format_file
   Specify a file that contains one or more newline separated format strings. Empty lines and lines whose first non-blank character is a hash mark (#) are ignored.

-n length
   Interpret only length bytes of input.

-o   Two-byte octal display. Display the input offset in hexadecimal, followed  by eight space-separated, six column, zero-filled, two byte quantities of input data, in octal, per line.

-s offset
   Skip offset bytes from the beginning of the input. By default, offset is interpreted as a decimal number. With a leading 0x or 0X, offset is interpreted as a hexadecimal number, otherwise, with a leading 0, offset is interpreted as an octal number. Appending the character b, k, or m to offset causes it to be interpreted as a multiple of 512, 1024, or 1048576, respectively.

-v      
   Cause hexdump to display all input data. Without the -v option, any number of groups of output lines, which would be identical to the immediately preceding group of output lines (except for the input offsets), are replaced with a line comprised of a single asterisk.

-x      
   Two-byte hexadecimal display. Display the input offset in hexadecimal, followed by eight, space separated, four column, zero-filled, two-byte quantities of input data, in hexadecimal, per line.

For each input file, hexdump sequentially copies the input to standard output, transforming the data according to the format strings specified by the -e and -f options, in the order that they were specified.

Examples

Using the -b option will display the input offset in hexadecimal format. This output format is also called One-byte-octal display format. The input offset is followed by sixteen space separated, three-column, zero-filled bytes of input data in octal representation per line.

$ hexdump -b test.txt 
0000000 163 160 145 143 151 146 171 040 141 040 146 151 154 145 040 164
0000010 150 141 164 040 143 157 156 164 141 151 156 163 040 157 156 145
0000020 040 157 162 040 155 157 162 145 040 156 145 167 154 151 156 145
0000030 012 163 145 160 141 162 141 164 145 144 040 142 171 040 157 156
0000040 145 040 157 162 040 155 157 162 145 040 156 145 167 040 154 151
0000050 156 145 163 056 012 154 151 156 145 163 040 167 150 157 163 145
0000060 040 146 151 162 163 164 040 156 157 156 055 142 154 141 156 153
0000070 040 143 150 141 162 141 143 164 145 162 040 151 163 040 141 040
0000080 150 141 163 150 040 155 141 162 153 040 050 043 051 040 141 162
0000090 145 040 151 147 156 157 162 145 144 056 012 012                
000009c

Using the -c option will display the data in character ascii format. The input offset would be shown in hexadecimal format. This output format is also called One-byte-character display. The input offset is followed by sixteen space separated, three-column, space-filled characters of input data per line.

$ hexdump -c test.txt 
0000000   s   p   e   c   i   f   y       a       f   i   l   e       t
0000010   h   a   t       c   o   n   t   a   i   n   s       o   n   e
0000020       o   r       m   o   r   e       n   e   w   l   i   n   e
0000030  \n   s   e   p   a   r   a   t   e   d       b   y       o   n
0000040   e       o   r       m   o   r   e       n   e   w       l   i
0000050   n   e   s   .  \n   l   i   n   e   s       w   h   o   s   e
0000060       f   i   r   s   t       n   o   n   -   b   l   a   n   k
0000070       c   h   a   r   a   c   t   e   r       i   s       a    
0000080   h   a   s   h       m   a   r   k       (   #   )       a   r
0000090   e       i   g   n   o   r   e   d   .  \n  \n                
000009c

Using the -C option will display the data in ASCII+hex format, also known as Canonical hex+ASCII display. This displays the input offset in hexadecimal format. The input offset is followed by sixteen space-separated, two column, hexadecimal bytes, along with the same sixteen bytes in %_p format enclosed in ``|'' characters. Here is working example of this command option.

$ hexdump -C test.txt 
00000000  73 70 65 63 69 66 79 20  61 20 66 69 6c 65 20 74  |specify a file t|
00000010  68 61 74 20 63 6f 6e 74  61 69 6e 73 20 6f 6e 65  |hat contains one|
00000020  20 6f 72 20 6d 6f 72 65  20 6e 65 77 6c 69 6e 65  | or more newline|
00000030  0a 73 65 70 61 72 61 74  65 64 20 62 79 20 6f 6e  |.separated by on|
00000040  65 20 6f 72 20 6d 6f 72  65 20 6e 65 77 20 6c 69  |e or more new li|
00000050  6e 65 73 2e 0a 6c 69 6e  65 73 20 77 68 6f 73 65  |nes..lines whose|
00000060  20 66 69 72 73 74 20 6e  6f 6e 2d 62 6c 61 6e 6b  | first non-blank|
00000070  20 63 68 61 72 61 63 74  65 72 20 69 73 20 61 20  | character is a |
00000080  68 61 73 68 20 6d 61 72  6b 20 28 23 29 20 61 72  |hash mark (#) ar|
00000090  65 20 69 67 6e 6f 72 65  64 2e 0a 0a              |e ignored...|
0000009c
$   

Using the -o option will display the input offset in hexadecimal, followed by eight space-separated, six column, zero-filled, two byte quantities of input data, in octal, per line. This format is also called two-byte octal display.

$ hexdump -o test.txt 
0000000  070163  061545  063151  020171  020141  064546  062554  072040
0000010  060550  020164  067543  072156  064541  071556  067440  062556
0000020  067440  020162  067555  062562  067040  073545  064554  062556
0000030  071412  070145  071141  072141  062145  061040  020171  067157
0000040  020145  071157  066440  071157  020145  062556  020167  064554
0000050  062556  027163  066012  067151  071545  073440  067550  062563
0000060  063040  071151  072163  067040  067157  061055  060554  065556
0000070  061440  060550  060562  072143  071145  064440  020163  020141
0000080  060550  064163  066440  071141  020153  021450  020051  071141
0000090  020145  063551  067556  062562  027144  005012                
000009c
$     

Using the -n option will display only 'n' length bytes of input. The below command with -n option only shows first 30 bytes of the input data.

$ hexdump -n 30 -C test.txt 
00000000  73 70 65 63 69 66 79 20  61 20 66 69 6c 65 20 74  |specify a file t|
00000010  68 61 74 20 63 6f 6e 74  61 69 6e 73 20 6f        |hat contains o|
0000001e
$

Using the -s option will skip first 'n' bytes of the input and display the rest of the data.

$ hexdump -s 30 -C test.txt 
0000001e  6e 65 20 6f 72 20 6d 6f  72 65 20 6e 65 77 6c 69  |ne or more newli|
0000002e  6e 65 0a 73 65 70 61 72  61 74 65 64 20 62 79 20  |ne.separated by |
0000003e  6f 6e 65 20 6f 72 20 6d  6f 72 65 20 6e 65 77 20  |one or more new |
0000004e  6c 69 6e 65 73 2e 0a 6c  69 6e 65 73 20 77 68 6f  |lines..lines who|
0000005e  73 65 20 66 69 72 73 74  20 6e 6f 6e 2d 62 6c 61  |se first non-bla|
0000006e  6e 6b 20 63 68 61 72 61  63 74 65 72 20 69 73 20  |nk character is |
0000007e  61 20 68 61 73 68 20 6d  61 72 6b 20 28 23 29 20  |a hash mark (#) |
0000008e  61 72 65 20 69 67 6e 6f  72 65 64 2e 0a 0a        |are ignored...|
0000009c
$ 

Using the -e option to specify a format string to be used for displaying data. Like one hex byte per line.

$ echo hello |hexdump -v -e '/1 "%02X \n"' 
68 
65 
6C 
6C 
6F 
0A 
$ 
Advertisements