To count lines in a CSV file on Linux or macOS, run wc -l filepath.csv in Terminal. If the file has one header line and one record per line, subtract one from the result to get the number of data rows.

  1. Open Terminal. On macOS, you can use Spotlight with ⌘ + Space.
    Open Terminal using Mac Spotlight
    Open Terminal using Mac Spotlight
  2. Move terminal working directory to the folder containing your CSV file. Use ls to show current directory files and folders and cd to move inside a folder.
  3. Enter the following command. Replace filepath with your CSV file path:
wc -l filepath.csv
  1. The line count will be printed before the file name
    Word Count Command
    Word Count Command

The wc -l command counts newline characters, including the header line. For a typical CSV with a header and a final newline, a result of 101 means 100 data rows. A file without a final newline may need one added to the line count before you subtract the header.

Check the file format before treating this as a record count. Quoted CSV fields can contain line breaks, so one record may span several lines. In that case, wc -l, grep, and the commands below count text lines rather than CSV records. Use a CSV-aware editor or parser when you need an exact record count.

Get the number of rows containing a keyword in a CSV file

If each record occupies one line, combine grep and wc to count lines containing a keyword. This matches text anywhere on the line, including the header.

grep "keyword" filepath.csv | wc -l
Filter and Count Lines
Filter and Count Lines

You can also search for multiple keywords with grep using a regular expression (the -E option is for --extended-regexp). Any line with at least one of the keywords will be counted.

grep -E "keyword1|keyword2" filepath.csv | wc -l
Filter and Count Lines with multiple keywords
Filter and Count Lines with multiple keywords

By default, grep is case sensitive (uppercase and lowercase characters are distinct). To ignore case in the search, add the -i (for --ignore-case) in your command:

grep -i "keyword" filepath.csv | wc -l

or

grep -i -E "keyword1|keyword2" filepath.csv | wc -l

Get the number of rows of multiple CSV files

To count text lines across all CSV files in the current folder, use:

cat *.csv | wc -l
Get the sum of all csv files
Get the sum of all csv files

Each file's header contributes one line to that total. To make smaller files after checking their size, see how to split a large CSV in Terminal. You can also inspect the records in an online CSV editor.