
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.
PS$ Get-Content .\filename.txt -Encoding byte -TotalCount .5KB | Set-Content bytetest.txt -Encoding byte
That will place the output in a file called bytetest.txt. For more information on the Get-Content module call:
PS$ Get-Help Get-Content
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 Spolsky “The 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.