Merge CSV files from the command line
How command-line CSV merges work, what they change in your data, and when a simple line-based command is enough versus a CSV-aware tool.
Merging CSV files from a terminal usually means one of two things: stacking rows from several files that share the same columns, or joining files side by side on a shared key column. These are different operations, and the right command depends on which one you need. This review covers both and focuses on whether the merged file still means what the source files meant.
Short recommendation
If every file has the same single-line header, the same column order, the same delimiter and the same encoding, a line-based command with awk or tail is fast, needs no extra software, and is enough. Its main tradeoff is that it does not understand CSV structure. It cannot detect columns in a different order, quoted fields that contain line breaks in the header, or a file that uses semicolons instead of commas. In those cases it can produce a file that looks valid but has misaligned data, with no error message. When the files come from different exports, different people or different software versions, use a CSV-aware tool such as csvkit, Miller or xsv/qsv. For joining on a key column, use a CSV-aware tool from the start.
Stacking vs. joining
| Operation | What it does | Typical tools |
| Stack (concatenate rows) | Appends rows from each file under one header | awk, tail, csvstack, mlr cat, xsv cat rows |
| Join (combine columns) | Matches rows across files by a key column and puts their fields on one row | csvjoin, mlr join, xsv join |
| Plain concatenation (cat) | Appends raw bytes, including every header line | cat, type (Windows) |
What a merge actually changes
A careful row stack should change only the file layout. The values stay the same, the extra header lines are removed, and rows are combined in a predictable order. The problems come from what the command does not change or check:
- Duplicate headers: plain cat keeps every file's header line, so later headers appear as data rows.
- Column order: line-based tools append text as-is. If one file lists email before name and another lists name before email, the values end up under the wrong headings.
- Missing trailing newline: with cat or tail, if a file does not end in a newline, its last row is joined to the first row of the next file. awk prints each record with its own line ending, so it avoids this problem.
- Line endings: files saved with Windows CRLF endings and files with Unix LF endings can be mixed in one output. Many tools accept this, but the stray carriage return can become part of the last field value.
- Byte order marks: some exports start with a UTF-8 byte order mark. Skipping each later file's header line also drops its mark. Plain cat leaves the marks in the middle of the merged file, where they can become part of a field value.
- Encoding and delimiter: no merge command converts between encodings or delimiters. A file in a legacy Windows code page, or a semicolon-delimited export, needs to be converted before merging.
It also helps to separate stored changes from display changes. The merged file stores text exactly as written. A spreadsheet app that opens the file may drop leading zeros, turn some codes into dates, or guess the wrong encoding. That is a display interpretation, not a change in the file. It becomes a real change only if the file is saved again from that application.
Check headers before merging
This command prints each distinct header line with the number of files that use it. If you get more than one line, the files differ in column names, order, line endings or byte order marks, and a plain line-based merge is not safe.
head -q -n 1 *.csv | sort | uniq -c
Line-based stack with awk
This command keeps the first file's header and skips the first line of every later file. Write the output to a different directory, or use a name the pattern does not match, so a later run does not read the previous result as input.
awk 'FNR == 1 && NR != 1 { next } { print }' *.csv > ../merged.csv
Shell patterns expand in sorted name order, so file10.csv comes before file2.csv. If row order matters, list the files explicitly or use zero-padded names. This approach assumes the header occupies exactly one physical line. Data rows with quoted line breaks pass through unchanged, because the command only drops the first line of each file.
CSV-aware stacking
These tools parse quoting and headers instead of treating the file as plain lines.
# csvkit: stack rows, optionally recording the source file
csvstack *.csv > ../merged.csv
csvstack --filenames -n source *.csv > ../merged.csv
# Miller: stack rows; unsparsify fills in columns missing from some files
mlr --csv cat *.csv > ../merged.csv
mlr --csv unsparsify *.csv > ../merged.csv
# xsv or qsv: stack rows from files with matching columns
xsv cat rows *.csv > ../merged.csv
These tools handle files with different columns in different ways, and the behavior has changed between releases. Check the documentation for your installed version before you rely on one of them to align columns by name. Miller's unsparsify, used without a fixed field list, reads every record before it writes any output, so memory use grows with the size of the input.
Joining on a key column
The coreutils join command needs input sorted on the key and does not understand CSV quoting, so it can split quoted fields that contain commas. CSV-aware joins avoid that problem:
# csvkit: inner join by default; --left or --outer keep unmatched rows
csvjoin -c id left.csv right.csv > ../joined.csv
# Miller: -f names the left file; --ul and --ur emit unpaired rows
mlr --csv join -j id -f left.csv right.csv > ../joined.csv
An inner join silently drops rows that have no match. Decide whether unmatched rows matter before you choose the join type, and compare row counts afterward.
PowerShell
Get-ChildItem *.csv | ForEach-Object { Import-Csv $_.FullName } |
Export-Csv ..\merged.csv -NoTypeInformation
This pipeline matches columns by header name. Export-Csv builds its columns from the first object it receives, so columns that appear only in later files can be left out. The default output encoding differs between Windows PowerShell and PowerShell 7, so set the Encoding parameter explicitly when the receiving application expects a specific encoding.
Will the merged file stay usable after sharing?
A merged CSV is still plain text, so copying, searching and editing work the same way they did with the source files. Screen readers and other assistive technology read it like any other text file or table. The practical risks come from the receiving application, not from the merge:
- Encoding detection: some spreadsheet applications rely on a byte order mark to detect UTF-8. Without one, accented letters and non-Latin scripts may display incorrectly. Whether a given application needs the mark depends on the application and version, so test with the actual destination.
- Delimiter expectations: in regions where the comma is the decimal separator, spreadsheet software may expect semicolon-delimited files.
- Type guessing: identifiers with leading zeros, long numbers and date-like codes can be reformatted when the file is opened. If the recipient works in a spreadsheet, tell them to import those columns as text.
No destination-specific testing was supplied for this review, so these points are risks to check, not confirmed behavior for any particular application.
Verifying the result
After stacking, the number of data rows should equal the total of the data rows in the inputs. Counting lines with wc -l gives that total only when no field contains a line break. A CSV-aware count avoids that limitation:
mlr --icsv --opprint count ../merged.csv
for f in *.csv; do mlr --icsv --onidx count "$f"; done
Also spot-check a few rows from each source file under the merged header. Column misalignment is the error most likely to go unnoticed.
Gaps in this review
This review does not include performance measurements, version-by-version behavior tests, or checks against specific spreadsheet or database import tools. The commands follow each tool's documented interface. Before you use any of them in an automated workflow, confirm how your installed version handles mismatched headers and encodings.