Skip to content

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

wc myfile.txt
This will output the number of lines, words, and bytes in the file named myfile.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

cat myfile.txt | wc
This pipes the content of myfile.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

wc -l myfile.txt
This will output only the number of lines in the file myfile.txt.

Counting only the number of words in a file

wc -w myfile.txt
This will output only the number of words in the file myfile.txt.

Counting only the number of bytes in a file

wc -c myfile.txt
This will output only the number of bytes in the file myfile.txt.

Counting the number of characters in a file

wc -m myfile.txt
This will output only the number of characters in the file myfile.txt. This is different from -c for UTF-8 characters

Counting the longest line length in a file

wc -L myfile.txt
This will output the length of the longest line in the file myfile.txt.

Using wc with multiple files

wc file1.txt file2.txt file3.txt
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


Share on Share on

Comments