How to Use the wc Command in Linux (With Examples)


The wc command in Linux is a handy utility for counting the number of lines, words, and characters in a text file. This command can be especially helpful when working with large files or when combined with other Linux commands. In this tutorial, we will take a look at how to use the wc command, including some examples of common usage scenarios.

Syntax of wc command

The syntax of wc command is:

$ wc [OPTION]... [FILE]...

Basic usage of wc command

Some of the basic usage of the wc command is as follows:

Display the line, word, and byte count

By default, the wc command without any command line options displays the count of lines, words, and bytes for each file (or files) or standard input.

$ wc <filename>

Let’s say to display the lines, word, and byte count for a file test.txt, the command would be:

$ wc test.txt

The first entry in the output shows the number of lines, the second entry shows the number of words, and the third entry shows the number of bytes in the specified file.

Display count for multiple files

To display the count for multiple files, use the wc command like this:

$ wc <filename1> <filename2> <filename3>

This command will print the statistics for each file and a total count at the end of the output.

Other than the combined output, you can also separately display the lines, words, and byte count in a file.

Count number of lines in a file

To display the line count in a file, use the wc command with the -l flag as follows:

$ wc -l <filename>

Count number of words in a file

To display the word count in a file, use the wc command with the -w flag as follows:

$ wc -w <filename>

Count number of bytes in a file

To display the byte count in a file, use the wc command with the -c flag as follows:

$ wc -c <filename>

Count number of characters in a file

To display the character count in a file, use the wc command with the -m flag as follows:

$ wc -m <filename>

Moreover, you can combine two flags as well. To display word and character count in a file, the command would be:

$ wc -w -m <filename>

Find the length of the longest line in a file

Using the wc command, you can also find the length of the longest line in a file, however, it doesn’t show which line it is. It just displays the number of characters in the longest line.

$ wc -L <filename>

How to use the wc command with other Linux commands

Let’s see how to use the wc command with other Linux commands to perform certain functions.

1. Count the number of files in a directory

To count the number of files in a directory, use the following command:

$ ls -1 <directory_name> | wc -l

To count the number of files in the ~/Documents directory, you would use the following command:

$ ls -1 ~/Documents | wc -l

To include the hidden files too in the count, use the following command:

$ ls -1A ~/Documents | wc -l

2. Count the occurrence of a certain word in a file

You can also count how many times a specific word appears in a file using the wc command with grep as follows:

$ grep -o "<word>" <filename> | wc -w

To count the occurrence of a word jan in the file named dates.txt, the command would be:

$ grep -o "jan" dates.txt | wc -w

3. Count Unique lines in a file

To count unique lines in a file, use the wc command with the sort and uniq commands:

$ sort <filename> | uniq | wc -l

To count the number of unique lines in the file dates.txt, the command would be:

$ sort dates.txt | uniq | wc -l

Conclusion

That is all there is to it! In this article, we covered some examples of the wc command and how to use it with other Linux commands to get the information we need from our files. If you know of some other use cases of the wc command, we would love to know in the comments.

Print Friendly, PDF & Email
Categories
Tags