Hello there! In this comprehensive guide, we are going to take a deep dive into the venerable wc utility that is built into Linux and most Unix-like operating systems. By the end, you will have mastered the ins and outs of this useful little tool for counting words, lines, bytes, and more.
Let‘s get started!
A Quick Intro to the wc Command
For those who may not be familiar, wc stands for "word count". As the name implies, it can count the number of words in a file.
But wc is actually much more versatile than just counting words! Here is an overview of what it can do:
- Count the number of lines in a file
- Count the number of words
- Count the number of bytes
- Count the number of characters
At a high level, wc accepts input files, analyzes them, and prints out counts for lines, words, bytes and characters.
The basic syntax is simple:
wc [options] [files]
Let‘s walk through some examples to see wc in action.
wc Usage By Example
Here I have a file called article.txt that contains a short text article:
wc article.txt
This prints out:
13 47 186 article.txt
By default, wc displays:
- Lines
- Words
- Bytes
- Filename
Let‘s see how we can isolate each metric using the various wc options:
Count Lines Only -l
To show only the line count, use -l:
wc -l article.txt
This displays:
13 article.txt
The line count includes empty lines too.
Count Words Only -w
To show only the word count, use -w:
wc -w article.txt
Just the word count is displayed:
47 article.txt
Count Bytes Only -c
To see only the byte count, use -c:
wc -c article.txt
This prints the bytes:
186 article.txt
Count Characters Only -m
To count only characters, use -m:
wc -m article.txt
This shows the character count:
149 article.txt
So in summary, the options allow you to customize the output to just the counts you need!
Counting Multiple Files
One of the most useful features of wc is that it can quickly process multiple files in one command.
For example:
wc file1.txt file2.txt
This will display the line, word and byte counts for each file on separate rows, followed by a total:
12 34 209 file1.txt
4 11 105 file2.txt
16 45 314 total
This makes comparing stats across different files a breeze!
The History and Origins of wc
The wc utility has been around for a very long time. Early versions of wc first appeared in AT&T Unix in the late 1970s.
At the time, storage space was very expensive. Being able to count disk usage in bytes was important for managing costly resources.
In 1978, wc was included in Version 7 of Unix from Bell Labs. This version added the ability to count lines and words as well.
Over the years, wc became a standard utility that was included in all Unix-like operating systems. Today it ships with Linux distros, BSD, macOS, and more.
While disk space is cheap now, wc remains a handy tool for tasks like counting code lines, analyzing text files, and generating statistics. Its simplicity and flexibility have kept it relevant even after 40+ years!
Counting Lines of Code
One common use case for wc is counting lines of code in programs.
For example, to count lines in a Python script called script.py, you could do:
wc -l script.py
This prints out the number of lines:
342 script.py
Very handy for quantifying the size of code projects!
You can also combine wc with globs to count multiple files. Say you have a directory of Python modules:
wc -l *.py
This will count lines across all .py files, giving total counts for each file and overall lines.
Some other examples:
- Count lines in Java files with
wc -l *.java - Count lines across PHP files with
wc -l **/*.php - Count lines in all Rust files with
wc -l $(find . -name ‘*.rs‘)
As you can see, wc -l is immensely helpful for tallying up code size.
Comparing File Sizes
Another common use case is using wc to compare sizes of different files.
The byte count from wc -c provides a quick way to see which files consume more disk space.
For example, let‘s look at some log files in /var/log:
wc -c /var/log/*
This might show:
84592 /var/log/dmesg.log
584902 /var/log/syslog
8134 /var/log/mail.log
You can instantly see that syslog is the largest log file currently, followed by dmesg.log, and mail.log is the smallest.
This works for any collection of files – you could compare sizes of BMP images, JSON configs, backup archives, etc.
So if you ever need a quick size comparison across files, wc -c is your friend!
Word Counts for Readability Analysis
Another very common use for wc is generating statistics on documents for analysis and readability scoring.
For example, educators may want to know the word count of papers to ensure students meet length requirements. Writers may want to keep blog posts or articles under a certain word limit.
Counting words with wc -w provides useful metrics for these cases.
Let‘s say you had a research paper saved in paper.txt:
wc -w paper.txt
This might output:
1852 paper.txt
Analyzing the word count can help the writer gauge if the paper meets expectations or if more content needs to be added.
You can also use wc with pipes and grep to filter out certain words:
cat paper.txt | grep -v References | wc -w
This will exclude words from the References section to get a more accurate count.
For researchers and writers, wc is indispensable for understanding document length and readability.
Using wc in Scripts and Pipelines
One of the great things about command line utilities like wc is that they can be easily combined together for powerful effect.
For example, let‘s say you wanted to find the longest line in a file. You could pipe wc together with sort and tail like so:
cat file.txt | wc -L | sort -nr | head -n 1
This prints the length of the longest line.
Here is how it works:
cat file.txtprints the contents of the filewc -Lcounts the length of each linesort -nrsorts the output numerically in reverse (highest first)head -n 1shows just the first (longest) line length
Similarly, you can use wc in shell scripts to perform analysis. For example, this simple script calculates total lines across a codebase:
#!/bin/bash
TOTAL_LINES=0
for file in *.py
do
LINES=$(wc -l $file | awk ‘{print $1}‘)
TOTAL_LINES=$((TOTAL_LINES + LINES))
done
echo "Total lines of Python code: $TOTAL_LINES"
The possibilities are endless! wc is one tool that every script writer should have in their toolbox.
wc Options for Advanced Users
While the basic wc usage is pretty simple, there are some more advanced options and features that power users can take advantage of:
Process files in parallel with -p
This uses multiple cores to speed up counting on large files:
wc -p file1.txt file2.txt
Specify custom line length with -L
Sets the max line length instead of detecting automatically:
wc -L 80 file.txt
Null deliminated filenames -0
Handle weird filenames and newlines:
find . -print0 | wc -l0
Format output with --format
Customize the output format with C-style specifiers:
wc --format="%l lines" file.txt
There are many more handy options – review the man pages with man wc to learn more!
wc vs Alternative Tools
While wc is great at counting, there are some overlap and alternatives in terms of functionality:
cat– Print number of lines withcat file | wc -lgrep– Count lines/words for a regex matchawk– More advanced line/word counting capabilitiesgit count-objects– Count packed Git objectsdu– Disk usage summaryls -l– View file sizes on disk
In many cases, wc is just one tool that can achieve a specific counting objective. But it really shines for simplicity and ease of use in counting lines, words, and bytes.
Installing and Running wc on Linux
One of the nice things about wc is that it comes pre-installed on pretty much every Linux distribution by default. So there is no need to install anything – it‘s ready to go right out of the box!
Here are some examples of where wc is available:
- Ubuntu – located in
/usr/bin/wc - CentOS – located in
/usr/bin/wc - Debian – located in
/usr/bin/wc - Fedora – located in
/usr/bin/wc - Arch Linux – located in
/usr/bin/wc
Because it has been around since the early days of Unix, you‘ll find wc virtually everywhere.
The standard GNU wc should work across all Linux environments and POSIX systems. Some lightweight utils like BusyBox may include a simpler version of wc with fewer options.
But the full-featured wc will offer maximum compatibility and consistency in behavior across Linux and Unix-likes.
Common wc Questions and Answers
Here are some frequently asked questions about using wc:
How does wc differ between Linux/Unix environments?
The GNU implementation of wc is the standard that will work consistently across most POSIX systems like Linux, BSD, macOS, etc. Some lightweight versions like BusyBox wc may have subtle differences in behavior.
What‘s the difference between bytes and characters in wc?
For text files, bytes and characters are often the same. But bytes include all data, while characters only count human readable letters/symbols in encoding. For binary files, bytes would differ substantially from characters.
Can I influence the memory usage of wc?
Yes, by ordering files from largest to smallest. wc allocates memory based on the size of the first file. So put big files first.
How do I handle counting weird filenames with newlines?
Use the -0 option along with find -print0 to properly handle newlines in filenames.
Can I count words matching a specific pattern?
Yes, you can pipe the output of wc into grep or awk to filter by a word pattern and get counts.
What‘s the fastest way to count lines across many files?
Use the -p parallel option, ordering from big files to small files:
wc -lp bigfile.txt smallfile.txt
This utilizes multiple cores and efficient memory allocation.
Final Thoughts
While it may seem like a simple utility, wc is one of those evergreen Linux/Unix tools that has stood the test of time. Its flexibility in counting lines, words, bytes and characters makes it useful in a wide range of situations.
Whether you are analyzing text files, counting code, comparing sizes or generating metrics, wc has you covered. It shines for both interactive use and scripting.
So next time you need to count something on the command line, remember the trusty wc command. I hope this guide has provided a comprehensive overview of how to use wc and given you some ideas for how to employ it in your daily work!
Let me know if you have any other wc tips or favorite use cases. Happy counting!



