Introduction
In the Linux environment, one of the simplest but most powerful commands to work with text files iswc. Its name comes from word count and, although its main function is to count words, it also allows you to get the number of lines, characters and bytes of a data flow. This article explains in detail how to usewc, your most common options and some practical examples that can save you time in system management and log processing.
Basic syntax
The simplest way to invokewcis:
wc [options] [file]
If no file is specified, the command reads from the standard input, which makes it ideal to use in pipes. The options change what type of count is shown; by default, without options,wcshows 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: print the length of the longest line.
It is possible to combine several options, for example,wc -lwshow lines and words in the same order in which they appear in the list of options.
Practical examples
Suppose you have a file calledregistro.log. To know how many lines it contains, it is enough to:
wc -l registro.log
If you want to know the number of words and characters, you can run:
wc -w -m registro.log
Or, in a more compact way:
wc -wm registro.log
When no file is indicated,wcRead from the standard input. For example, to count the lines of the output ofls -la:
- | wc -l
Or to tell how many words there are in the confirmation message of a Git commission:
git show --name-only | wc -w
Combination with other commands
The true power ofwcis revealed when used in combination with other commands by piping. Some common scenarios include:
- See how many files are in a directory:
ls | wc -l - Determine the total line size of all files
.txtfrom a directory:find . -name '*.txt' -exec cat {} \; | wc -l - Get the number of characters from a chain that is generated with
echo:echo 'Hola mundo' | wc -m - Auditing web logs: count how many GET requests there are in an access file:
grep 'GET' access.log | wc -l
These combinations allow fast metrics to be obtained without the need to write complex scripts.
Tricks and variants
Althoughwcis simple, there are some tricks that can be useful:
- To prevent the file name from appearing on the output (useful when processing multiple files), use the option-files0-fromwithfind -print0or simply redirect the output to
cutto remove the second column. - If you need to count only visible characters (ignoring spaces and line leaps), you can combine
tr -d '[:space:]'beforewc -c - In systems where the locale affects the multibyte character count, be sure to use
-mfor-cto obtain a precise Unicode character count. - Some versions of
wcallow the option-Totalwhich shows an additional row with the sum of all processed files.
Knowing these details helps you to adapt the command to specific situations and to correctly interpret the results.
Conclusion
The commandwcis an essential tool for any Linux user who needs to get fast metrics over text files. Its simplicity hides a great versatility: from counting lines in a configuration file to getting log statistics using pipes. Dominating your options and learning to combine it with other commands will allow you to perform data management and analysis tasks efficiently and without writing additional code.


