How to extract the first x-megabyte from a large file in unix/linux?

umps picture umps · Aug 31, 2012 · Viewed 8.7k times · Source

I have a large file which I am only interested in the first couple of megabytes in the head.

How do I extract the first x-megabyte from a large file in unix/linux and put it into a seperate file?

(I know the split command can split files into many pieces. And using bash scripts I can erase the pieces I don't want. I would prefer a easier way)

Answer

sehe picture sehe · Aug 31, 2012

E.g.

 dd if=largefile count=6 bs=1M > largefile.6megsonly

The 1M spelling assumes GNU dd. Otherwise, you could do

 dd if=largefile count=$((6*1024)) bs=1024 > largefile.6megsonly

This again assumes bash-style arithmetic evaluation.