How to print first 10 bytes in hexadecimal of a file?

24,497

Solution 1

I came here seeing three answers thinking that I'd have nothing to add, and that this would be an exercise in how many people can post the same 1-liner in the first minute of a question being asked. But I find people using some new-fangled hexdump tool. That command is way longer than 2 letters; it alludes to some base other than The One True Base (base 8); and it's even apparent from its name what it does. Clearly this is not the Unix way.

So here's the joy of od ("octal dump").

First GNU, as you will find on your Linux Mint:

od --format=x1 --read-bytes=10 foo

Now BSD, where the irony is that it's actually the same program as hexdump:

od -t x1 -N 10 foo

Solution 2

Option -l <len> | -len <len> is for: stop after writing <len> octets.

Use it with a FILE like this:

xxd -l 10 FILE

or

hexdump -C -n 10 FILE

where -n <len> is the same as the -l <len> option from xxd.

Solution 3

You can use xxd to do that.

$ xxd -ps -l 10 FILENAME
546865204d4954204c69

This prints the first 10 byte (-l 10) of FILENAME in plain hex format (-ps).

Share:
24,497

Related videos on Youtube

techfun
Author by

techfun

Updated on September 18, 2022

Comments

  • techfun
    techfun over 1 year

    I need to print first 10 bytes of a file in hexadecimal from linux mint command prompt.

    Can anyone help me?

    Thanks

  • Adrian Pronk
    Adrian Pronk over 10 years
    I cut my teeth on 8-bit microprocessors where everything was done in hex. So to me, the one true base is 16. I've used Unix since about '84 and one thing I've always, always hated about it is its fixation with octal: I can't stand octal with its 3-bit encoding which won't seamlessly extend from one to multiple bytes. And octal integer literals in C and other languages is a train-wreck that still wreaks havoc to this day. (+1, by the way)