1

This question already has an answer here:

I have a large file in my unix box with 2 Gb. The files contains the xml lines. I want to split the files into say 10 files say each file is now of 204 MB ( approx ) so that combining the 10 files should give me back the original file which is of 2Gb. Note that the content should be reproducible when I merge the 10 files with the original file.

How one should do this in unix?

marked as duplicate by don_crissti, jimmij, Stéphane Chazelas bash Dec 11 '14 at 11:56

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3

There is a split command:

~$ split --help
Usage: split [OPTION]... [INPUT [PREFIX]]
Output fixed-size pieces of INPUT to PREFIXaa, PREFIXab, ...; default
size is 1000 lines, and default PREFIX is `x'.  With no INPUT, or when INPUT
is -, read standard input.
...
-n, --number=CHUNKS     generate CHUNKS output files.
...
CHUNKS may be:
N       split into N files based on size of input
K/N     output Kth of N to stdout
l/N     split into N files without splitting lines
l/K/N   output Kth of N to stdout without splitting lines
r/N     like `l' but use round robin distribution
r/K/N   likewise but only output Kth of N to stdout

So you only have to do

~$ split -n10 -d myfile mySubFile_

That create 10 files with numerical suffixes (-d option) with suffixe mySubFile_

~$ ls -1t
mySubFile_00
mySubFile_01
mySubFile_02
mySubFile_03
mySubFile_04
mySubFile_05
mySubFile_06
mySubFile_07
mySubFile_08
mySubFile_09

that you can recombine with

cat mySubFile_* > myfile

Not the answer you're looking for? Browse other questions tagged or ask your own question.