WC linux – (Count Lines, Words, Characters ) in Linux with best examples

WC Linux – (Count Lines, Words, Characters ) in Linux with best examples

WC command in the Linux/ Unix world is used to find out the Number of  Lines, Characters, and words. In this tutorial, we are going to learn how to use this command with examples.

Step 1: WC Command Basic Usage

If we don’t parse any parameters while use “wc ” command, then it will display “number of lines”, “number of words”  and “number of bytes” of a file. Execute the following command to get the information from myserverfix.txt file.

wc myservefix.txt

the output of this command is the following:

11  16 1500   myserverfix.txt

In the output

  • 11 is the number of lines.
  • 16 is the number of words.
  • 1500 is the number of bytes.

 

Step 2:  Count Number of Words in a File

To count the number of words in a file, we need to pass an additional parameter ‘w’ with the command. which is the following.

wc -w myserverfix.txt

the output of this command

22 myserver.txt

Here 22 is the number of words in the myserver.txt file.

 

Step 3: Count Number of Lines in a File.

To count the number of lines in a file, we need to parse another parameter in the “wc” command. Execute the following command in the terminal.

wc -l myserverfix.txt

The output of this command is following.

22 myserverfix.txt

Here 22 is the number of lines in the file.

 

Step 4:  Check Number of Characters in a  File.

To check the number of characters in a file execute the ‘wc’ command with -c parameters. This will display the information, execute the following command.

wc -c myserverfix.txt

The output of this command is following.

134 myserverfix.txt

 

Step 5: Check the Length of the Longest line in a file.

wc -L myserverfix.txt

It will display the length of the Longest Line in a File. However, if you want to explore more options that WC command provides, just execute the following command in the terminal.

wc --help

The output of this command is going to be the following.

Usage: wc [OPTION]... [FILE]...
  or:  wc [OPTION]... --files0-from=F
Print newline, word, and byte counts for each FILE, and a total line if
more than one FILE is specified.  With no FILE, or when FILE is -,
read standard input.
  -c, --bytes            print the byte counts
  -m, --chars            print the character counts
  -l, --lines            print the newline counts
  -L, --max-line-length  print the length of the longest line
  -w, --words            print the word counts
      --help			display this help and exit
      --version			output version information and exit

 

The Linux WC command is very helpful when we are using it with the bash scripts and other commands. If you liked this article do check out our other articles.

Install Ruby on Ubuntu 20.04