===================
== Nathan's Blog ==
===================
infrequent posts about things I am working on

How to Get the First N Bytes of a File

big files bytes linux powershell tutorial wc

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 <a href="http://man7.org/linux/man-pages/man1/head.1.html">head</a> can help. Just running:

Will get you

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:

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.

That will place the output in a file called bytetest.txt. For more information on the Get-Content module call:

PS: In case you stumbled on this post and are looking for ways to figure out what encoding you are working with, might I recommend Joel SpolskyThe Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)” and Jeff Atwood’s “There Ain’t No Such Thing as Plain Text.” It offers great insight into file encodings and why they matter.