is it possible to reverse xxd to get original file from binary file
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 videos on Youtube
arvindh
Updated on September 18, 2022Comments
-
arvindh almost 2 years
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.
-
Admin over 9 years
xxd
does not output binary files. It outputs textual representations of files. Please read the manual page. The binary format mentioned above is just the bytes of the file written out in 1's and 0's.
-
-
arvindh over 9 yearsterminal troughs back this:
awk: run time error: not enough arguments passed to printf("...%&'") FILENAME="bin_jpg.txt" FNR=59 NR=59.
My usage is more or less like recovering the original file, -
Romeo Ninov over 9 yearstry to exec it on this way: awk '{printf "%s" $8}' binary_format.txt >out_file
-
arvindh over 9 yearsnow the error is,
awk: run time error: not enough arguments passed to printf("%s......") FILENAME="bin_jpg.txt" FNR=1 NR=1
. -
Romeo Ninov over 9 years'{printf "%s",$8}' And update your version of awk, seems your is very old
-
arvindh over 9 yearsShould i place these lines inside a file and then run it as a
.sh
,..? -
Romeo Ninov over 9 yearsThis will not recover the exact text. It just add enter on each 60th symbol!
-
Allen over 9 yearsWhy would you be interested only in the eighth field?
-
Allen over 9 yearsYou can put it in a file or on the command line. This will output the same format as
-p
: 60 hex values per line. -
Romeo Ninov over 9 years@Allen, check please how the format (binary) looks like. The original content is in field 8. The rest is counter and binary strings
-
Romeo Ninov over 9 yearsI see, so you rewrite the coder
-
Allen over 9 years@R. Ninov: Wrong. That is a textual representation that is not one to one. It uses a period, '.', for anything that doesn't have an ASCII printable character.
-
Romeo Ninov over 9 years@Allen, didn't check that, just test with ascii file
-
arvindh over 9 years@Allen, im suppose push 2 different files into another program(i know this sounds lame,.). This particular program accepts only binary representation/format of any file(it doesn't accepts .jpg, .zip etc) that's been given as input. That's the reason i need to convert them into binary. im not an expert, but since i came across with this
xxd
that can get binary format of a file, i chose this way. -
Allen over 9 yearsIn unix, a file is just a stream of bytes. What program are you talking about?
-
Allen over 9 yearsxxd takes a file that is a 'binary' format and outputs a TEXTUAL representation of it so it is not what you want anyway.
-
arvindh over 9 yearsTerminal says bash: command substitution: line 18: syntax error near unexpected token
$l' bash: command substitution: line 18:
bin_to_hex($l))' -
Allen over 9 yearsYou have to write your own function
bin_to_hex()
. You do understand that xxd outputs TEXTUAL representations of files, right? -
arvindh over 9 years@Allen, its just a code written in python, which doesn't have much file handling techniques like buffers or stringIO modules.(nothing huge or impressive about that program/code). so if you think this is not what im suppose to do, you have any suggestions.