The wc command on Linux is short for word count. It’s a simple tool that does exactly as its name would imply: count the number of words in a file. It can also count similar things, like the number of characters in a file, the number of lines, and the number of bytes.
Regardless of your command line skill, the wc command can be used to quickly determine how lengthy a file is, or see if it adheres to some kind of length requirement. It’s common to utilize wc in a Bash script, for example when the length of a document needs to be determined in order to decide what action to take next. Despite its simplicity, it comes in handy a lot more than you might think, believe it or not.
In this guide, we’ll go over several examples for the wc command on Linux. By the end of this tutorial, you’ll know exactly how to use the command and everything it’s capable of.
In this tutorial you will learn to:
- Use the wc command in Linux
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
|Conventions||# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Frequently Used Options
The most common usage of the wc command in Linux is to print the number of lines, words, and bytes in one or more input files – and always in that order. Take a look at some of the examples below to see how it works.
- Run the wc command by passing the name of a file to it.
$ wc example.txt 4 18 76 example.txt
The output we received in this example indicates that the file contains 4 new lines, 18 words, and 76 bytes.
- You can also use wc on multiple files simultaneously. It will show the counts for each file, as well as a sum of the total counts.
$ wc file* welcome.txt example.txt 6 6 31 file 2 10 54 file3 3 26 149 welcome.txt 4 18 76 example.txt 15 60 310 total
- Of course, wc can also accept input from other commands. Here’s an example where we aren’t using a file at all. Instead, we are rather counting the lines, words, and bytes generated from an
$ echo "How many words are in this sentence?" | wc 1 7 37
- Use the
--linesoption if you only want to determine the number of lines in a file.
$ wc -l example.txt 4 example.txt
- As you might have expected the
--wordsoption can be used if you only want to determine the number of words in a file.
$ wc -w example.txt 18 example.txt
- Use the
--bytesoption if you only want to determine the number of bytes in a file.
$ wc -c example.txt 76 example.txt
- Use the
--charsoption if you want to count the number of characters in a file. In normal text files, this will usually be the same number as the bytes (one character equals one byte).
$ wc -m example.txt 76 example.txt
You can always use the man command to read more about the wc command in Linux and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.
The usage of the wc command is intrinsically simple and doesn’t include any features that one might consider “advanced.” However, like many other Linux commands, its true power and usefulness comes when it’s combined with other commands.
Here’s a really simple way to determine the number of files in a directory by using the ls command and wc command in combination.
$ ls -l /home/linuxconfig | wc -l 14
ls -l will list one file per line, and
wc -l counts the number of lines, it’s safe to say that our home directory contains 14 files.
There are dozens of other similar applications for such a command, but sometimes you have to think a little outside the box to incorporate it. As you master the Linux command line, you’ll notice a lot of ways to work the wc command into your normal repertoire.
In this guide, we learned all about the wc command in Linux. Although it’s far from complex, wc command is an essential command for many situations, and can be easily strung together with other commands to extend their functionality and usefulness.