This tutorial explains Linux “hexdump” command, options and its usage with examples.
Usage:
hexdump [-bcCdovx] [-e format_string] [-f format_file] [-n length] [-s skip] file…
* Hexdump is a Linux command that provides many options to dump file contents.
* It can dump file contents into formats such as hexadecimal, octal, ASCII, decimal.
* This command takes input either from a file or standard input.
* Hexdump is very helpful utility for debugging and verifying file contents written by any application program.
Hexdump utility provides options to display file contents in a 2-byte format
Here’s the listing of example usage of “hexdump” command:
We are going to use file sample.txt as input.
sanfoundry-> cat sample.txt 12345
1. To display file contents in hexadecimal format(hexdump -x file_path):
sanfoundry-> hexdump -x sample.txt 0000000 3231 3433 000a 0000005
Here as we can see, hexdump utility picks in chunk of 2 bytes from file and displays them from LSB(least significant) (i.e. last one) to MSB(Most significant) (i.e. first one). As in here from file sample.txt first chunk of 2 bytes is “12” and it is represented as 3231 in the hexdump format, since we know that ASCII value of 1 is 49 and in the hex form 49 is written as 31.Similarly 34 is represented as 3433 in the hexdump format. The last one i.e ‘\n’ character is represented as 0x0a Since ASCII value of ‘\n’ is 10. Here input offset is also shown and it is 0000000 and ending at 0000005 that shows that in all 5 bytes were dumped(4 of “1234” and 1 for ‘\n’).
A line displayed in output contains a maximum of 8 entries in it.
sanfoundry-> cat sam.txt ABCDEFGHJKLMNOPQRSTUVWXYZ sanfoundry-> hexdump -x sam.txt 0000000 4241 4443 4645 4847 4b4a 4d4c 4f4e 5150 0000010 5352 5554 5756 5958 0a5a 000001a
2. To display file contents in decimal format(hexdump -d file_path):
sanfoundry-> hexdump -d sample.txt 0000000 12849 13363 00010 0000005
As we know that btes are taken in reversed order in hexdump, So here “12” is taken as “21”.
“21” in ASCII Format – 5049.
In Binary format – [00110010][00110001].
“-d” option takes this sequence in one order and outputs the result as a whole.So here “0011001000110001” is taken and it’s decimal representation i.e 12849 is shown in the output.
3. To display file contents in octal format(hexdump -o file_path):
sanfoundry-> hexdump -o sample.txt 0000000 031061 032063 000012 0000005
As we know that bytes are taken in reversed order in hexdump, So here “12” is taken as “21”.
“21” in ASCII Format – 5049.
In Binary format – [00110010][00110001].
“-o” option takes this sequence in one order and outputs the result as a whole.So here “0011001000110001” is taken and it’s octal representation i.e 030061 is shown in the output.
Note
If you want to convert a Binary number in it’s octal representation faster take a group of 3 from the end and displays it. for e.g here for “..0 011 001 000 110 001” grouping is done in 3’s and display the output by converting each number in decimal form as 031061.
4. To display file contents in octal format byte by byte(hexdump -b file_path):
sanfoundry-> hexdump -b sample.txt 0000000 061 062 063 064 012 0000005
Here, every alphabet is dumped sequentially from “1” to “4” in octal format.
“1” in ASCII = 49 and “1” in octal = 061.
Similarly for 2, 3 and 4 is done and represented.
5. To display file contents in character display format(hexdump -c file_path):
sanfoundry-> hexdump -c sample.txt 0000000 1 2 3 4 \n 0000005
Here directly contents of the file are represented in the character format as it is.
6. To dump only specified bytes from input file(hexdump -s num_to_skip -[option] file_path):
sanfoundry-> hexdump -s 1 -c sample.txt 0000001 2 3 4 \n 0000005
Here -s option specifies the number of lines to skip in the output.
7. To dump only specified number of bytes from file(hexdump -n num_of_bytes_to_dump -[option] file_path):
sanfoundry-> hexdump -n 1 -c sample.txt 0000000 1 0000001 sanfoundry-> hexdump -n 1 -x sample.txt 0000000 0031 0000001
Here only specified number of bytes are dumped in the file.
8. To display file contents in hexadecimal plus ASCII format(hexdump -C file_path):
sanfoundry-> hexdump -C sample.txt 00000000 31 32 33 34 0a |1234.| 00000005
ASCII value of “1′ is 49 and in hex form it is “31”. So all the values are sequentially shown in hex form and contents of the file are shown in | |
brackets enclosed.
9. Formating options:
sanfoundry-> echo abcdefghijklm | hexdump -e '/1 "%_ax) "' -e '/1 "%02X" "\n"' 0) 61 1) 62 2) 63 3) 64 4) 65 5) 66 6) 67 7) 68 8) 69 9) 6A a) 6B b) 6C c) 6D d) 0A
Here option “_a” or “_A” for displaying input offset.string “abcdefghijklm” is shown byte by byte in hex format with the help of format string ‘/1 “%02X” “\n”‘.
10. To Use -A option:
sanfoundry-> echo abcdefghijklm | hexdump -e '/1 "%_Ax) "' -e '/1 "%02X" "\n"' 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 0A e)
Here only “a” is replaced by “A” from the previous command. Here, we can clearly see that after all data (i.e. “abcd…klm”) is processed, e)) is shown that is input offset to show number of characters processed.
11 . Make the dumping more verbose(.. hexdump -v -[option]):
sanfoundry-> ( echo "abcdefghijklmno"; echo "abcdefghijklmno" ) | hexdump -C 00000000 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 0a |abcdefghijklmno.| * 00000020 sanfoundry-> ( echo "abcdefghijklmno"; echo "abcdefghijklmno" ) | hexdump -v -C 00000000 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 0a |abcdefghijklmno.| 00000010 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 0a |abcdefghijklmno.| 00000020
Here we can see the outpus of both with “-v” and without “-v” option.
Some More examples of formating with file sam.txt
sanfoundry-> cat sam.txt HI I am rock1 I am a good boy HI I am rock2 I am a good boy HI I am rock3 I am a bad boy HI I am rock4 I am a bad boy HI I am rock5 I am a good boy
sanfoundry-> hexdump -v -e '"%010_ad |" 16/1 "%_p" "|\n"' sam.txt 0000000000 |HI I am rock1 I | 0000000016 |am a good boy .H| 0000000032 |I I am rock2 I a| 0000000048 |m a good boy .HI| 0000000064 | I am rock3 I am| 0000000080 | a bad boy .HI I| 0000000096 | am rock4 I am a| 0000000112 | bad boy .HI I a| 0000000128 |m rock5 I am a g| 0000000144 |ood boy .|
Here This command shows “Print the current decimal byte offset padded to 10 digits with 0’s, then print the | pipe character. Next take 16 items each 1 byte in size and print each item as a printing character. Finally print another | pipe character followed by a newline”.
Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.
- Practice Programming MCQs
- Check Information Technology Books
- Apply for Programming Internship
- Check Linux Books