17

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

Can anyone help me?

Thanks

1

3 Answers 3

16

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
1
  • 1
    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) Jan 28, 2014 at 10:29
16

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.

6

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).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .