Fundamentals 9 min read

Master Linux wc: Count Lines, Words, Bytes and More with Practical Examples

This guide explains the Linux wc command, covering its purpose, syntax, common options, and how to combine it with other tools, while providing step‑by‑step examples, sample output, and tips for counting lines, words, characters, bytes, and longest lines in files or streams.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux wc: Count Lines, Words, Bytes and More with Practical Examples

What is the wc command?

The wc utility (short for "word count") is a built‑in Unix/Linux tool that reports the number of lines, words, characters, and bytes in a file or standard input.

wc command syntax

wc [OPTION] [FILE]

Run man wc to view the full manual and list all available options.

How to use wc

First, create a sample file named linuxmi.txt and paste the following text (the Python Zen):

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.[a]
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than right now.[b]
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea – let's do more of those!

Default wc output

linuxmi@linuxmi:~/www.linuxmi.com$ wc linuxmi.txt
19 137 830 linuxmi.txt

The four columns represent lines, words, bytes, and the file name.

Print only the number of lines

linuxmi@linuxmi:~/www.linuxmi.com$ wc -l linuxmi.txt
19 linuxmi.txt

Print only the number of words

linuxmi@linuxmi:~/www.linuxmi.com$ wc -w linuxmi.txt
137 linuxmi.txt

Print only the number of bytes

linuxmi@linuxmi:~/www.linuxmi.com$ wc -c linuxmi.txt
830 linuxmi.txt

Print only the number of characters

linuxmi@linuxmi:~/www.linuxmi.com$ wc -m linuxmi.txt
824 linuxmi.txt

Print the length of the longest line

linuxmi@linuxmi:~/www.linuxmi.com$ wc -L linuxmi.txt
70 linuxmi.txt

Using wc with multiple files

Create two additional files, zimu.txt (alphabet list) and shuzi.txt (numbers 1‑10), or use any two text files.

linuxmi@linuxmi:~/www.linuxmi.com$ wc linuxmi.txt zimu.txt shuzi.txt

The first three lines show counts for each file; the final line shows the totals.

Combining wc with other Linux commands

Count files or directories in a directory

linuxmi@linuxmi:~/www.linuxmi.com$ ls www.linuxmi.com | wc -l

Count the number of running processes

linuxmi@linuxmi:~/www.linuxmi.com$ ps | wc -l

Explore more Linux commands

Linux offers a rich set of command‑line utilities; learning how to combine them, as shown with wc, can greatly improve productivity and scripting capabilities.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxUnixtext processingwc command
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.