Linux Files Operation: wc (Word Count) Usage Examples
The wc
(word count) command in Linux is a simple powerful tool for counting words, lines, characters, and bytes in files or input streams. Whether you’re analyzing text files, processing logs, or working with data, wc
offers valuable insights into the size and structure of your files.
In this article, we’ll explore several practical examples of how to use wc
to gather word count statistics. We’ll also discuss its various options to customize the output for specific needs, making it an indispensable command for both everyday tasks and advanced text processing.
Using the wc
Command in Linux
The wc command in Ubuntu stands for “word count”. It is a simple command-line utility used to display the number of lines, words, and bytes (or characters) in files. The basic syntax of the wc command is:
wc [options] [file...]
Here are the common options you can use with the wc command:
- wc without any options: Displays the number of lines, words, and bytes in each specified file, and a total line if more than one file is specified.
wc file.txt
- -l: Prints the number of lines in the file.
wc -l file.txt
- -w: Prints the number of words in the file.
wc -w file.txt
- -c: Prints the number of bytes in the file.
wc -c file.txt
- -m: Prints the number of characters in the file. This option takes into account multi-byte characters, whereas -c counts bytes.
wc -m file.txt
- -L: Prints the length of the longest line in the file.
wc -L file.txt
Word Count Command Examples
- Count only lines in a single file:
wc -l file.txt
- Count lines, words, and bytes in multiple files:
wc file1.txt file2.txt
Combining Options with wc
Command
You can combine different options to get specific counts in the same command:
# This command will output the number of lines and words in file.txt
wc -lw file.txt
Practical Use Cases
- Count lines in log files:
wc -l /var/log/syslog
- Count the number of files in a directory:
ls -1 | wc -l
- Count words in a text file:
wc -w document.txt

Linux Files Operation: wc (Word Count) Usage Examples