FactorPad
Build a Better Process

Linux wc Command Summary with Examples

Linux wc allows you to count lines, words and bytes.
  1. Purpose - Learn what wc is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with wc.
  4. A tip - Finish off with one more insight.
face pic by Paul Alan Davis, CFA
Updated: February 23, 2021
In this tutorial on Linux wc, 26 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / wc-command.html


An ad-free and cookie-free website.


Examples of the Linux wc Command

Beginner

Learn to count words, lines or bytes in a file at the Linux command line.

Video Tutorial

Videos can also be accessed from the Linux Essentials Playlist on YouTube.

Linux wc Command Summary with Examples (4:09)

Video Script

The Command and Why You Need It

Our twenty-sixth word, or command to memorize is wc from our category Text.

wc allows you to count lines, words and bytes.

Common Linux wc Options
-options description
-l, --lines Count lines
-w, --words Count words
-c,--bytes Count bytes

Recall from the last video we spent time on navigation topics and now we'll shift back to text for a few videos.

Before we start, it helps to think about commands as mini programs and most follow this structure: command -option(s) argument(s).

The wc command has 8 options and arguments are the files on which you'd like to perform the count.

Like most commands, help is available with double-dash --help.

Without options wc prints all three : lines, then words, then bytes. To report only one count, use -l for lines, -w for words and make note, it's -c and not dash-b, for bytes.

So why is wc an important command? Well, wc is helpful for monitoring changes to files. And now you know how to do that.

Demonstration

Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go, let's start with the broadest view using the wc * which shows counts for all of our files. Lines, words and bytes.

$ wc * wc: stuff: Is a directory 0 0 0 stuff 7 38 176 video05.txt 4 18 100 video11.txt 0 0 0 video16.txt 1 1 45 video19.txt 8 33 149 video20.txt 1 3 14 video21.txt 6 17 86 video23.txt 6 17 86 video24.txt 33 127 656 total

Second, let's create a file to process by grabbing the help file wc --help and output it > to a text file called video26.txt.

$ wc --help > video26.txt $ ls stuff video11.txt video19.txt video21.txt video24.txt video05.txt video16.txt video20.txt video23.txt video26.txt

Now using wc -l and count 22 lines.

$ wc -l video26.txt 22 video26.txt

Let's open with less from video #5, using the -N option.

$ less -N video26.txt
1 Usage: wc [OPTION]... [FILE]... 2 or: wc [OPTION]... --files0-from=F 3 Print newline, word, and byte counts for each FILE, and a total line if 4 more than one FILE is specified. With no FILE, or when FILE is -, 5 read standard input. A word is a non-zero-length sequence of characters 6 delimited by white space. 7 The options below may be used to select which counts are printed, always in 8 the following order: newline, word, character, byte, maximum line length. 9 -c, --bytes print the byte counts 10 -m, --chars print the character counts 11 -l, --lines print the newline counts 12 --files0-from=F read input from the files specified by 13 NUL-terminated names in file F; 14 If F is - then read names from standard input 15 -L, --max-line-length print the length of the longest line 16 -w, --words print the word counts 17 --help display this help and exit 18 --version output version information and exit 19 20 GNU coreutils online help: <http://www.gnu.org/software/coreutils/> 21 Full documentation at: <http://www.gnu.org/software/coreutils/wc> 22 or available locally via: info '(coreutils) wc invocation' video26.txt (END)

And look, the syntax, eight options and 22 lines.

Third, hit q to leave less. For words wc -w on video26.txt 160.

$ clear $ wc -w video26.txt 160 video26.txt

And last, to reinforce two things, byte count is dash -c, not dash-b. Also recall that with double-dash, as long as Linux identifies what you mean, it'll work. So --by counts, pretty neat huh?

$ wc --by video26.txt 1203 video26.txt

A Final Tip

Okay now you know how to use wc. And you know the syntax for commands, options and arguments.

One last tip about the wc command. It really comes in handy to count programs or changes to the number of lines in a log file.

Okay, thanks for visiting today. I hope this was a helpful introduction to the wc command.


Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:


What's Next?

Follow our YouTube Channel and @factorpad Twitter handle for updates on new opportunites to learn.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / wc-command.html


linux wc command
wc linux
linux wc
wc command
linux word count
linux count files
linux count lines
linux count bytes
linux count words
wc command syntax
linux wc summary
linux wc examples
linux wc options
linux wc tutorial
wc unix
line counter
count files in directory
man wc

A newly-updated free resource. Connect and refer a friend today.