To split a large CSV into smaller files on Linux or macOS, use split -l when each record occupies one text line. For example, split -l 100000 filename.csv file_ creates files of up to 100,000 lines. Smaller files can be easier to open in a CSV editor.
Check for line breaks inside quoted fields first. The split command counts text lines, not CSV records, and may cut a record in half. If your CSV has multiline fields, use the CSV Splitter instead. You can count text lines in Terminal to estimate the size of a simple CSV. If you only need to locate matching text, search the CSV in Terminal before splitting it; that guide also explains why a matching line is not always a complete CSV record.
Split quoted multiline records in Terminal
If you want to keep the operation in Terminal, use a CSV parser instead of line-based split. With Python 3 installed, run the following on a comma-separated UTF-8 file with a header. Replace 100000 with the maximum number of data records per part:
python3 - filename.csv csv_parts 100000 <<'PY'
import csv
import sys
from pathlib import Path
limit = int(sys.argv[3])
if limit < 1:
raise SystemExit("Rows per part must be positive")
with open(sys.argv[1], newline="", encoding="utf-8-sig") as source:
reader = csv.reader(source)
header = next(reader, None)
if header is None:
raise SystemExit("The CSV is empty")
folder = Path(sys.argv[2])
folder.mkdir() # Refuse an existing output folder.
target = None
total = parts = 0
try:
for row in reader:
if not row:
continue
if total % limit == 0:
if target:
target.close()
parts += 1
target = (folder / f"part_{parts:04d}.csv").open(
"x", newline="", encoding="utf-8")
writer = csv.writer(target)
writer.writerow(header)
writer.writerow(row)
total += 1
finally:
if target:
target.close()
print(f"{total} data records written to {parts} files")
PYThe new csv_parts folder contains part_0001.csv, part_0002.csv, and so on. Every part has the original header; the header does not use up a data-record slot. Empty lines are skipped. The script refuses an existing output folder so a rerun cannot overwrite earlier parts. Choose a new folder name for another run.
Before using it on the full file, try three data records with a part limit of 2, including a quoted comma and a quoted line break. Expect 3 data records written to 2 files, with two records in the first file and one in the second. Open both with a CSV-aware viewer and check that the full multiline value remains in one record. The source file is unchanged.
The Python CSV reader and writer handle quoting when writing each part. For a semicolon-delimited file, add delimiter=";" to both csv.reader(source) and csv.writer(target). The output preserves field values rather than the original byte-for-byte quoting or line endings.
macOS and Linux also include the split utility:
split [-l line_count] [-b byte_count] [-n chunk_count] [file [prefix]]
The command split breaks up files using:
- lines - useful for text-based files
- bytes - for binary files
- number - if you want to split a file into a specific number of parts.
CSV is a format for structured text data. For files with one record per line, use split with the line count option. The header counts toward the first file's line limit.
split -l 100000 filename.csvSplit generates files with basic names beginning with x: xaa, xab, xac, etc. See the next section to split your CSV files and rename the generated files.
Using split and renaming generated files
Split has two interesting parameters. The first is -d to use numeric suffixes (1, 2, 3, etc.) instead of letters. The second is the prefix parameter for the generated files. To define the prefix, just add a string after your CSV filename.
split -l 100000 -d filename.csv file_To give the generated files a .csv extension, rename the parts once:
for part in file_[0-9]*; do
[ -f "$part" ] || continue
case "$part" in *.csv) continue ;; esac
mv "$part" "$part.csv"
doneAdding CSV header to generated files
You have several generated files like: file_00.csv, file_01.csv, etc.
But the CSV header line is on the first file (file_00.csv) and all other files don't have a CSV header line.
Copy the original file's header into each later part. Write to a temporary file first so the shell does not truncate a part while reading it:
header_file=$(mktemp)
head -n 1 filename.csv > "$header_file"
for part in file_*.csv; do
[ -f "$part" ] || continue
[ "$part" = "file_00.csv" ] && continue
temp_file=$(mktemp)
cat "$header_file" "$part" > "$temp_file" && mv "$temp_file" "$part"
done
rm "$header_file"The first part already has the header. The remaining parts now have the same columns and can be opened separately. For a file with quoted line breaks, use a CSV-aware splitter instead of these commands.
For quoted multiline CSVs or a browser workflow with header handling, follow how to split a CSV into multiple files. Check overlap and output row counts before importing every part.



