FactorPad
Faster Learning Tutorials

Linux wc command summary with examples

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.
Paul Alan Davis, CFA, October 15, 2016
Updated: July 30, 2018
In this tutorial, 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 Next

~/ home  / tech  / linux essentials  / wc command


The Linux wc command

Beginner

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

Video Tutorial

Linux wc command summary with examples (4:09)

Videos can also be accessed from the YouTube Playlist.

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.

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?

Click to Subscribe to our growing YouTube Channel.

  • For the Outline to all 100 tutorials, click Outline.
  • To go back to the rm command, hit Back.
  • To learn about the helpful head command, click Next.

Outline Back Next

~/ home  / tech  / linux essentials  / wc command



 
 
Keywords:
linux wc command
linux word count
wc linux
linux count lines
linux count bytes
linux count log files
man wc
linux count programs
linux measure changes to files
linux compare files
linux line count
wc command syntax
linux wc summary
linux wc options
linux wc tutorial
wc unix
line counter
count files in directory