How to Count Lines Words and Characters in Ubuntu
This article provides a quick overview of using the command line to analyze text files in Ubuntu. It covers the specific command needed to retrieve line, word, and character counts efficiently without requiring additional software installations.
To count these metrics, use the wc (word count) command
built into the Linux terminal. Open your terminal and navigate to the
directory containing your file. The basic syntax is
wc filename. Running this without options displays the
number of lines, words, and bytes in the file by default.
For specific counts, use the following flags:
- Count Lines: Use
wc -l filenameto display only the number of lines. - Count Words: Use
wc -w filenameto display only the number of words. - Count Bytes: Use
wc -c filenameto display the number of bytes. - Count Characters: Use
wc -m filenameto display the number of characters.
The -c flag counts bytes, while -m counts
characters. These differ when using multi-byte encodings like UTF-8. If
you need to analyze multiple files at once, list them after the command,
such as wc -l file1.txt file2.txt. The terminal will output
individual counts for each file followed by a total sum.