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.
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] filenameCommon 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
Mandriva1. Basic usage (no options)
Running wc devops.txt returns three numbers: lines, words, and bytes.
$ wc devops.txt
12 16 112 devops.txt2. Count lines
Use -l to display only the line count.
$ wc -l devops.txt
12 devops.txt3. Count words
Use -w to display only the word count.
$ wc -w devops.txt
16 devops.txt4. Count characters
Use -m to display the total number of characters.
$ wc -m devops.txt
112 devops.txt5. Count bytes
Use -c to display the total number of bytes (identical to characters for plain ASCII text).
$ wc -c devops.txt
112 devops.txt6. 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.txtSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
