Count the number of rows of a CSV file on Linux or macOS

CSV Banner

In this blog we will learn how to do the following

  1. Get the number of rows of a CSV file
  2. Get the number of rows of multiple CSV files

Prerequisite

  • Terminal installed in your system
  • Multiple csv files. So that we can test the commands.

Now, let us open a terminal and move your working directory to the folder which contains your CSV file. In my case I have created a folder test and placed multiple csv files inside the folder.

Terminal

As we have all the prerequisite ready, let us start run some commands

Get the number of rows of a CSV file

To get the number of rows of a csv file run the command. Replace filepath with your csv file path.

wc path/to/your/csv/file.csv

wc: prints newline, word and byte counts for files

CSV output 1

As we can see in the output that it prints 1000 1208 80362 test1.csv

1000: Number of rows/lines in a file. 1208: Number of words in a file. 80362: Size of the file in bytes. test1.csv: Name of the file

We can pass an extra argument to the command to get the number of row or lines of a file by using -l flag. Run the command below.

wc -l path/to/your/csv/file.csv

-l: prints the newline counts

CSV output 2

As we can see in the output above, we get 1000 test1.csv. It gives the number of rows/lines followed by the name of the file.

Get the number of rows of multiple CSV files

If we want to get the total rows of a csv file we run the below command:

cat *.csv | wc -l

CSV output 3

From the output we can see that there are 2201 lines in the files test1.csv test2.csv test3.csv test4.csv.

Conclusion

So far, we have learned how to count the number of rows in a CSV file using the wc command in the terminal. We also learned how to get the total number of rows in multiple CSV files using the cat command along with the wc command. These commands can be helpful when we want to quickly get the number of rows in our CSV files without having to open them in a spreadsheet application.

💌 If you'd like to receive more tutorials in your inbox, you can sign up for the newsletter here.

Please let me know if there's anything else I can add or if there's any way to improve the post. Also, leave a comment if you have any feedback or suggestions.

Discussions

Up next