How to print first 10 bytes in hexadecimal of a file?
I need to print first 10 bytes of a file in hexadecimal from linux mint command prompt.
Can anyone help me?
Thanks
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
).