How to Get the First N Bytes of a File

There comes a time when you just need to take a little off the top of a file, see what you are working with. That is where knowing how to use a utility like head can help. Just running:

$ head filename.txt

Will get you

Print the first 10 lines of each FILE to standard output.

http://man7.org/linux/man-pages/man1/head.1.html

But what if that file does not have nice lines? Large SQL dump files come to mind. head has an answer. Use the -c flag to print the beginning bytes of a file instead of lines. Change the command above to:

$ head -c 512 filename.txt

and you will get the first 512 bytes of the file! This comes in handy when trying to see what the file looks like or figure out what kind of encoding the file is using. And don’t worry, you can do something similar on Windows using PowerShell.