wc Command in Linux | Explained

Several text editors are used for creating & editing files that do not provide word count or information for a file. In Linux or UNIX-based distributions, we can easily find the words, characters, and lines count for the files with the wc command.

The wc, short for word count, is a command-line utility; as the name suggests, its main purpose is to count the words in a file or several files. The wc command helps us to count the words, characters, new lines, maximum line length, and bytes in files. It helps us manage the documents’ length and size (number of bytes). The post covers the following topics about the wc command:

Let’s get into the uses for wc commands.

How Does the wc Command Work in Linux?

This section will use the wc command for different examples to understand this command. The wc command has a simple syntax provided below:

$ wc [option] [file]
  • wc: It shows the wc command utility is utilized.
  • option: Replace with available options.
  • file: Substitute it with the respective file names.

Let’s talk about the options/flag of the wc command.

Options

The word count command is equipped with the following built-in options:

-wCounts & displays the number of words.
-lShows the new lines count.
-LDisplays the maximum word count for a single line.
-mCount the characters.
-cShows the actual bytes (size) taken by the document.

Let’s perform some examples using the above options.

How to Use the wc Command in Linux?

In this section, several examples will be performed to show the usages of the wc command in detail.

Example 1: Count Words in a File

Word count can be obtained using the wc command without any options. For instance, to get the number of words in “testfile1.txt”, run this command:

$ wc testfile1.txt

The output format shows four different features of the command:

  • 1st column: Represents the total new lines in the file (Note, it will count the blank lines in the file also).
  • 2nd Column: Shows the words count.
  • 3rd Column: Displays the size (in bytes) the file has taken.
  • 4th Column: Name of the processes file.

For Multiple Files:

This command allows you to count words for multiple files with a single command. To get stats for “testfile1.txt” & “testfile2.txt”, use this:

$ wc testfile1.txt testfile2.txt

The output displays the features (no. of lines, word count, bytes, filename) for both the files separately and combined. (total).

Example 2: Count the Characters in a File

If you have a large file and want to display the characters only rather than words, use the “m” flag. For example, to check “testfile1.txt”, execute:

$ wc -m testfile1.txt

For Multiple Files:

To find the character count in multiple files (like; testfile1.txt & testfile2.txt), utilize the below command:

$ wc -m testfile1.txt  testfile2.txt

The output displays the characters count in respective files & total.

Example 3: Count New Lines in a File

We can get the number of new lines in a file, which includes the blank files using the “l” option. For counting new lines for “testfile1.txt”, use: 

$ wc -l testfile1.txt

For Multiple Files:

For getting the number of lines of several files like; “testfile1.txt” and “testfile2.txt, execute the below command:

$ wc -l testfile1.txt testfile2.txt

Example 4: Find the Maximum Line Length

The “-L” option gives the maximum characters count in a line. For instance, the “testfile1.txt” maximum line length can find out using:

$ wc -L testfile1.txt

For Multiple Files:

For finding the maximum character count in a line for several files can be displayed running the below command:

$ wc -L testfile1.txt testfile2.txt

Example 5: Find the Size of the File in Bytes

The “c” option allows us to get the directory’s size in Bytes. For finding the size of “testfile1.txt”, use this command:

$ wc -c testfile1.txt

For Multiple Files:

We can find the size of several files, in this case, “testfile1.txt” and “testfile2.txt” by running the below-written command:

$ wc -c testfile1.txt testfile2.txt

Example 6: Count Files in a Specific Directory

If you want to count the number of files in a specific directory, we can use the “wc” command with the “find” command. To count the files for the current directory (Downloads), use this command:

$ find . -type f | wc -l

Example 7: Count Directories in a Specific Directory

The below command can be used to count the number of directories within a specified directory named “Downloads”:

Note: The “d” option looks for the directory.

$ find . -type d | wc -l

Example 8: Count Number of Files & Directory in a Specific Directory

The number of files & directories in the current/specific directory can be counted by utilizing the “ls”  command piped with the “wc” command as follows:

$ ls TestFolder1 | wc -l

Example 9: Get Users Count in a System

The “wc” command counts the number of users when used with the “passwd” file as shown below:

$ getent passwd | wc -l

Example 10: Count Word Count in a Line Using grep & wc Commands

The “grep” command is used with the wc command to count a specific in unique lines. For instance, to count the unique lines in which the word “This” is used using the grep with wc command, execute this command in the terminal:

Note: A search word used more than once in a line will be counted as one (unique line count).

$ grep "This" testFile.txt | wc -l

The output shows the word “This” is found in two lines.

That’s the end of this post.

Conclusion

In Linux, the wc command counts the words, the number of lines, Characters, maximum characters in a line, and size in bytes for the files. We can use the wc command with a different command to count the number of files, directories, and users in the system. Moreover, this article explains the options and uses of the wc command in detail with the help of examples.