How to obtain a plain (raw) binary dump of any file in Unix/Linux?

9,659

A pragmatic solution with xxd, cut and tr:

xxd -b -c1 file | cut -d" " -f2 | tr -d "\n"
Share:
9,659

Related videos on Youtube

learnerX
Author by

learnerX

"Indeed a fool he is, who in his arrogance, thinks his knowledge is complete and hence proceeds to look down on others who continue to thirst for knowledge"

Updated on September 18, 2022

Comments

  • learnerX
    learnerX over 1 year

    I have read many similar question on the Stack Exchange community but none of them address a 'clean' binary dump. I have used: xxd and sed combinations to try to obtain a clean binary dump from any given file.

    Let me explain what I'm talking about. You have probably seen the output of `xxd -p', it looks something like this:

    f4d37fd532171a4a3445be1bb03e7d6b75f3871ffee937aebc9eb2c9417f
    f4e2fc8516e0a60a2c7e83c6a14c1a5e1b1e3f2ab9e12904cbd18aa2a4b9
    f493d317c7a3ce71b7ab99fad4b64431d34866ced4d1f3e3f38136860009
    fd08800c90491c8f7230d8b07936c4c0c29d2d12b138cdb2c544be9cc9fa
    0e79d66faf375737966b67e7c7bf7efcf1703616a4643193d9aaafd63215
    cf09a6e3e152bd01e9696b79f5faa5edd9f9b05628fdd9f77ee0dae1c5e8
    dc2351d59693d84abac088cc4cd72916b5499830e4482e214bda0892f38c
    60e9856fa111172a529222a7905e20b3c272a5b25c6fec9fbe386bb7f6cf
    

    There are no line numbers, no corresponding ASCII, nothing. Just plain old collection of bytes. That is the thing I'm after, except that I need the binary.

    There are two options: I could first obtain a plain xxd hex dump like the one shown above and convert that to binary, OR may be there's some tool or utility in bash that I don't know about that could do this.

    This is what I want: A binary dump looks like this:

    001101100011010100110110001100010011100001100001
    011000010011000000111001011001000011010100110010
    001101000011001101100001011000110110010100110000
    001100100110010001100001001100000011010100110011
    001110000110011000111001011000100110000100110010
    011001010110010000110011001100010110001100001010
    

    Any of the above is fine to me, as long as I obtain a clean plain binary dump from any file. So, my question is how do I obtain it--without any linebreaks or spaces or line numbers or nothing?