Intro To 'xxd' Command In Linux
2024-05-26 - By Robert Elder
Create Hex Dumps Back Of Binary Files
I use the 'xxd' command to produce a hexadecimal dump of a binary file:
xxd data
00000000: 01ab fe52 ee2a b577 1d98 6a81 ea10 87c5 ...R.*.w..j.....
00000010: b46f 8af2 134a 74b8 b01a 1d0c fb91 e8bb .o...Jt.........
00000020: 7ad6 89ca 0b29 f10f 6d20 043f 2223 e64c z....)..m .?"#.L
00000030: c109 716e f036 1a55 6bac 30f2 70e8 7cfa ..qn.6.Uk.0.p.|.
00000040: 8e2e 1865 55bf d922 af7c 033c b20b e0a5 ...eU..".|.<....
00000050: 980e 75f1 886d 7dea 680c 3bf7 c506 9cc8 ..u..m}.h.;.....
00000060: b09d cfe0 ....
Reversing Hex Dumps Back Into Binary Files
I can also use the '-r' flag to go the other way, by converting a hex dump back into a binary file:
echo "0: 4865 6c6c 6f20 576f 726c 6421 0a"
0: 4865 6c6c 6f20 576f 726c 6421 0a
echo "0: 4865 6c6c 6f20 576f 726c 6421 0a" | xxd -r
Hello World!
The '-p' flag can also be included to avoid the need to write out byte offsets in the input hex dump:
echo "48656c6c6f20576f726c64210a"
48656c6c6f20576f726c64210a
echo "48656c6c6f20576f726c64210a" | xxd -r -p
Hello World!
Using 'xxd' As A Hex Editor In Vim
Here, I have a jpeg image file of a face:
If I run the 'xxd' command on this file, the entire hex dump of the image file is printed on the terminal:
xxd face.jpg
00000000: ffd8 ffe0 0010 4a46 4946 0001 0101 012c ......JFIF.....,
00000010: 012c 0000 fffe 0013 4372 6561 7465 6420 .,......Created
00000020: 7769 7468 2047 494d 50ff e202 b049 4343 with GIMP....ICC
00000030: 5f50 524f 4649 4c45 0001 0100 0002 a06c _PROFILE.......l
00000040: 636d 7304 3000 006d 6e74 7252 4742 2058 cms.0..mntrRGB X
00000050: 595a 2007 e800 0500 0e00 0400 0c00 1161 YZ ............a
00000060: 6373 7041 5050 4c00 0000 0000 0000 0000 cspAPPL.........
00000070: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000080: 00f6 d600 0100 0000 00d3 2d6c 636d 7300 ..........-lcms.
00000090: 0000 0000 0000 0000 0000 0000 0000 0000 ................
000000a0: 0000 0000 0000 0000 0000 0000 0000 0000 ................
...
If I open this file in vim, I'll see a mess of binary characters:
vi face.jpg
However, I within the context of vim, I can use the exclamation mark character to run the contents of Vim through an external command, such as the 'xxd' command:
Now, I can see a hexadecimal representation of this file directly in vim.
I can also edit the hex dump within vim, for example by changing one section of the file to be all zeros:
00000500: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000510: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000520: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000530: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000540: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000550: 0000 0000 0000 0000 0000 0000 0000 0000 ................
and then run 'xxd' in reverse to convert the hex dump back into a binary image file:
Now, I can view the image file again, and see how the appearance of the image has changed based on the binary exits that I made:
And that's why the 'xxd' command is my favourite Linux command.
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
$1.00 CAD |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
The Most Confusing Grep Mistakes I've Ever Made
Published 2020-11-02 |
Use The 'tail' Command To Monitor Everything
Published 2021-04-08 |
An Overview of How to Do Everything with Raspberry Pi Cameras
Published 2019-05-28 |
An Introduction To Data Science On The Linux Command Line
Published 2019-10-16 |
Using A Piece Of Paper As A Display Terminal - ed Vs. vim
Published 2020-10-05 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|