How to Use the Linux wc Command to Count Lines, Words, and Characters in a File

If you have a massive log file or a long script and need to quickly ascertain its size, opening the file in a text editor like Nano or Vim simply to scroll to the bottom is highly inefficient, and doing so on a multi-gigabyte file can cause the terminal to crash.

Instead, Linux administrators rely on the incredibly fast, universally installed wc (word count) command to instantly calculate the total number of lines, words, and characters inside any text file.

How to Use the wc Command

The command works instantly, outputting the exact statistics to your terminal without ever actually opening the file graphically.

  1. Open your Linux terminal.
  2. Type wc followed by a space and the exact path to the file you want to analyze.
    wc /var/log/syslog
  3. Press Enter.

Understanding the Output

By default, if you use the command with no flags, the terminal will output a single line of text containing three numbers, followed by the filename. For example:

1420   8500   75000 /var/log/syslog

These numbers represent, in exact order:

  • Column 1 (1420): The total number of lines in the file.
  • Column 2 (8500): The total number of words in the file.
  • Column 3 (75000): The total number of raw bytes (characters) in the file.

Filtering the Output with Flags

If you are writing a bash script and only care about one specific metric—for instance, you just need the exact number of lines—you can use flags to isolate the data.

  • wc -l (Lines): Prints only the line count. (e.g., wc -l config.txt outputs 1420 config.txt).
  • wc -w (Words): Prints only the word count.
  • wc -c (Bytes): Prints only the total byte count.

This command is incredibly powerful when combined with the pipe (|) operator. For example, if you type ls -l | wc -l, Linux will count the number of lines generated by the ls command, instantly telling you exactly how many files are sitting inside your current directory.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.