Linux – is it possible to reverse xxd to get original file from binary file

binary filescommand linelinuxxxd

i have been using xxd to create a hexa representation of a file(any file like .jpg, .docx, .zip etc), like this,..

$ xxd -p original_file.zip > hexa_format.txt

and to reverse

$ xxd -r -p hexa_format.txt > original_file.zip

if anybody feels that this isn't the proper way, please correct me.
anyhow this isn't my question.
now i have to convert them to binary instead of hexa. so the command goes like this, if im correct.

$ xxd -p -b original_file.zip > binary_format.txt

my question is,
how do i reverse it back to the original file from the binary file(binary_format.txt) created from the above command. the man page of xxd says it cannot be done(in the last line).

-b | -bits
              Switch to bits (binary digits) dump, rather than hexdump.   This
              option  writes octets as eight digits "1"s and "0"s instead of a
              normal hexadecimal dump. Each line is preceded by a line  number
              in  hexadecimal and followed by an ascii (or ebcdic) representa‐
              tion. The command line switches -r, -p, -i do not work with this
              mode.

if this couldn't be done is there any other command that can do it,. like piping multiple comamnds so.

Best Answer

You will have to write your own binary to hex function ...

cut -f2-8 -d' ' infile | tr -d '\n' | tr ' ' '\n' | while read l; 
do 
  echo -n    $(bin_to_hex($l)) >> outfile
  (( ++n )) 
  (( n % 60 == 0)) && echo  "" >> outfile 
done

This should output the same format as -p which can then be run through -r. If you read the man page for xxd you will read that there is no -r for -b. It says so on the excerpt that you included in your question.

Related Question