Skip to content

wc Command: Word, Line, and Byte Counting

Summary

The wc command in Linux is a versatile utility used for counting the number of lines, words, characters, and bytes within a file or input stream. It's a fundamental tool for text analysis and data manipulation.

Introduction

The wc (word count) command is a standard command-line utility in Linux and other Unix-like operating systems. Its primary function is to count the number of lines, words, characters, and bytes in a file or input received through standard input. The wc command provides a quick and easy way to gather statistical information about text files, making it useful for scripting, data processing, and general system administration tasks.

Use case and Examples

Counting Lines, Words, and Bytes in a File

wc myfile.txt
This command will output the number of lines, words, and bytes in myfile.txt, followed by the filename. For example, if myfile.txt contains "This is a test.\nAnother line.", the output might be 2 6 27 myfile.txt.

Counting Only Lines

wc -l myfile.txt
This command counts only the number of lines in myfile.txt. The output will be a single number representing the line count, followed by the filename.

Counting Only Words

wc -w myfile.txt
This command counts only the number of words in myfile.txt.

Counting Only Characters

wc -m myfile.txt
This counts the number of characters in myfile.txt. This is useful for UTF-8 encoded files where bytes and characters might differ.

Counting Only Bytes

wc -c myfile.txt
This command counts only the number of bytes in myfile.txt.

Using wc with Pipes

cat myfile.txt | wc -l
This example uses cat to send the contents of myfile.txt to wc via a pipe. wc then counts the number of lines and outputs the result. This is particularly useful for processing the output of other commands.

Counting from Standard Input

wc
This is a test.
^D
Typing wc without a filename causes it to read from standard input. You can type text, and when you press Ctrl+D (end-of-file), wc will display the line, word, and byte counts for the text you entered.

Counting Files in a Directory

ls | wc -l
This command lists all files and directories in the current directory using ls, and then pipes the output to wc -l to count the number of lines (which corresponds to the number of files and directories).

Commonly used flags

Flag Description Example
-c Counts the number of bytes. wc -c myfile.txt
-m Counts the number of characters. Useful for UTF-8 encoded files. wc -m myfile.txt
-l Counts the number of lines. wc -l myfile.txt
-w Counts the number of words. wc -w myfile.txt
-L Prints only the length of the longest line. wc -L myfile.txt


Share on Share on

Comments