Counting lines words or characters with Linux wc command

Counting lines words or characters with Linux wc command

Linux provides many commands, that can be used for many different purposes and situations, to either obtain information quickly or to solve any problem with a script.

One of them serves for counting lines, words or characters in a file and it is very frequently used. It is named as wc and in this post, we will review some use cases where this command could be useful for.

Executing wc to obtain lines, words and characters from a file

If you use the wc command without adding any option followed by the filename or file path, it will show the file’s lines, words and characters in the following format: <number of lines> <number of words> <number of characters> <file name>.

Let’s see the following example with a file named “file_count”:

$ wc file_count
wc execution to get the line, word and character count

As you can appreciate, there’s 1 line, 2 words but you might wonder why there are 11 characters. The reason is because there is a new line character at the end of the line, which also increments the count. Just to clarify if the result confuses you.

In addition, it is possible to run wc with multiple files as arguments to obtain similar output format described above for each file:

$ cat file_count hello_world
running wc for multiple files

The difference is that the wc will add a summary result line with the sum of each counts done on the files, followed with the word “total” instead of a file name or path.

Conveniently, you can use the wildcard to count all the files present in the current folder without the need of specifying them one by one:

$ wc *
you can run wc with wildcard instead of specifying all the files one by one on the folder.

Count the lines, words or characters separately from a file

If you just need one of the counts, you may choose to run wc with the different options available.

To count the lines on a file only, the -l option shall be used. For word count only, use -w. And for character count, the -c:

$ wc -l file_count
$ wc -w file_count
$ wc -c file_count
Running wc to obtain separately line, word and character count

Any command line from above, can be used with wildcard so, if we want to count the words only from all the files in the current folder:

$ wc -w *
wc -w with wildcard example

Pipe the output to the wc command

The wc output changes slightly if you use pipe to fetch the command input like:

$ cat file_count | wc
$ cat hello_world | wc -w
using pipe to fetch the wc command input

This could be more convenient so we don’t need to process the output to get only the number and instead, we can assign it in a variable and use it straightly in a script:

$ COUNT=$(cat file_count hello_world | wc -c)
$ echo $COUNT
$ if [ $COUNT -gt 12 ]; then echo "count is greater than 12"; fi
$ if [ "$COUNT" == "24" ]; then echo "count is equal to 24"; fi
Saving the results of wc in environment variable to handle it in a script later.