There is a similar tool called xxd. If you run xxd with just a file name it dumps the data in a fairly standard hex dump format:

# xxd bdata
0000000: 0001 0203 0405
......

Now if you pipe the output back to xxd with the -r option and redirect that to a new file, you can convert the hex dump back to binary:

# xxd bdata | xxd -r >bdata2
# cmp bdata bdata2
# xxd bdata2
0000000: 0001 0203 0405
Answer from Bert on Stack Overflow
🌐
Linux Man Pages
linux.die.net › man › 1 › xxd
xxd(1): make hexdump/do reverse - Linux man page
When editing hexdumps, please note ... the printable ascii (or ebcdic) columns are always ignored. Reverting a plain (or postscript) style hexdump with xxd -r -p does not depend on the correct number of columns....
Top answer
1 of 8
30

There is a similar tool called xxd. If you run xxd with just a file name it dumps the data in a fairly standard hex dump format:

# xxd bdata
0000000: 0001 0203 0405
......

Now if you pipe the output back to xxd with the -r option and redirect that to a new file, you can convert the hex dump back to binary:

# xxd bdata | xxd -r >bdata2
# cmp bdata bdata2
# xxd bdata2
0000000: 0001 0203 0405
2 of 8
10

Restore file, given only the output of hexdump file

If you only have the output of hexdump file and want to restore the original file, first note that hexdump's default output depends on the endianness of the system you ran hexdump on!

If you have access to the system that created the dump, you can determinate its endianness using below command:

[[ "$(printf '\01\03' | hexdump)" == *0103* ]] && echo big || echo little

Reversing little-endian hexdump

This is the most common case. All x86/x64 systems are little-endian. If you don't know the endianness of the system that ran hexdump file, try this.

sed 's/ \(..\)\(..\)/ \2\1/g;$d' dump | xxd -r

The sed part converts hexdump's format into xxd's format, at least so far that xxd -r works.

Reversing big-endian hexdump

sed '$d' dump | xxd -r

Known Bugs (see comment section)

  • A trailing null byte is added if the original file was of odd length (e.g. 1, 3, 5, 7, ..., byte long).
  • Repeating sections of the original file are not restored correctly if they were hexdumped using a *.

You can check your dump for above problematic cases by running below command:

grep -qE '^\*|^[0-9a-f]*[13579bdf] *$' dump && echo bug || echo ok

Better alternative to create hexdumps in the first place

Besides the non-posix (and therefore not so portable) xxd there is od (octal dump) which should be available on all unix-like systems as it is specified by posix:

od -tx1 -An -v

Will print a hexadecimal dump, grouping digits as single bytes (-tx1), with no Address prefixes (-An, similar to xxd -p) and without abbreviating repeated sections as * (-v). You can reverse such a dump using xxd -r -p.

🌐
Linux Journal
linuxjournal.com › content › doing-reverse-hex-dump
Doing a Reverse Hex Dump | Linux Journal
June 2, 2008 - # xxd bdata | xxd -r >bdata2 # cmp bdata bdata2 # xxd bdata2 0000000: 0001 0203 0405 ...... Note that when doing reverse conversions with xxd, the data needs to look like a hex dump: there needs to be an offset and the data needs to be formatted correctly. So, for example, this works:
🌐
Man Pages
manpages.org › xxd
man xxd (1): make a hexdump or do the reverse.
Hexdump from file position 0x100 ( = 1024-768) on. % sh -c "dd of=plain_snippet bs=1k count=1; xxd -s +-768 > hex_snippet" < file
🌐
GitHub
github.com › Nicoretti › xxd-rs
GitHub - Nicoretti/xxd-rs: make a hexdump or do the reverse. · GitHub
user@host:~$ xxd-rs dump Cargo.toml 00000000: 5b70 6163 6b61 6765 5d0a 6e61 6d65 203d [package].name = 00000010: 2022 7878 642d 7273 220a 6465 7363 7269 "xxd-rs".descri 00000020: 7074 696f 6e20 3d20 2241 2072 7573 7420 ption = "A rust 00000030: 636c 6f6e 6520 6f66 2078 7864 220a 7265 clone of xxd".re 00000040: 706f 7369 746f 7279 203d 2022 6874 7470 pository = "http 00000050: 733a 2f2f 6769 7468 7562 2e63 6f6d 2f4e s://github.com/N 00000060: 6963 6f72 6574 7469 2f78 7864 2d72 7322 icoretti/xxd-rs" 00000070: 0a72 6561 646d 6520 3d20 2252 4541 444d .readme = "READM 00000080: 452e 6d64 220a 6175
Starred by 18 users
Forked by 6 users
Languages: Rust
🌐
iw Wireless
real-world-systems.com › docs › xxd.1.html
xxd - make a hexdump or do the reverse 121724
% sh -c "cat > plain_copy; xxd -s 0 > hex_copy < file" dd one 1K block then hexdump from file position 0x480 (=1024+128) onwards.
🌐
O'Reilly
oreilly.com › library › view › linux-pocket-guide › 9780596806347 › re28.html
xxd [options] [files] - Linux Pocket Guide [Book]
February 18, 2004 - Similar to od, xxd produces a hexadecimal or binary dump of a file in several different formats. It can also do the reverse, converting from its hex dump format back into the original data. For example, the command:
Author: Daniel J. Barrett
Published: 2004
Pages: 200
Find elsewhere
🌐
SysTutorials
systutorials.com › docs › linux › man › 1-xxd
Xxd (1) Linux Manual Page - SysTutorials
May 19, 2026 - When editing hexdumps, please note ... the printable ascii (or ebcdic) columns are always ignored. Reverting a plain (or postscript) style hexdump with xxd -r -p does not depend on the correct number of columns....
🌐
Samba
lists.samba.org › archive › linux › 2013-October › 032586.html
[clug] command to reverse 'xxd -b'??
October 28, 2013 - Only I hit a speed bump, reversing the output of 'xxd -b'. [the man page says "No, we dont do this."] 'xxd -r -p' will happily reverse hex values into binary. [v. neat, learned that today] 'xxd -b' dumps file in binary, strings of ascii '0' and '1'. But I couldn't find a command that would ...
🌐
GeeksforGeeks
geeksforgeeks.org › linux-unix › xxd-command-in-linux
xxd Command in Linux - GeeksforGeeks
February 13, 2026 - The hexadecimal dump was saved in a file called "hexdump.txt," and we used the '-r' option to reverse this process, converting the hex dump back into its original format. The resulting data has been stored in a file named "reversed.txt."
🌐
Ubuntu
manpages.ubuntu.com › manpages › xenial › man1 › xxd.1.html
Ubuntu Manpage: xxd - make a hexdump or do the reverse.
-r skips everything on the input line after reading enough columns of hexadecimal data (see option -c). This also means, that changes to the printable ascii (or ebcdic) columns are always ignored. Reverting a plain (or postscript) style hexdump with xxd -r -p does not depend on the correct ...
🌐
Unix.com
unix.com › shell programming and scripting
Reverse hexdump without xxd - Shell Programming and Scripting - Unix Linux Community
October 19, 2017 - On the other hand, the "xxd" command with -r option performs reverse hexdump, while the "xxd" command without -r performs the (forward) hexdump. An example of hexdump is to convert ABCD into 41 42 43 44.
🌐
HowtoForge
howtoforge.com › home › linux xxd command tutorial for beginners (with examples)
Linux xxd Command Tutorial for Beginners (with Examples)
In this tutorial, we will discuss xxd using some easy-to-understand examples. But before we do that, it's worth mentioning that all examples here have been tested on an Ubuntu 22.04 LTS machine. The xxd command in Linux lets you create a hexdump or even do the reverse.
🌐
LinuxOPsys
linuxopsys.com › xxd-command-in-linux-with-examples
xxd Command in Linux with Examples
December 18, 2023 - Like od and hexdump commands, xxd is yet another tool used in Linux for hex dump creation. In addition, it can perform the reversion operation. In this tutorial, we learn about the xxd command in Linux with some examples.
🌐
Linux.org
linux.org › docs › man1 › xxd.html
xxd - make a hexdump or do the reverse. at Linux.org
XXD(1) General Commands Manual XXD(1) NAME xxd - make a hexdump or do the reverse. SYNOPSIS xxd -h[elp] xxd [options] [infile [outfile]] xxd -r[evert] [options] [infile [outfile]] DESCRIPTION xxd creates a hex dump of a given file or standard input. It can also convert a hex dump back to its original binary form.
🌐
The Geek Diary
thegeekdiary.com › xxd-command-expressed-in-hexadecimal-form
xxd command – Expressed in hexadecimal form – The Geek Diary
Similar to od, xxd produces a hexadecimal or binary dump of a file in several different formats. It can also do the reverse, converting from its hex dump format back into the original data. For example, here’s a hex dump of binary file /usr/bin/who: