How to merge multiple CSV files into one
To merge CSV files into one, put them in a folder and combine them with Excel's Power Query (Data > Get Data > From File > From Folder), a one-line command, a short Python script, or an online merger. The catch is the header row: copying files end to end repeats it once per file, and files whose columns are in a different order end up misaligned. Each method below says how it handles both.
Last updated
Online, in your browser
Merge CSV files combines two or more CSV files into one with a single header row. Columns are matched by name, so files with a different column order, or with extra or missing columns, still line up. You can add a source_file column, and the files stay on your device.
In Excel with Power Query
- Put all the CSV files in one folder, and nothing else.
- In a blank workbook, go to Data > Get Data > From File > From Folder and select the folder.
- Click Combine > Combine & Load.
- In the dialog, pick the first file as the example, check the delimiter and click OK.
- Save the result with File > Save As > CSV UTF-8 (Comma delimited), or keep it as a workbook.
Power Query keeps one header, matches columns by name and adds a Source.Name column. When you add files to the folder later, Data > Refresh All merges them again.
On a Mac or Linux (Terminal)
This keeps the header of the first file only. It assumes all files have the same columns in the same order:
cd ~/Downloads/exports
awk 'FNR==1 && NR!=1 {next} {print}' *.csv > ../merged.csvThe result is written one folder up so the command does not read its own output. A plain cat *.csv > merged.csv also works but repeats the header once per file.
On Windows (PowerShell)
PowerShell reads each file as a table, so it writes one header. Columns are taken from the first file: a column that exists only in later files is dropped.
cd C:\Users\you\Downloads\exports
Get-ChildItem *.csv | ForEach-Object { Import-Csv $_.FullName } |
Export-Csv ..\merged.csv -NoTypeInformation -Encoding UTF8The older copy *.csv merged.csv in Command Prompt pastes the files together, header lines included, and can mix encodings.
With Python (pandas)
dtype=str keeps leading zeros and long IDs as they are, and columns are matched by name:
import glob
import pandas as pd
files = sorted(glob.glob("exports/*.csv"))
merged = pd.concat(
(pd.read_csv(f, dtype=str).assign(source_file=f) for f in files),
ignore_index=True,
)
merged.to_csv("merged.csv", index=False, encoding="utf-8")Which method to pick
| Method | One header | Columns matched by name | Good for |
|---|---|---|---|
| Merge CSV files (online) | Yes | Yes | A few files, private data |
| Excel Power Query | Yes | Yes | A folder you refresh every month |
| awk (Mac, Linux) | Yes | No | Many files with identical columns |
| PowerShell | Yes | First file's columns | Windows, no install |
| copy / cat | No | No | Quick checks only |
| Python pandas | Yes | Yes | Thousands of files, automation |
After merging, check the file in the CSV Viewer. If it is too big for Excel, cut it with the CSV splitter. To find rows that differ between two files rather than combine them, use Compare CSV files. Merging workbooks? Use Merge Excel files.
Frequently asked questions
Why is the header row repeated in my merged file?
Commands like copy *.csv on Windows or cat *.csv on a Mac paste the files end to end, headers included. Use a method that keeps only the first header: Power Query, the awk command on this page, pandas, or an online merger.
What if the files have columns in a different order?
Match the columns by name, not by position. Power Query, pandas and PrivateCSV's Merge CSV files do this. Plain copy or cat commands do not, and the data ends up in the wrong columns.
Can I tell which file each row came from?
Yes. Power Query adds a Source.Name column, the pandas example adds a source_file column, and Merge CSV files has an option to add one.
Can I merge CSV files into one Excel file with several sheets?
In Excel, import each CSV into its own sheet with Data > From Text/CSV and save the workbook. To combine Excel workbooks instead of CSV files, use a merge tool for Excel files.
How many files can I merge?
Command-line tools and Python handle thousands of files. Excel stops at 1,048,576 rows per sheet, so a very large merged result should stay a CSV file.