The wc Command in Linux

Wc Command In Linux

In this tutorial, we are going to discuss wc command in Linux. Basically, the wc command stands for Word-Count in Linux. ‘wc’ command is used to count the no. of lines, words, and characters in Linux. You just need to mention the file name on which you want to perform count command.

The syntax for the following is :

# wc < [filename]
Wcinput

Here, it would print lines, words, and total characters in the file. ‘<‘ symbol is called input redirection. It simply means that it is taking input from state.txt to execute the wc command. It’s not important to use input redirection symbol. You can simply state the wc and file name to print the total number of lines, words, and characters.

Consider the file on which wc options are performed.

Countcat

Let’s discuss the options used with wc command

  • -w: print the total words
  • -l: prints the newline counts
  • -m: print the total character by character count
  • -c: prints the total characters by byte count
  • -L: prints the length of the longest line
  • –version: to check the version of the command

Print characters by byte count

In the above example, using wc only would print lines, words, and characters as well. But what if, we want to count characters by a byte count? For this, we need to use the ‘-c’ option along with wc as shown in the figure below.

The syntax for the following is:

# wc -c [filename]
Wcc

Here, it would print the total number of characters present in the file.

Print number of lines using wc command in Linux

Instead of characters byte count, if you want to print the total number of lines, we will use the ‘-l’ option along with wc command in Linux. Let’s understand from an example.

# wc -l [filename]
Wcl

It will print the number of lines present in the file.

Print total characters by character count

Earlier we discussed how to print total character by a byte count. wc command lets you calculate characters by two types; either by byte count or by character count. ‘-c’ is used to print byte count while ‘-m’ is used to printing the character count with the wc command in Linux.

The syntax for the following is:

# wc -m [filename]
Wcm

Here, the wc command would print the character count.

Print the length of the longest line

Now, what if you want to print the length of the longest line. For, this we will use the ‘L’ option along with wc command. Folks, don’t get confused in ‘-l’ and ‘-L’ as both are different options. ‘-l’ is used to print the newline counts while ‘-L’ option is used to print the length of the longest line. Let’s understand from an example.

The syntax for the following is:

# wc -L [filename]
WcLl

Here, it would print the length of the longest line in the file.

Print word count with the wc command in Linux

Till now, we have discussed how to print the total number of characters and lines. Let’s understand how we will print the total number of words. For this, we will use ‘-w’ to print the total number of words using the wc command in Linux.

The syntax for the following is:

# wc -w [filename]
Wcw

Here, it will print the total number of words present in the file.

Store the output in a file

We know how to count the lines, words, and characters. If you want to store the word count result in a file we will use ‘>’ output redirect operator. This simply means it will redirect the output of the wc command to a file. Let’s have a look at the following example given below.

The syntax for the following is:

# wc [filename] > [ filename]
Wcoutpout

It will store the result to file named state1.txt.

Count the Number of Installed Packages

We have so far discussed how to count words, lines, and characters. Let’s have a look at how to count the number of installed packages in Red Hat Linux. For this, we will just combine two commands i.e. rpm and wc. Let’s have a look at the example given below.

The syntax for the following will be:

# rpm -qa | wc -l
Wcrpm

rpm stands for Redhat Package Management which is the package manager for Red Hat Linux. ‘-qa’ option is used with rpm to list all the packages installed in the Linux system. Here, the output of the ‘rpm -qa’ will be piped to the wc command to count the number of lines. End result? We get the number of installed packages.

Check the version of wc command in Linux

We use ‘–version’ command to check the version of wc command in Linux.

The syntax for the following is:

# wc --version
Wcversion

It will simply print the version of wc command.

Conclusion

In this tutorial, we’ve gone through how to use the wc command in Linux. If you have any questions, do let us know in the comments.  If you’re interested in learning more about the command, have a look at how to use the man pages in Linux to view the command manual.

Leave a Comment

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