Sunday, August 12, 2012

File splitting

Have you ever wanted to email a large file and discovered that it was JUST a bit larger than the maximum allowed file size? Have you ever wanted to copy a 6GB file but only had a 2GB USB flash drive to do it? Have you ever wanted to transfer a 4.7GB DVD image file through your free 2GB Dropbox account, and even considered paying the $5 monthly rate to get the overkill of 50GB limit?

I have. Well, not the Dropbox thing - I got 16GB free because I invited everyone I could when it started :). Even so, one time I couldn't move a 20GB file through.

And just now I'm asking a non-techie to send me a file several GB in size, but her Dropbox account is not large enough. Mine is, but I'm not giving her my password. Oh no! What to do?

So I googled a bit, and sure enough, Unix can solve that problem too! How, you may ask? With split!!

As described by man split:


SYNOPSIS
     split [-a suffix_length] [-b byte_count[k|m]] [-l line_count] [-p pattern] [file [name]]

DESCRIPTION
     The split utility reads the given file and breaks it up into files of 1000 lines each.  If file is a single dash (`-') or absent, split reads from the standard input.


So, say you have a file called IMG_4625.JPG that is around 3MB large:

3009327 Aug 12 00:05 IMG_4625.JPG

and for some reason you want to split it into files only 500K each. You can do:

split -b 500k IMG_4625.JPG 


This will create the following files:


512000 Aug 12 00:09 xaa
512000 Aug 12 00:09 xab
512000 Aug 12 00:09 xac
512000 Aug 12 00:09 xad
512000 Aug 12 00:09 xae
449327 Aug 12 00:09 xaf

When they are in this form, you can transfer them however you want. To put them back together, you just say:

cat x* > IMG_4625.JPG

And voilĂ , you have your file back! Easy, huh? Extra options are available through the man file.

Credits (thank you!):
http://stackoverflow.com/questions/2016894/easy-way-to-split-a-large-text-file

No comments:

Post a Comment