Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 384
umps Avatar asked Aug 31 '12 19:08

umps


2 Answers

Head works with binary files and the syntax is neater than dd.

head -c 2M input.file > output.file

Tail works the same way if you want the end of a file.

like image 148
Roger Heathcote Avatar answered Sep 20 '22 05:09

Roger Heathcote


On a Mac (Catalina) the head and tail commands don't seem to take modifiers like m (mega) and g (giga) in upper or lower case, but will take a large integer byte count like this one for 50 MB

head -c50000000 inputfile.txt > outputfile.txt
like image 42
WCBdata Avatar answered Sep 19 '22 05:09

WCBdata