Introduction
In the Linux environment, one of the simplest yet powerful commands for working with text files is wc. Its name comes from ‘word count’, and although its main function is to count words, it also allows obtaining the number of lines, characters, and bytes of a data stream. This article explains in detail how to use wc, its most common options, and some practical examples that can save you time in system administration and log processing.
Basic Syntax
The simplest way to invoke wc is:
wc [options] [file]
If no file is specified, the command reads from standard input, making it ideal for use in pipelines (pipes). The options modify what type of count is displayed; by default, without options, wc shows three columns: lines, words, and bytes, in that order.
Main Options
- -l: shows only the number of lines.
- -w: shows only the number of words.
- -c: shows the number of bytes.
- -m: shows the number of characters (useful when the file contains multibyte characters, such as UTF‑8).
- -L: prints the length of the longest line.
It is possible to combine several options; for example, wc -lw will show lines and words in the same order as they appear in the list of options.
Practical Examples
Suppose you have a file named registro.log. To know how many lines it contains, simply:
wc -l registro.log
If you want to know the number of words and characters, you can run:
wc -w -m registro.log
Or, more compactly:
wc -wm registro.log
When no file is indicated, wc reads from standard input.
This post is also available in ESPAÑOL.