Merging Split Files

October 11th, 2007

Jesus asks: I have a question about the split Command… How can I “come back” to “largefile” from 126 small files?It sounds like the split command was used? If so, then you can use a for loop with file concatenation. First, I will split the files for my example:

$ ls -l big.log
-rw-rw-r--  1 brock brock 175743061 Oct 11 22:08 big.log
$ expr 175743061 \/ 126
1394786
$ split -b 1394786 big.log
$ ls -1 x*
xaa
xab
...
xew

Split creates its output files in decreasing alphabetic order. Thus when you list them in the shell they are in the order in which
they were split. As such, you can simply use cat to merge the files. (Thanks to Paul and Davidov for pointing out my for loop was superfluous.)

$ cat x* >merged.big.log
$ ls -l *.log
-rw-rw-r--  1 brock brock 175743061 Oct 12 22:08 big.log
-rw-rw-r--  1 brock brock 175743061 Oct 12 01:11 merged.big.log
$ diff merged.big.log big.log
$ md5sum *.log
47de08911534957c1768968743468307  big.log
47de08911534957c1768968743468307  merged.big.log

2 Responses to “Merging Split Files”

  1. Elias Pipping Says:

    Is there a reason to use md5sum instead of cmp?(*)

    (*) except that ‘cmp’ is way too easily typed as ‘cp’

  2. admin Says:

    Elias Pipping,

    No, I am just used to using md5sum. However, if on Solaris, I use cmp. Good point!

Leave a Reply

If Wordpress eats your comment (shell output, loops, ex..) email the text to me.