I think the difficulty is determining how the space can be recovered from the original files.
I think the following might work:
- Allocate a sparse file of the
combined size.
- Copy 100Mb from the end of the second file to the end of the new file.
- Truncate 100Mb of the end of the second file
- Loop 2&3 till you finish the second file (With 2. modified to the correct place in the destination file).
- Do 2&3&4 but with the first file.
This all relies on sparse file support, and file truncation freeing space immediately.
If you actually wanted to do this then you should investigate the dd
command. which can do the copying step
Someone in another answer gave a neat solution that doesn't require sparse files, but does copy file2 twice:
- Copy 100Mb chunks from the end of file 2 to a new file 3, ending up in reverse order. Truncating file 2 as you go.
- Copy 100Mb chunks from the end of file 3 into file 1, ending up with the chunks in their original order, at the end of file 1. Truncating file 3 as you go.