Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Count Lines in File: Command Line How-To

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Counting the lines in a file is a common task when working in a terminal, whether you're checking log sizes, verifying data exports, or just curious about a text file's length. The command line offers several quick ways to do this, and this guide walks you through the most reliable methods on Linux, macOS, and Windows (via PowerShell or WSL). You'll need access to a terminal or command prompt—no special software required.

What You'll Need

  • A terminal (Command Prompt, PowerShell, or a Unix shell like Bash).
  • A text file whose line count you want to know.
  • Basic familiarity with navigating to the file's directory (using cd).

Step-by-Step

  1. Open your terminal. On macOS/Linux, launch Terminal. On Windows, open PowerShell or Command Prompt. If you're using Windows Subsystem for Linux (WSL), open your preferred Linux distribution.
  2. Navigate to the file's directory. Use the cd command to change to the folder containing your file. For example: cd /path/to/your/folder. You can type pwd (on Unix) or Get-Location (PowerShell) to see your current directory.
  3. Use the wc command (Unix/Linux/macOS/Git Bash). The most common utility is wc (word count). Run: wc -l filename.txt. The -l flag tells it to count lines. The output shows the line count followed by the filename. For example: 42 filename.txt.
  4. Use cat with nl or grep (alternative Unix methods). If you want to see the line numbers displayed alongside the content, use nl -ba filename.txt. To count only non-empty lines, use grep -c . filename.txt. To count all lines including empty ones with grep, use grep -c '' filename.txt (note the two single quotes).
  5. Use PowerShell (Windows). In PowerShell, run: (Get-Content filename.txt).Count. This reads the file and returns the number of lines. For large files, this might be slower, but it works for most cases.
  6. Use Command Prompt (Windows). The classic find command works: find /c /v "" filename.txt. This counts lines that do not match an empty string, effectively counting all lines. The output shows something like ---------- FILENAME.TXT: 42.
  7. Handle multiple files. With wc -l, you can list multiple files: wc -l file1.txt file2.txt. It will show each file's count and a total at the end. In PowerShell, you can use Get-ChildItem *.txt | ForEach-Object { (Get-Content $_).Count }.
  8. Count lines from a command's output (pipe). If you want to count lines from another command's output, use a pipe. For example, to count how many files are in a directory, run ls -1 | wc -l (Unix) or (Get-ChildItem).Count (PowerShell).

Common Mistakes

  • Forgetting the -l flag with wc. Without it, wc shows lines, words, and bytes, which can confuse you.
  • Using cat alone. cat filename just prints the file content; it doesn't give a count.
  • Quoting errors in find. The find /c /v "" command requires the empty string in quotes. Missing quotes or spaces can cause errors.
  • Assuming line count equals number of visible lines. If a file doesn't end with a newline character, the last line might not be counted by some tools. This is rare but can happen.
  • Using grep -c 'pattern' when you want all lines. That counts only lines matching the pattern, not all lines.

Tips

  • Use wc -l as your default. It's fast, simple, and available on virtually every Unix-like system, including Git Bash on Windows.
  • For huge files, avoid PowerShell's Get-Content. It loads the entire file into memory. Instead, use cmd /c find /c /v "" filename from PowerShell, or switch to WSL and use wc -l.
  • Check for trailing newline. If you want to count lines as defined by newline characters, remember that a file ending with a newline is standard. Tools like wc -l count newline characters, so a file with 5 lines but no newline at the end will show 4. To fix, you can add a newline at the end or use grep -c '' which counts all lines regardless.
  • Combine with sort and uniq for analysis. For example, sort file.txt | uniq -c | wc -l counts unique lines.
  • Use wc -l on compressed files with zcat. For gzipped files, run zcat file.gz | wc -l.

Troubleshooting

  • "wc: filename: No such file or directory" – The file isn't in your current directory. Check your spelling and use ls to list files. Ensure you're in the right folder.
  • PowerShell returns nothing or an error. Make sure you're using the correct filename and path. If the file is large, the command might take time. If you get an error about the file being in use, close any program that has it open.
  • Command Prompt says 'find' is not recognized. This is rare, but if find isn't available, use PowerShell instead. You can also try more filename | find /c /v "" but the direct find should work.
  • The count seems off by one. This usually indicates a missing trailing newline. As mentioned, wc -l counts newline characters. To count actual lines regardless of trailing newline, use grep -c '' filename on Unix, or in PowerShell, use (Get-Content filename | Measure-Object -Line).Lines.
  • On macOS, wc -l shows extra spaces. That's normal; the output format includes spaces. You can use wc -l < filename to get just the number without the filename.

Frequently Asked Questions

Does wc -l count blank lines?

Yes, it counts every newline character, so blank lines are included.

How do I count lines in all files of a folder?

Use wc -l * to count lines in all files in the current directory. It will show each file's count and a total.

Can I count lines in a file without opening it?

Yes, all the commands above do exactly that—they read the file without opening an editor.

What's the difference between wc -l and cat -n?

cat -n displays the file content with line numbers, but it doesn't give a total count. wc -l just gives the total count.

How do I count lines in a CSV file?

Same as any text file: wc -l data.csv. Note that if fields contain newlines, the count will be higher than the number of records.

Is there a way to count lines in real-time as a file grows?

You can use tail -f filename | wc -l but it will only output when the pipe is closed. A better approach is to use watch wc -l filename to update every 2 seconds.