wc (word count) command is used in the Linux systems to count the number of words, lines, and bytes in a text file. You can pipe and use it in numerous ways with other commands to display information about text files pretty easily.

Using wc command

General Syntax:.

wc [options..] [file_name]

Options available with wc command:

OptionDescription
-lprint number of lines in a file
-wprint number of words in a file
-cprint count of bytes in a file
-mprint count of characters in a file
-Lprint length of the longest line in a file

Example:

We will see a basic example explaining the use of the wc command in Linux.

We have a demo file in place named test.txt. Following is the content of the demo file test.txt.

This is a demo file.
This article willl help you with learning the wc command.
wc command is pretty easy  to use.
You can learn about its features in this article.
You can find many helpful articles related to all your Linux needs on this port$
 End of file


Thank you.

Using wc command on this file.

wc test.txt

Output:

gaurav@ubuntu:~$ wc test.txt
 11  51 275 test.txt
gaurav@ubuntu:~$

In this output, you can see that the numbers represent the values as follows.

  1. number of lines — 11
  2. number of words — 51
  3. number of bytes — 275

You can also display the number of lines and words using the wc command on multiple files.

Example:

wc /etc/passwd /proc/cpuinfo

Output:

gaurav@ubuntu:~$ wc /etc/passwd /proc/cpuinfo
  55   95 3102 /etc/passwd
 108  820 4688 /proc/cpuinfo
 163  915 7790 total
gaurav@ubuntu:~$

2nd line in the output displays the details about /etc/passwd file and the 3rd line /proc/cpuinfo. At the end of the output, the total figure of both the files combined is displayed.


How to Count Total Lines in a Text File

Using the -l option of the wc command, you can print the number of lines in a given text file.

General Syntax:

wc -l [file_name]

Example:

wc -l /etc/group

Output:

gaurav@ubuntu:~$ wc -l /etc/group
81 /etc/group
gaurav@ubuntu:~$

Here, in the output, we can see that the number of lines in the /etc/group file is 81.


How to Count Words in a Text File

Using -w (lowercase) option in wc command prints the total number of words in a text file in the terminal.

General Syntax:

wc -w [file_name]

Example:

wc -w test.txt

Output:

51 test.txt

There are 51 words in the text file test.txt.


Get the Byte Count of a File

You can use the -c option with the wc command to print the number of bytes in the file on your terminal.

Number of bytes utilized by a file tells us about the memory occupied by that text file.

General Syntax:

wc -c [file_name]

Example:

wc -c /etc/passwd

Output:

gaurav@ubuntu:~$ wc -c /etc/passwd
3102 /etc/passwd
gaurav@ubuntu:~$

From the output we can conclude that, the passwd file utilizes 3102 bytes.


Get Total Number of Characters in a File

Using -m option with the wc command will print the total number of characters in a given file.

General Syntax:

wc -m [file_name]

Example:

wc -m test.txt

Output:

gaurav@ubuntu:~/space$ wc -m test.txt
275 test.txt
gaurav@ubuntu:~/space$ 

The output shows that there are 275 characters in the given file.


Get Length of the Longest Line in a File

You can use the -L (uppercase) option with the wc command to print the length of the longest line in the text file. This command prints the length in terms of the number of characters in a line.

General Syntax:

wc -L [file_name]

Example:

wc -L test.txt

Output:

82 test.txt

This output represents that there are 82 characters in the longest line in the given text file test.txt.


How to Count Number of Text Files in the Current Directory

wc command can also be used to count the total number of text files in the current directory. In order to do this, you have to use wc command with the — find command through piping.

Let us look at this usage of wc command through an example.

Example:

find . -type f | wc -l

. (dot) : Here, the . (dot) means that find command should search in the current directory.

-type: This specifies the find command to look for similar file types in the current directory.

f: Here, f represent ‘files’.

Whatever is the output of this first command find will then be piped to the wc command. wc will then count the total number of files in the current directory and display the number on your terminal.

Output:

gaurav@ubuntu:~/space$ find . -type f | wc -l
13
gaurav@ubuntu:~/space$ 

The output is displayed as 13 which means that there are 13 text file of the same type in the given directory.


Conclusion

As you’d agree that the usage of wc command is pretty simple and can be easily used with your text files to get the details about them. This command can also be used in combination with other commands using the piping option.