Fundamentals 4 min read

Master the Linux ‘wc’ Command: Count Lines, Words, Bytes, and Characters

This guide explains how to use the Unix/Linux wc command to count lines, words, characters, and bytes in files, detailing each option with clear examples and showing how to display the longest line length.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master the Linux ‘wc’ Command: Count Lines, Words, Bytes, and Characters

wc command syntax

The wc (word count) utility in Unix/Linux reports the number of lines, words, bytes, and characters in one or more files.

# wc [options] filename

Common options

wc -l

– prints the line count of each file. wc -w – prints the word count of each file. wc -c – prints the byte count of each file. wc -m – prints the character count of each file. wc -L – prints the length of the longest line (in characters).

Example file

Assume a file named devops.txt containing a list of Linux distributions:

$ cat devops.txt

Red Hat
CentOS
AlmaLinux
Rocky Linux
Fedora
Debian
Scientific Linux
OpenSuse
Ubuntu
Xubuntu
Linux Mint
Deepin Linux
Slackware
Mandriva

1. Basic usage (no options)

Running wc devops.txt returns three numbers: lines, words, and bytes.

$ wc devops.txt

12  16 112 devops.txt

2. Count lines

Use -l to display only the line count.

$ wc -l devops.txt

12 devops.txt

3. Count words

Use -w to display only the word count.

$ wc -w devops.txt

16 devops.txt

4. Count characters

Use -m to display the total number of characters.

$ wc -m devops.txt

112 devops.txt

5. Count bytes

Use -c to display the total number of bytes (identical to characters for plain ASCII text).

$ wc -c devops.txt

112 devops.txt

6. Longest line length

Use -L to show the length of the longest line in characters. In the example file, the longest line is "Scientific Linux" (16 characters).

$ wc -L devops.txt

16 devops.txt
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.

LinuxUnixcommand-linewctext-processing
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.