How to Combine CSV Files Using Terminal


How can we merge multiple CSV files into a single file using the command line?

Suppose we have a directory with multiple .csv files.

📂current_dir  
 ┣ 📜file1.csv 
 ┣ 📜file2.csv
 ┣ 📜file3.csv

Let’s create one combined.csv that contains the contents of all the files.

First, we’ll need to navigate to the directory containing the CSV files using cd in the terminal.

Merge All CSV Files

In order to merge all CSV files in a directory, we can run the following command:

cat *.csv > combined.csv

cat will output a file to the terminal.

The angle bracket > redirects the left side of the operator into the file specified on the right side.

So, in this command, we are outputting all the CSV files in the current folder and storing it into combined.csv.

Merge Specific CSV Files

If we don’t want to merge every file, but rather specify the CSV files to merge, we can explicitly type the filenames.

cat file1.csv file2.csv > combined.csv