Cybersecurity Updates & Tools

wc Command in Linux: Count Lines, Words, Characters, and Bytes

The wc command in Linux counts lines, words, characters, and bytes in files or standard input. It is most useful in shell pipelines where you need a quick count of output from another command.

How to Use the wc Command in Linux

The basic syntax is:

bashwc [OPTIONS] [FILE]...

Without options, wc prints three columns in a fixed order — line count, word count, and byte count — followed by the filename:

bashwc /proc/cpuinfo
448 3632 22226 /proc/cpuinfo

When multiple files are passed, wc shows a count for each file and a total line at the end:

bashwc /proc/cpuinfo /proc/meminfo
448 3632 22226 /proc/cpuinfo 49  143  1363 /proc/meminfo497 3775 23589 total

wc right-aligns numbers in columns when processing multiple files, which adds leading spaces. To get a clean number with no filename and no padding, redirect the file as stdin instead of passing it as an argument:

bashwc -l < /proc/cpuinfo

This form is easier to parse in scripts because the output is just the number.

Count Lines, Words, Bytes, Characters, and the Longest Line

Each option prints only the requested count:

  • -l — count lines (counts \n newline characters)
  • -w — count words (any non-empty string delimited by whitespace)
  • -c — count bytes
  • -m — count characters (UTF-8 aware)
  • -L — print the length of the longest line
bashwc -l /etc/passwd              # line count onlywc -w ~/Documents/notes.txt   # word count onlywc -lL /proc/cpuinfo          # line count and longest line length

-c and -m return the same value for ASCII text, because each character is one byte. For files with multibyte UTF-8 characters — accented letters, emoji, or non-Latin scripts — -c returns a higher number. Use -m when you need to count characters in international text.

wc -l only counts \n newline characters. A file with content on a single line and no trailing newline returns 0, which surprises most people the first time. Use grep -c '' as a workaround that counts all lines regardless of a trailing newline:

bashgrep -c '' file.txt

wc in Pipelines and Getting a Clean Count

wc is commonly used to count the output of other commands:

bashfind . -type f | wc -l                  # count files in the current directorygrep "error" /var/log/syslog | wc -l   # count matching log linesgetent passwd | wc -l                  # count user accounts on the system

For counting matches inside a file, grep -c "pattern" file is a direct alternative to piping through wc. It avoids spawning a second process and runs slightly faster. Use the pipe form (grep | wc -l) when the input is coming from another command rather than a file.

Extract only the total from a multi-file count:

bashwc -w file1.txt file2.txt | tail -1

The last line of wc output for multiple files is always the grand total.

Use -l for line counts, -m when working with UTF-8 text, and redirect input (< file) when you need a clean number for a script or pipeline. Leave a comment below if you run into any issues.