wc - Word Count
Summary
The wc command in Linux is a utility that displays the number of lines, words, and bytes (or characters) in a file. It's a simple yet powerful tool for quick text analysis.
Introduction
The wc command (short for word count) is a standard command-line utility in Unix-like operating systems used to count the number of lines, words, characters, and bytes in a file or input stream. It is commonly used for basic text analysis and for verifying file sizes. By default, wc prints the number of lines, words, and bytes, followed by the filename. You can customize its behavior by using various flags to display only the desired counts.
Use case and Examples
Counting lines, words, and bytes in a file
This will output the number of lines, words, and bytes in the file namedmyfile.txt. For example, 10 100 800 myfile.txt indicates that myfile.txt has 10 lines, 100 words, and 800 bytes. Counting lines, words, and bytes from standard input
This pipes the content ofmyfile.txt to the wc command, which then counts the lines, words, and bytes from the standard input. Counting only the number of lines in a file
This will output only the number of lines in the filemyfile.txt. Counting only the number of words in a file
This will output only the number of words in the filemyfile.txt. Counting only the number of bytes in a file
This will output only the number of bytes in the filemyfile.txt. Counting the number of characters in a file
This will output only the number of characters in the filemyfile.txt. This is different from -c for UTF-8 characters Counting the longest line length in a file
This will output the length of the longest line in the filemyfile.txt. Using wc with multiple files
This will output the statistics for each file individually and then provide a total count at the end.Commonly used flags
| Flag | Description | Example |
|---|---|---|
-l | Counts the number of lines. | wc -l myfile.txt |
-w | Counts the number of words. | wc -w myfile.txt |
-c | Counts the number of bytes. | wc -c myfile.txt |
-m | Counts the number of characters. | wc -m myfile.txt |
-L | Prints the length of the longest line. | wc -L myfile.txt |