split Stopped at Part 99 of 127 and Left 99 Good-Looking Files: --numeric-suffixes=1 Turns Off GNU split's Suffix Widening
A stand-in clip went into split to be cut into numbered parts for an uploader. The parts were glued back together as a check, and the rebuilt file came out 57,248 bytes short. Ninety-nine parts were sitting in the folder, numbered 01 to 99, looking exactly like a finished job. The only report of the problem was one line on standard error: split: output file suffixes exhausted.
The cause is one option. GNU split normally widens its suffixes on its own when it runs out of names. Ask it to start numbering at 1 instead of 0, which is the natural thing to want for files called part01, and that widening switches off. It stops at 99, exits 1, and leaves what it wrote.
Every block below was run on Linux with GNU coreutils split 8.32, GNU bash 5.1.16 and GNU diffutils cmp 3.8. Each script ran in a fresh, empty folder and every output block is pasted unedited. The clip is text from seq, so every run produces the same bytes.
The split that stopped at 99
#!/bin/bash # A stand-in clip of 260,000 bytes (text, so every run is identical), # split into 2 KiB parts numbered from 01 for an uploader. rm -rf run1 && mkdir run1 && cd run1 seq 1 60000 | head -c 260000 > clip.mp4 split -b 2K --numeric-suffixes=1 --additional-suffix=.part clip.mp4 clip.mp4. echo "split exit=$?" ls clip.mp4.*.part | wc -l ls clip.mp4.*.part | sed -n '1p;$p' cat clip.mp4.*.part > rebuilt.mp4 stat -c '%n %s' clip.mp4 rebuilt.mp4 cmp clip.mp4 rebuilt.mp4 && echo "rebuilt matches" The clip is 260,000 bytes and each part is 2 KiB, which is 2,048 bytes, so the job needs 127 parts. The suffix starts at 01 because the uploader numbers from one.
split: output file suffixes exhausted split exit=1 99 clip.mp4.01.part clip.mp4.99.part clip.mp4 260000 rebuilt.mp4 202752 cmp: EOF on rebuilt.mp4 after byte 202752, line 35643 split wrote 99 parts and quit. 99 parts of 2,048 bytes is 202,752 bytes, which is exactly where cmp found the rebuilt file ending. The last 28 parts were not written at all.
Two things make this easy to miss. The error goes to standard error, so a script that sends its output to a log, or to nowhere, does not show it. And the files that did get written are complete, correctly named and in order. A folder listing looks like success. Only the exit status, the part count or a byte comparison says otherwise.
Why the default would have worked
Leave the options out and split handles a large count by itself. The manual installed with coreutils 8.32 (info '(coreutils) split invocation') describes it: by default the suffix starts at two characters and split "will increase this width by two when the next most significant position reaches the last character". The names stay in order because the output is built "such that concatenating the output files in traditional sorted order by file name produces the original input file".
The same section gives the exception, in the entry for -a: the default length is 2, "and unless ‘-n’ or ‘--numeric-suffixes=FROM’ is specified, will auto increase the length by 2 as required". The entry for --numeric-suffixes repeats it. When FROM is given, "the auto suffix length expansion described above is disabled". It then adds the advice that would have prevented the whole thing: "Therefore you may also want to use option ‘-a’ to allow suffixes beyond ‘99’."
So the rule has three cases. Plain split and plain -d grow their suffixes. An explicit -a fixes the width. --numeric-suffixes=1 (or any FROM) also fixes the width, at 2, even without -a on the command line.
Five ways to name the same parts
This script splits the same clip five ways and checks each result by concatenating the parts in glob order and comparing the bytes with the original.
#!/bin/bash # Same 260,000-byte clip, 2 KiB parts, five ways of naming them. # "rebuilds" = cat of the parts in glob order is byte-identical to the clip. seq 1 60000 | head -c 260000 > clip.mp4 try() { rm -rf t && mkdir t split -b 2K "$@" clip.mp4 t/p 2>&1; rc=$? cat t/* | cmp -s - clip.mp4 && same=yes || same=no printf '%-27s exit=%s parts=%-3s %s .. %-6s rebuilds=%s\n' "$*" "$rc" \ "$(ls t | wc -l)" "$(ls t | head -n 1)" "$(ls t | tail -n 1)" "$same" } try try -d try -d -a 2 try --numeric-suffixes=1 try --numeric-suffixes=1 -a 3 exit=0 parts=127 paa .. pew rebuilds=yes -d exit=0 parts=127 p00 .. p9036 rebuilds=yes split: output file suffixes exhausted -d -a 2 exit=1 parts=100 p00 .. p99 rebuilds=no split: output file suffixes exhausted --numeric-suffixes=1 exit=1 parts=99 p01 .. p99 rebuilds=no --numeric-suffixes=1 -a 3 exit=0 parts=127 p001 .. p127 rebuilds=yes Reading the rows in order:
- No options. Letters,
paathroughpew, 127 parts, rebuilds. Two letters give 676 names, so this run did not need to widen. -
-d. Digits from zero. Afterp89the names jump top9000, and the last one isp9036. That looks odd but it is the widening at work: a leading9sorts after every two-digit name from00to89, so the glob still lists the parts in the right order, and the rebuild matches. -
-d -a 2. The width is now fixed. 100 names,00to99, then the same error and exit 1. -
--numeric-suffixes=1. No-atyped, but the width is fixed anyway. 99 names,01to99. This is the run that looked finished. -
--numeric-suffixes=1 -a 3. Three digits,p001top127, exit 0, rebuilds.
The second row is worth a second look. If you only ever saw the -d output on small files, you would expect two digits forever. On a large one, the names change shape partway through. Anything downstream that parses the number out of the name, for example to show progress as part 91 of 127, needs to handle 9000 style names too, or the numbering should be pinned with -a so the shape stays the same.
Rerunning in the same folder makes it worse
The obvious repair is to add -a 3 and run again. In a clean folder that works, as the last row above shows. In the folder the failed run left behind, it does not.
#!/bin/bash # Run the failed split, then "fix" it with -a 3 in the same folder. rm -rf run3 && mkdir run3 && cd run3 seq 1 60000 | head -c 260000 > clip.mp4 split -b 2K --numeric-suffixes=1 --additional-suffix=.part clip.mp4 clip.mp4. 2>/dev/null echo "first run exit=$?" split -b 2K --numeric-suffixes=1 -a 3 --additional-suffix=.part clip.mp4 clip.mp4. echo "second run exit=$?" ls clip.mp4.*.part | wc -l ls clip.mp4.*.part | sed -n '1,3p' cat clip.mp4.*.part > rebuilt.mp4 stat -c '%n %s' clip.mp4 rebuilt.mp4 cmp -s clip.mp4 rebuilt.mp4 && echo "rebuilt matches" || echo "rebuilt differs" first run exit=1 second run exit=0 226 clip.mp4.001.part clip.mp4.002.part clip.mp4.003.part clip.mp4 260000 rebuilt.mp4 462752 rebuilt differs The manual describes this for -a: when the names run out, split "reports an error without deleting the output files that it did create". The first run above shows the same thing happening with --numeric-suffixes=1 alone. The second run writes 001 to 127 beside the old 01 to 99. Both sets match clip.mp4.*.part, so the folder holds 226 parts and the rebuilt file is 462,752 bytes, the full 260,000 plus the 202,752 from the first attempt. The names also interleave in glob order (these runs used the C.UTF-8 locale): 009 comes before 01, and 01 before 010, so the bytes are not even in a usable order.
An uploader that sends every file matching a pattern would send all 226. Nothing errors: the second split exits 0, and cat is happy to join whatever it is given.
-n sets the width for you
If what you know is how many parts you want rather than how big each one is, -n picks the count up front. With a start number the width is still worked out, as this run shows.
#!/bin/bash # -n picks the number of files up front instead of the size of each one. seq 1 60000 | head -c 260000 > clip.mp4 for n in 99 127; do rm -rf t && mkdir t split -n "$n" --numeric-suffixes=1 clip.mp4 t/p 2>&1; rc=$? printf 'n=%s exit=%s parts=%s first=%s last=%s\n' "$n" "$rc" \ "$(ls t | wc -l)" "$(ls t | head -n 1)" "$(ls t | tail -n 1)" done n=99 exit=0 parts=99 first=p01 last=p99 n=127 exit=0 parts=127 first=p001 last=p127 99 parts get two digits and 127 parts get three, both starting at the chosen number, both exit 0. The catch is that the size of each part now comes from the division. -n 127 on this clip makes 126 parts of 2,047 bytes and a last one of 2,078, so this mode does not help when the receiver has a hard size limit per part. For that case, size first and compute the width yourself.
A split that checks its own work
This version works out how many parts the sizes require, sets the width from the last part number, splits into a folder it has just emptied, and refuses to report success until the count and the bytes both match.
#!/bin/bash # Work out the suffix width from the sizes, split into a fresh folder, # and prove the parts rebuild the clip before anything else touches it. seq 1 60000 | head -c 260000 > clip.mp4 # stand-in clip set -euo pipefail clip=clip.mp4 chunk=2048 size=$(stat -c %s "$clip") parts=$(( (size + chunk - 1) / chunk )) last=$parts # numbering starts at 1 width=${#last} if (( width < 2 )); then width=2; fi echo "size=$size parts=$parts width=$width" out=parts rm -rf "$out" && mkdir "$out" split -b "$chunk" --numeric-suffixes=1 -a "$width" \ --additional-suffix=.part "$clip" "$out/$clip." got=$(find "$out" -name '*.part' | wc -l) [ "$got" -eq "$parts" ] || { echo "expected $parts parts, got $got"; exit 1; } cat "$out"/*.part | cmp - "$clip" && echo "parts rebuild the clip: $got files in $out" size=260000 parts=127 width=3 parts rebuild the clip: 127 files in parts Notes on the choices:
- The width comes from the last number, not the count. With numbering from 1, the last part is number
parts, so its digit count is the width. If you number from some other FROM, the last number isFROM + parts - 1, and that is what to measure. - The minimum stays at 2 so small jobs keep the usual
01names. - The output folder is emptied first, so parts from an earlier attempt cannot join the glob.
- The check is a byte comparison against the original, not just a count.
cmpreading from a pipe compares the joined parts with the clip and exits non-zero on the first difference, andset -estops the script there. - The clip is made before
set -euo pipefail. Underpipefail,seq 1 60000 | head -c 260000fails:headstops reading early andseqexits on a broken pipe. That has nothing to do withsplit, but it stopped the first draft of this script before it printed anything, so it is called out here.
The same draft also stopped at (( width < 2 )) && width=2. When the width is already 3, the test is false, the line returns 1, and set -e ends the script. Written as an if, it does not.
A checklist for scripts that use split
- Search for
--numeric-suffixes=and-a. Either one fixes the suffix width. Make sure the width is computed from the job, or large enough for any input you will ever give it. - Check the exit status of
split. Nothing else in the run reports the problem. A run that runs out of names still leaves complete, correctly named files. - Count the parts.
ceil(size / chunk)is the number to expect. Compare it with what is on disk. - Compare the bytes before deleting the original.
cat parts/* | cmp - originalis cheap next to recording the clip again. - Split into an empty folder. Leftovers from a failed run match the same glob and get joined in.
- Do not parse part numbers out of
-dnames without pinning-a. Plain-dswitches from89to9000when it widens.
What these runs do not cover
- One version. GNU coreutils 8.32 on Linux. The quotes come from the info manual that ships with it. Newer releases may word things differently, and I did not test them.
- Other implementations. BSD and macOS
split, and the BusyBox applet, are separate programs and were not run here. - Line and record modes. Every run here splits by bytes with
-bor by count with-n.-l,-Cand-twere not tested. - Hex suffixes.
-xand--hex-suffixes=FROMare documented as working like the numeric options. I did not run them. - Real video. The clip is text from
seq. A real video file splits and rejoins the same way byte for byte, but no player was involved in these tests.
Sources. Quotes come from the split invocation node of the GNU Coreutils manual as installed with coreutils 8.32. The current online version is split invocation.
The clips that end up in a folder like this come from somewhere. Mine come from an old phone running Background Camera RemoteStream on Google Play, an Android app I make that records with the screen off. The scripts above work the same on any large file.
If a script already passes --numeric-suffixes=1 without -a, the size at which it starts dropping parts is 99 times its part size: 202,752 bytes at 2 KiB here, and 99 MiB at 1 MiB parts. Any clip larger than that loses its tail.