Puzzleshot #029 - Chunk Norris
You’ve been handed orders.csv.gz: 4GB compressed, about 40GB uncompressed, roughly 50 million rows. The team downstream wants it as about 500 smaller gzipped CSV files so they can load them in parallel.
The catch: the disk has 10GB free. There’s no room to uncompress the whole thing, even temporarily.
With just the usual Linux toolset, how would you produce the 500 files?
Reveal solution
The short version: split can pipe each chunk through a command instead of writing it to a file. Feed it the decompressed stream, let gzip compress each chunk as it goes, and the uncompressed data never touches the disk.
Round 1 - the command:
zcat orders.csv.gz | split -l 100000 -d -a 3 --filter='gzip > $FILE.csv.gz' - part_
What each piece does:
zcatstreams the decompressed rows to stdout. Nothing is written to disk.-l 100000starts a new chunk every 100,000 lines. It always cuts between lines, never in the middle of a row.--filterruns a command for each chunk, with the chunk on its stdin. The filename split would have used is passed in $FILE. The - means read from stdin, and part_ is the filename prefix.-d -a 3numbers the chunks part_000, part_001 and so on, three digits wide.
In this mode split writes no files itself. The only things that land on disk are the compressed chunks, written by gzip.
For the sample file (200,000 lines), use -l 400 and you get exactly 500 files, part_000.csv.gz through part_499.csv.gz.
Proof that it fits in the disk limit:
I capped the file size a process can write with ulimit -f 3000 (about 3MB), on a sample that is 7.8MB uncompressed. A plain gunzip of the file died with “File size limit exceeded”. The split pipeline under the same cap finished with 500 files, and cat part_*.csv.gz | zcat matched the original file byte for byte.
That last check works because concatenated gzip files decompress as one stream. That also makes it easy to put the chunks back together.
Round 2 - two gotchas:
1. Use single quotes around the filter.
With double quotes, your shell expands $FILE before split ever runs. It’s empty, so the command becomes gzip > .csv.gz, and every chunk overwrites the same hidden file. In my test, ls showed nothing at all, and ls -A showed one file, .csv.gz, holding only the last 400 rows.
2. Set -a when you use -d.
Without -a, GNU split 9.4 started with two-digit names, and after part_89 it jumped to part_9000, part_9001 and so on. That’s legal, but it’s a strange sort order. -a 3 keeps every name the same width.
Worth knowing: the stock split on macOS may not have –filter at all. If so, gsplit from Homebrew coreutils does.
The things to consider:
Why not split first, then gzip the pieces? That needs room for all 40GB of uncompressed chunks at once.
Why does the pipeline need so little disk? Only one chunk is in flight at a time, and it’s compressed before it’s written.
What would you check before sending the files? Count the rows across all chunks against the original, and run gzip -t on each file.
The takeaway:
When disk is the constraint, stream. Compress as the data flows, not after.