Splitting and joining files on a Mac

03 January 2008

Hoosgot a free file archiver/splitter for Mac? I couldn’t find one so I’m resorting to the command line to split and join big files.

Splitting a file

You can use the ’split’ command to chop up a file into parts. If you have multiple files or want to split a directory, archive it first using tar or zip. I generally tar everything up anyway.

Optional:
tar -czvf bigfile.tar.gz bigfile where bigfile is the name of the big file you want to archive.

Split the file using ’split’. You can tell split to break up the file into chunks of a given size using the -m switch; append k (for kilobytes) or m (for megabytes) to the number to specify units. For example,

split -b 50m bigfile.tar.gz bigfilechunked

creates 50MB files named bigfilechunkedaa, bigfilechunkedab etc. that when reassembled or joined yield the stuff of bigfile.tar.gz.

Joining the files

To join or reassemble the files you use the ‘cat’ command. The long way would be to cat each file individually to an output file, such as cat bigfilechunkedaa bigfilechunkedab > outfile and cat bigfilechunkedac >> outfile. The double arrow there is significant because it is appending the ac file to the outfile.

An easier way would be to use a simple bash script to cat everything out for you.

#!/bin/bash
prefix=$1
files=`ls -m $prefix*|tr -d ,`
cat `echo $files` >> outfile
file outfile

Just run the script followed by the name you gave the split file (in my case, bigfilechunked–it’s everything before the aa, ab, ac, etc.), and it will reassemble it for you. It doesn’t know the original name of the file or archive you might have used, so I’m just using outfile as a generic name. I therefore run ‘file’ on the outfile so you’ll know the file type. You can then rename it to something more suitable, in my case something like outfile.tar.gz.

A benefit of archiving the file first is that it preserves the original file name. In other words, when I untar outfile.tar.gz, it unpacks the file(s) with their original names.

Thank goodness there are free tools to combat the endless $19.95 Mac shareware world.

One Response to “Splitting and joining files on a Mac”

  1. Splitting and joining files on a Mac

    […] http://ceitl.zanestate.edu/blog/archives/2008/01/splitting-and-joining-files-on-a-mac/ asks Hoosgot, […]

Leave a Reply