WC Command in Linux

FREE Online Courses: Elevate Skills, Zero Cost. Enroll Now!

In this article, we will be going through what wc is, a brief history, understanding its output, the syntax, and options of the wc command in linux. In the end, we will also be looking into some really cool examples of the wc command in action!

What is linux wc command?

Wc is an abbreviation for “Word Count”. It is a command-line-based utility in Linux-based operating systems that counts the number of characters, lines, and words in a file or data stream.

We might come across numerous scenarios where we might need to see the number of words, characters, or lines are there in a file. One good example is writing an article in a “.txt” file, and need to see the word count to not exceed the word count.

You can use the wc command with the options available to specify how it should function and format and print in the output. The wc command can accept zero or more input file names. If at all you don’t specify a file name, or when the file is “-”, the wc command will read the standard input (stdin).

If we simply run the wc command with no options, it will print the data in the following order: newline, word, character, byte, and maximum line length. We will look into the output of the wc command and understand it in the later sections.

History of linux wc command

The first appearance of the wc command was in the first version of UNIX. It was part of the X/Open portability Guide since the second issue of 1987. The wc command was later inherited into the first version of POSIX and the Single Unix Specification.

Wc command once use to be part of the GNU Textutils package, but now it is a part of the GNU Coreutils package. The wc command that comes in the GNU Coreutils pack was written, designed, and developed by Paul Rubin and David MacKenzie.

The wc command is also available in other operating systems other than Linux. For example, it is available in Microsoft Windows as a part of the GNUWin32 project and UnxUtils collection of the native Win32 port, it is also available in MSX-DOS 2 ias part of ASCII’s MSX-DOS 2 tools package.

Syntax of wc command in Linux

The syntax of the wc command is extremely simple:

wc <options> <file>

Let us take a closer look at the fields in the syntax of the wc command:

1. <options>

This field takes in a range of options that specify how the wc command should function and print the output.

2. <files>

This field takes in the name of the file you want to count the text. You can also specify the path of a specific directory or data stream in this field.

Options used with the wc command

The wc command, unlike most of the commands in Linux, has only a handful of options, to be more specific only 8 (out of which 2 are not considered as options). Let us take a look at them:

1. -c

This option prints the number of bytes. You can also write this option as “–bytes”.

2. -m

This option prints the number of characters. You can also write this option as “–chars”.

3. -l

This option prints the number of lines. You can also write this option as “–bytes”.

4. -w

This option prints the number of words. You can also write this option as “–words”.

5. -L

This line prints the length of the longest line. You can also write this option as “–max-line-length”.

6. –files0-from=<filename>

This option reads the input from the file specified by null-terminated names in the file specified by you.

7. –version

This option prints out the information about the wc command you are using in your system and then exits.

version of the wc command

8. –help

This option prints the help menu as shown below:

help menu of the wc command

Understanding output of Linux wc command

We can also run a wc command with no options at all, Here are a couple of examples (for both a file and a datastream):

understanding the output of the wc command

If you notice carefully, the wc command printed out 3 sets of numbers, let us look at what they represent.

1. The first number represents the lines.

2. The second number represents the number of words.

3. The third number represents the number of characters.

Therefore the data about the CPU information had 104 lines, 640 words, and 3696 characters, whereas the text file “cars2.txt” has 8 lines, 12 words, and 128 characters.

Now that we have laid down the fundamentals let us explore the possibilities of the wc command in the terminal. Let us consider the text file shown below to use the wc command on:

sample text file to test the wc command on

Counting the number of words

To count the number of lines, pair the wc command with the option “-w” or “–words” as shown in the output below:

wc -w <file>

counting the number of words

Counting the number of lines

To count the number of words, pair the wc command with the option “-l” or “–lines” as shown in the output below:

wc -l <file>

counting the number of lines

Counting the number of characters

To count the number of characters or letters, pair the wc command with the option “-c” or “–bytes” as shown in the output below:

wc -c <file>

counting the number of characters

Finding the length of the longest line

To print the number of characters in the longest line, use the wc command with the option “-L” as shown in the output below:

wc -L <file>

finding the length of the longest line

In the above output the line “XUV 500-mahindra” is the longest line which has 16 characters long.

Printing the number of lines and length of the longest line

You can also combine the options of the wc command, for example, If you want the wc command to display the total number of lines and the length of the longest line pair the options “l” and “L” as shown below:

wc -lL <file>

printing the number of lines and length of the longest line

Note that in the above example the first number represents the total number of lines and the second number represents the number of characters in the longest line. It will not be printed in any other format, as the wc command follows the following order and no one can change it: newline, word, character, byte, and maximum line length.

Printing the total number of words and characters

To print the total number of words along with the number of characters, pair the options “w” and “c” with the wc command as shown below:

printing the total number of words and characters

Again, if you notice, the number of words is printed first followed by the number of characters.

Displaying the count information of multiple files

You can count the number of lines, words, and characters of more than 1 file in just one wc command, to do so, simply write the filenames one after the other separated by a space as shown below:

wc <file1> <file2> <file3>

displaying the count information of multiple files

After printing the count information of all the files you specify, wc also prints the total at the end.

Displaying the count information of multiple data streams

To print the count information of multiple data streams, simply specify the path one after the other separated by a space as shown below:

displaying the count information of multiple data streams

Counting the number of files in a directory

Using the wc command we can also count the number of files in a directory. We simply pipe the ls command to the wc command and count the number of lines or words (words as names of files is only one word).

counting the number of files in a directory using the wc command

In the above output, the pipe symbol takes in the output of the ls command (which lists the contents of the directory) and passes it as the input of the wc command.

We can pipe the wc command with hundreds of Linux commands to count the information of a specific quantity, let us look at a few examples:

Counting the number of users

To printout the number of user accounts, pipe the “getent passwd” command with the wc command and count the number of lines as shown:

counting the number of users using the wc command

Counting the “–files0-from=<f>” option

The option “–files0-from=<filename>” allows the wc command to read the input from the files specified by NUL-terminated names in the file you specified. If the file you specified is “-” (as shown in the output below), the wc command will read names from the standard input (STDIN).

For example, you can search for files using the find command and provide those files to the wc command by piping them as shown below:

read the input from the files specified by nul terminated names in the file

The command in the above screenshot printed all the names of the directories in the “/etc” directory whose names start with “host”.

Using wc command without a file

Yup! We can also use the wc command without a file, we can simply echo a sentence and then pipes it with the wc command to get the count information, here is an example:

using wc command without a file

Summary

As you saw, even though the wc command is a really simple command, you can pipe it with different commands and produce desired results. You have now learned what wc is, a brief history of it, the syntax, and the options of the command. You have also learned the format and order of the output of the wc command including some beautiful examples of the wc command in action.

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *