Linux/Unix – Splitting a File into Smaller Ones
File management tools include those for splitting, comparing, and compressing files, making backup archives, and tracking file revisions. Other management tools exist for determining the contents of a file, and for changing its timestamp.
Splitting a File into Smaller Ones
It’s sometimes necessary to split one file into a number of smaller ones. For example, suppose you have a very large sound file in the near−CD−quality MPEG2, level 3 (“MP3”) format. Your file, `large.mp3′, is 4,394,422 bytes in size, and you want to transfer it from your desktop to your laptop, but your laptop and desktop are not connected on a network−−the only way to transfer files between them is by floppy disk. Because this file is much too large to fit on one floppy, you use split.
The split tool copies a file, chopping up the copy into separate files of a specified size. It takes as optional arguments the name of the input file (using standard input if none is given) and the file name prefix to use when writing the output files (using `x’ if none is given). The output files’ names will consist of the file prefix followed by a group of letters: `aa’, `ab’, `ac’, and so on−−the default output file names would be `xaa’, `xab’, and so on.
Specify the number of lines to put in each output file with the `−l’ option, or use the `−b’ option to specify the number of bytes to put in each output file. To specify the output files’ sizes in kilobytes or megabytes, use the `−b’ option and append `k’ or `m’, respectively, to the value you supply. If neither `−l’ nor `−b’ is used, split defaults to using 1,000 lines per output file.
To split `large.mp3′ into separate files of one megabyte each, whose names begin with `large.mp3.’, type:
$ split −b1m large.mp3 large.mp3. RET
This command creates five new files whose names begin with `large.mp3.’. The first four files are one
megabyte in size, while the last file is 200,118 bytes−−the remaining portion of the original file. No alteration is made to `large.mp3′. You could then copy these five files onto four floppies (the last file fits on a floppy with one of the larger files), copy them all to your laptop, and then reconstruct the original file with cat.
To reconstruct the original file from the split files, type:
$ cat large.mp3.* 62; large.mp3 RET
$ rm large.mp3.* RET
In this example, the rm tool is used to delete all of the split files after the original file has been reconstructed.