Make line-sized pieces first
The collector has eight synthetic event records, one per line. We want review bundles that hold at most three records without breaking a record across files. split reads one input and writes an ordered family of output files. It does not modify the input.
Start in a directory where you may create a new split-demo subdirectory. mkdir must succeed before && lets the next command run; cd then makes that directory the working directory for the lab. The printf format '%s\n' prints every following quoted record and a newline. A backslash immediately before each displayed newline is Bash continuation syntax: it escapes that newline so the next physical line remains part of the same printf command. Those backslashes are not event data passed to printf. The shell's > redirection creates events.log from that output. The printf article explains why a fixed format should remain separate from its data.
The -l option takes a line-count argument. Here -l 3 means move to a new output file after every three complete lines. events.log is the input operand, and batch- is the output prefix. With no suffix options, GNU split appends two letters beginning with aa. The final printf receives the filenames expanded by the shell's batch-* glob and prints one name per line.
mkdir split-demo &&
cd split-demo &&
printf '%s\n' \
'E001 login ok edge01' \
'E002 login fail edge02' \
'E003 sudo ok edge01' \
'E004 scan deny edge03' \
'E005 config ok edge02' \
'E006 login fail edge04' \
'E007 export ok edge01' \
'E008 logout ok edge01' > events.log &&
split -l 3 events.log batch- &&
printf '%s\n' batch-*batch-aa
batch-ab
batch-acThe first two pieces contain three lines; the last contains two. POSIX specifies -l, although implementations differ in their default suffix capacity and diagnostics. Naming the prefix avoids mixing these files with split's default xaa, xab, and so on.
Reassemble in the same order
Pieces are useful only if their order remains recoverable. The shell expands batch-* in its locale-defined pathname order before cat runs. With this controlled ASCII prefix and aa, ab, ac suffixes, that is the production order. cat concatenates those files into rebuilt.log.
The && operators prevent the success message after a failed concatenation or comparison. cmp compares events.log and rebuilt.log byte for byte and produces no output when they match. Only then does printf print the confirmation. This checks the complete bytes, including every newline; a matching line count alone would not.
cat batch-* > rebuilt.log &&
cmp events.log rebuilt.log &&
printf '%s\n' 'rebuild matches'rebuild matchesPreserve the naming contract with the pieces. Renaming batch-ac to batch-a, mixing prefixes, or reconstructing numeric suffixes lexically after 9 can change the order. A manifest or archive is safer than asking a recipient to infer it.
A reused prefix can retain old records
split overwrites output names that it opens, but it does not remove higher-numbered pieces left by an earlier, larger run. That makes the output prefix a stateful namespace. Reusing it can produce a convincing mixture of current and stale data.
First, -l 2 creates four stale-* pieces from all eight events. The head article explains the -n count option; head -n 4 writes only the first four records to current.log. Splitting that shorter file with the same prefix overwrites stale-aa and stale-ab, but no new operation opens stale-ac or stale-ad. The final shell glob therefore still finds four files.
split -l 2 events.log stale- &&
head -n 4 events.log > current.log &&
split -l 2 current.log stale- &&
printf '%s\n' stale-*stale-aa
stale-ab
stale-ac
stale-adstale-ac and stale-ad still contain E005 through E008 from the first run. In a supplied log review, that can reintroduce superseded records without any split error. Use a new empty directory or a run-unique prefix, then publish the verified set atomically. Do not delete a broad wildcard in an uncertain working directory merely to make the symptom disappear.
Design suffixes for the consumer
Names can carry enough structure for sorting and file-type handling. The -d flag selects decimal suffixes instead of letters. The -a option takes a suffix length; -a 3 pads the numbers to 000, 001, and so on. --additional-suffix appends the literal .log after each generated suffix.
The -a option is specified by POSIX. The -d flag and --additional-suffix are GNU extensions, so scripts using them must verify the target implementation. Three digits also impose a finite namespace: choose the width from a defensible upper bound rather than copying this example blindly.
split -l 2 -d -a 3 --additional-suffix=.log events.log part- &&
printf '%s\n' part-*part-000.log
part-001.log
part-002.log
part-003.logZero padding keeps lexical and numeric order aligned for this range. The suffix does not change the contents: these are plain text fragments, not independently complete log formats with headers or checksums.
Byte limits and record limits answer different questions
A byte budget may be appropriate for storage or transport, but a byte boundary can cut through a line. Create three short records. The -b option takes a size argument and writes at most eight bytes per piece here. A size without a suffix is bytes; GNU also accepts units such as K for 1024 and KB for 1000, so spell the intended unit rather than relying on memory.
The loop is Bash syntax: for assigns each matching filename to f, do and done delimit the body, and the double quotes preserve the filename as one argument. printf labels the file. od then displays every byte in hexadecimal: -A n suppresses address offsets, -v prevents repeated lines from being abbreviated, and -t x1 selects one-byte hexadecimal units. The od article develops this byte view.
printf '%s\n' alpha beta gamma > records.txt &&
split -b 8 records.txt bytes- &&
for f in bytes-*; do
printf '%s:' "$f"
od -A n -v -t x1 "$f"
donebytes-aa: 61 6c 70 68 61 0a 62 65
bytes-ab: 74 61 0a 67 61 6d 6d 61
bytes-ac: 0abytes-aa ends with be, the first two bytes of beta; bytes-ab continues with ta. Byte splitting preserves reconstruction but not line independence. POSIX specifies -b. GNU -C, or --line-bytes, instead packs complete lines up to a byte target when possible, but a single overlong line still has to be split; it is a bounded-line policy, not a promise that records are always indivisible.
Ask for a number of complete-line chunks
Sometimes the desired output is three worker inputs rather than a fixed number of lines. GNU -n accepts a chunk specification. The form l/3 asks for three contiguous chunks, distributing complete lines without splitting them. Balancing those pieces requires knowing the input's extent. For a non-seekable input such as a pipe, GNU non-round-robin -n modes first copy the complete stream to $TMPDIR, falling back to /tmp when TMPDIR is unset or unavailable, and use that temporary copy to determine the size. Use a regular input file, or select a protected TMPDIR with enough free space, when that full temporary copy matters.
The loop uses wc -l to count newline characters in each piece. The < redirection makes the file wc's standard input, so wc prints only the number and not a filename. read assigns that number to the shell variable n; its -r flag keeps backslashes literal instead of interpreting them as escapes. The count is numeric here, but the defensive form avoids surprising interpretation if the pattern is reused. printf then prints the piece name and count.
split -n l/3 events.log equal- &&
for f in equal-*; do
read -r n < <(wc -l < "$f")
printf '%s %s lines\n' "$f" "$n"
doneequal-aa 3 lines
equal-ab 3 lines
equal-ac 2 linesThe <(...) form is Bash process substitution: it exposes wc's output through a temporary file-like pathname for read. Equal byte sizes are not equal work; a three-line chunk may contain more expensive events than another. Record the distribution policy so a consumer knows whether adjacency was preserved.
Round robin trades adjacency for streaming
GNU -n r/3 distributes records cyclically: the first record goes to output 1, the second to output 2, the third to output 3, and the fourth returns to output 1. Unlike l/3, round-robin mode can operate on a pipe because it does not need to measure the whole input first.
The explicit - operand tells split to read standard input from the pipe. The loop labels each output, and cat prints its complete contents. This is distribution, not matching: like the positional behavior explored in paste, the relationship comes from input order.
cat events.log | split -n r/3 - stream- &&
for f in stream-*; do
printf '%s\n' "[$f]"
cat "$f"
done[stream-aa]
E001 login ok edge01
E004 scan deny edge03
E007 export ok edge01
[stream-ab]
E002 login fail edge02
E005 config ok edge02
E008 logout ok edge01
[stream-ac]
E003 sudo ok edge01
E006 login fail edge04Each worker sees a subsequence, not a contiguous time window. Concatenating stream-aa, stream-ab, and stream-ac does not rebuild the original order. Use this policy only when independent processing tolerates reordered work and the merge step has an explicit identity or sequence field.
Change the record separator when newline belongs to data
Unix pathnames may contain newline but cannot contain NUL, the zero byte. For a supplied pathname stream, GNU -t changes split's record separator. The argument '\0' selects NUL. This affects where -l counts records; it does not turn each output into a self-describing file.
printf writes three synthetic NUL-terminated records, with an embedded newline inside the second. Its \000 escape writes the NUL bytes. split -t '\0' -l 2 puts two complete NUL records in nul-aa and one in nul-ab. The loop displays the bytes with od; 0a is newline and 00 is NUL.
printf 'cfg/a\000cfg/with\nnewline\000cfg/c\000' > paths.nul &&
split -t '\0' -l 2 paths.nul nul- &&
for f in nul-*; do
printf '%s:' "$f"
od -A n -v -t x1 "$f"
donenul-aa: 63 66 67 2f 61 00 63 66 67 2f 77 69 74 68 0a 6e
65 77 6c 69 6e 65 00
nul-ab: 63 66 67 2f 63 00Every producer and consumer must remain NUL-aware; shell variables cannot store NUL. This preserves outer record boundaries, but it does not define fields inside a record or validate that a supplied pathname is safe to open.
A per-piece filter is a command-execution boundary
GNU --filter sends each piece to a shell command instead of directly creating the normal output file. There are two parsing stages. The invoking Bash removes the outer single quotes and passes the enclosed filter text unchanged as one argument to split; it does not expand $FILE. For each piece, split starts a child shell and sets FILE to the generated output name. That child shell expands $FILE. The double quotes in "$FILE.gz" keep the expanded name plus .gz as one redirection target. This example keeps the filter text fixed: gzip -c compresses its standard input and uses -c to write the compressed bytes to standard output; the child shell's > writes those bytes to that target.
After listing the three compressed files, cat writes the gzip members to standard output. The first | operator connects that output to gzip's standard input. In gzip -dc, -d selects decompression and -c writes the decompressed bytes to standard output instead of replacing a file. The second | connects those bytes to cmp. In cmp - events.log, the - operand means standard input, so cmp checks the reconstructed stream byte for byte against events.log. Concatenated gzip members are a documented gzip format feature; the comparison, not the filename extension, proves reconstruction here.
split -l 3 --filter='gzip -c > "$FILE.gz"' events.log zipped- &&
printf '%s\n' zipped-*.gz &&
cat zipped-*.gz | gzip -dc | cmp - events.log &&
printf '%s\n' 'compressed rebuild matches'zipped-aa.gz
zipped-ab.gz
zipped-ac.gz
compressed rebuild matches--filter invokes a shell for each piece. Building that command from untrusted text creates shell-injection risk, and a sudo or set-user-ID policy that grants split can effectively grant command execution; this is a capability and unsafe delegation, not evidence of a split vulnerability. GTFOBins documents the boundary. Keep the filter fixed, quote FILE, use an empty destination, check every status, and never treat a file extension as integrity evidence.
Write down the boundary contract
A trustworthy split operation answers five questions before it runs: what constitutes a record, whether a piece may break that record, how output names sort, whether the destination namespace is empty, and how reconstruction will be verified. Line counts, byte budgets, balanced contiguous chunks, and round-robin work queues are different contracts.
The command is small, but its consequences live outside it. split does not remove stale pieces, authenticate an input, encrypt output, make workers idempotent, or prove that every delivered piece arrived. Use controlled destinations, preserve a manifest and source digest, check exit status, and verify the exact reassembly or downstream completion rule. Recheck extension parity on the implementation you deploy: GNU Coreutils 9.7 was executed here, while POSIX guarantees only the portable subset identified above.