2024-12-08
6 min read

How to Count Number of Lines in Files Using Linux Terminal

How to Count Number of Lines in Files Using Linux Terminal

Counting lines in text files is a common task when analyzing data files, logs, or any text-based content. Linux provides several efficient methods to count lines in non-binary files like CSV, TXT, or configuration files.

Prerequisites

You'll need access to a Linux terminal. The commands shown work on most Linux distributions and also on macOS and other Unix-like systems.

Method 1: Using wc Command (Most Common)

The wc (word count) command is the standard tool for counting lines, words, and characters in files:

wc -l filename.txt

This outputs the line count followed by the filename:

150 filename.txt

To get only the number without the filename:

wc -l < filename.txt

Output:

150

Method 2: Counting Lines in Multiple Files

To count lines in multiple files simultaneously:

wc -l file1.txt file2.csv file3.log

Output:

   150 file1.txt
   237 file2.csv
    89 file3.log
   476 total

Use wildcards to count lines in all files of a specific type:

wc -l *.csv
wc -l *.txt
wc -l *.log

Method 3: Using cat and wc Together

For single files, you can pipe cat output to wc:

cat filename.txt | wc -l

This is less efficient than wc -l filename.txt but useful when combined with other operations:

cat file1.txt file2.txt | wc -l

Method 4: Using awk Command

The awk command can also count lines and is useful for more complex operations:

awk 'END {print NR}' filename.txt

This prints the number of records (lines) processed. The advantage of awk is that you can combine counting with other text processing:

awk 'NF > 0 {count++} END {print count}' filename.txt

This counts only non-empty lines (lines with at least one field).

Method 5: Using sed Command

The sed command can count lines as a side effect:

sed -n '$=' filename.txt

This prints the line number of the last line, effectively giving you the total count.

Method 6: Using grep Command

Count lines containing specific patterns:

grep -c ".*" filename.txt

The .* pattern matches any line (including empty lines). This is equivalent to wc -l.

To count only non-empty lines:

grep -c "." filename.txt

The . pattern matches lines with at least one character.

Counting Lines in CSV Files

For CSV files, you might want to count data rows (excluding headers):

# Count all lines including header
wc -l data.csv

# Count data rows only (excluding header)
tail -n +2 data.csv | wc -l

If your CSV has multiple header lines:

# Skip first 3 lines (headers) and count data rows
tail -n +4 data.csv | wc -l

Counting Lines with Conditions

Count non-empty lines only:

awk 'NF' filename.txt | wc -l

Count lines longer than 80 characters:

awk 'length > 80' filename.txt | wc -l

Count lines matching a pattern:

grep "error" logfile.txt | wc -l

Count lines NOT matching a pattern:

grep -v "debug" logfile.txt | wc -l

Working with Large Files

For very large files, wc -l is optimized and usually the fastest option:

time wc -l very_large_file.txt

If you need to monitor progress for extremely large files:

pv large_file.txt | wc -l

This shows a progress bar while counting (requires pv to be installed).

Counting Lines in Compressed Files

For compressed files, decompress and count in one command:

# For gzip files
zcat file.txt.gz | wc -l
gunzip -c file.txt.gz | wc -l

# For bzip2 files
bzcat file.txt.bz2 | wc -l

# For xz files
xzcat file.txt.xz | wc -l

Creating Reusable Functions

Add this function to your .bashrc or .zshrc:

count_lines() {
    if [ $# -eq 0 ]; then
        echo "Usage: count_lines <file1> [file2] [file3] ..."
        return 1
    fi

    for file in "$@"; do
        if [ -f "$file" ]; then
            lines=$(wc -l < "$file")
            echo "$file: $lines lines"
        else
            echo "$file: File not found"
        fi
    done
}

Usage:

count_lines data.csv logs.txt config.conf

Counting Lines in Directory Recursively

Count lines in all text files within a directory:

find /path/to/directory -name "*.txt" -exec wc -l {} + | tail -1

For specific file types:

# Count lines in all CSV files
find . -name "*.csv" -exec wc -l {} + | tail -1

# Count lines in all log files
find . -name "*.log" -exec wc -l {} + | tail -1

Practical Examples

Analyze log file growth:

# Count lines every hour
while true; do
    echo "$(date): $(wc -l < access.log) lines"
    sleep 3600
done

Compare file sizes:

echo "File sizes comparison:"
for file in *.csv; do
    printf "%-20s: %d lines\n" "$file" $(wc -l < "$file")
done

Quick data validation:

# Check if CSV has expected number of rows
expected_rows=1000
actual_rows=$(tail -n +2 data.csv | wc -l)
if [ "$actual_rows" -eq "$expected_rows" ]; then
    echo "Data validation passed: $actual_rows rows"
else
    echo "Data validation failed: expected $expected_rows, got $actual_rows"
fi

Performance Comparison

For counting lines in large files, here's the performance order (fastest to slowest):

  1. wc -l filename - Fastest, optimized for line counting
  2. wc -l < filename - Nearly as fast, no filename output
  3. awk 'END {print NR}' filename - Good for complex processing
  4. grep -c ".*" filename - Slower, but useful for pattern counting
  5. cat filename | wc -l - Slower due to unnecessary pipe

Next Steps

Now that you can count lines in files, you might want to learn about:

  • Advanced text processing with awk and sed
  • Analyzing CSV files with command-line tools
  • Working with log file analysis and monitoring
  • Using cut, sort, and uniq for data processing
Published: 2024-12-08|Last updated: 2024-12-08T10:00:00Z

Found an issue?